Our lovely KK has a difficult Social problem.
A big earthquake happened in his area.
N(2≤N≤2000)N(2≤N≤2000) cities have been implicated. All the roads between them are destroyed.
Now KK was commissioned to rebuild these roads.
However, after investigation,KK found that some roads are too damaged to rebuild.
Therefore, there are only M(0≤M≤15000)M(0≤M≤15000) roads can be rebuilt.
KK needs to make sure that there is a way between any two cities, and KK wants to rebuild roads as few as possible.
With rebuilding minimal number of roads, he also wants to minimize the difference between the price of the most expensive road been built and the cheapest one.

Input

The first line of the input file contains an integer T(1≤T≤10)T(1≤T≤10), which indicates the number of test cases.

For each test case,The first line includes two integers N(2≤N≤2000)N(2≤N≤2000),M(0≤M≤15000)M(0≤M≤15000).

The next MM lines include three integers a,b,c(a≠b,1≤c≤2∗109)a,b,c(a≠b,1≤c≤2∗109),indicating there is a undirected edge between aa and bb,the cost is cc.

Output

For each test case, output the smallest difference between the price of the most expensive road and the cheapest one.If there is no legal solution, then output -1.

Sample Input

2
5 10
1 2 9384
1 3 887
1 4 2778
1 5 6916
2 3 7794
2 4 8336
2 5 5387
3 4 493
3 5 6650
4 5 1422
2 0

Sample Output

1686
-1

题解:这道题的时间给的是6s,排完序后所以可以枚举每一种修路的情况,但是如果把所有的情况都存在数组里会溢出,就只把最贵的和最便宜的放数组然后做差,然后找到最小的。

此题坑点有主要有二:

1,存所有情况数组溢出

2,定义那个较大的数一定要尽量大(在比较求差值最小的时候)

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int pre[100005];
struct node
{int x,y;long long int val;
}road[200005];
int find(int x)
{if(x==pre[x])return x;else{return pre[x]=find(pre[x]);}
}
bool merge(int x,int y)
{int fx=find(x);int fy=find(y);if(fx!=fy){pre[fx]=fy;return true;   }else{return false;}
}
bool cmp(node x,node y)
{return x.val<y.val;
}
int main()
{int n;cin>>n;while(n--){int m,s;cin>>m>>s;for(int j=1;j<=m;j++){pre[j]=j;} for(int t=1;t<=s;t++){scanf("%d%d%d",&road[t].x,&road[t].y,&road[t].val);} sort(road+1,road+s+1,cmp);long long  int cnt=0;int a[3000005];int crt=0; int l=0;for(int t=1;t<=s;t++){for(int j=t;j<=s;j++){if(merge(road[j].x,road[j].y)){if(cnt%(m-1)==m-2||(cnt%(m-1)==0)){a[crt]=road[j].val;crt++;}cnt++;}if(cnt%(m-1)==0){for(int k=1;k<=m;k++){pre[k]=k;} break;}}}long long int Min=999999999;int k=0;if(crt%2==1){for(int t=0;t<crt-1;t=t+2){  if(Min>a[t+1]-a[t]){Min=a[t+1]-a[t];}}}else{for(int t=0;t<crt;t=t+2){  if(Min>a[t+1]-a[t]){Min=a[t+1]-a[t];}}}if(cnt>=m-1){printf("%lld\n",Min);}else{printf("-1\n");}} return 0;
}

转载于:https://www.cnblogs.com/Staceyacm/p/10782100.html

N - 嘤嘤嘤 (并查集+枚举)相关推荐

  1. poj2912(种类并查集+枚举)

    题目:http://poj.org/problem?id=2912 题意:n个人进行m轮剪刀石头布游戏(0<n<=500,0<=m<=2000),接下来m行形如x, y, ch ...

  2. poj2912(带权并查集+枚举)

    题目链接:http://poj.org/problem?id=2912 题意:给n个人,m组关系,玩石头剪刀布的游戏,n个人中除一个人judge以外,其他人属于3个group(即石头.剪刀.布),他们 ...

  3. pku2192(并查集+枚举)

    这题的话,跟食物链很相识,也是有三类,所以那个公式的话,还是一样的 题意是要找出里面唯一的一个judge,judge的特别就是他没有固定属于哪一类,所以只能枚举每一个人当judge的情况了 满足是唯一 ...

  4. HDU-5624 -KK's Reconstruction(并查集+枚举)

    Problem Description Our lovely KK has a difficult Social problem. A big earthquake happened in his a ...

  5. hdu 1598 find the most comfortable road (并查集+枚举)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000/ ...

  6. BZOJ 1050: [HAOI2006]旅行comf(枚举+并查集)

    [HAOI2006]旅行comf Description 给你一个无向图,N(N<=500)个顶点, M(M<=5000)条边,每条边有一个权值Vi(Vi<30000).给你两个顶点 ...

  7. hdu1589(枚举+并查集)

    find the most comfortable road Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  8. Rochambeau POJ - 2912 (枚举和加权并查集+路径压缩)找唯一裁判

    题意:有n个人玩石头剪刀布,有且只有一个裁判.除了裁判每个人的出拳形式都是一样的. a<b表示b打败a,a=b表示a和b出拳一样,平手.a>b表示a打败b. 给出m个回合的游戏结果,问能否 ...

  9. POJ 1944 Fiber Communications (枚举 + 并查集 OR 线段树)

    题意 在一个有N(1 ≤ N ≤ 1,000)个点环形图上有P(1 ≤ P ≤ 10,000)对点需要连接.连接只能连接环上相邻的点.问至少需要连接几条边. 思路 突破点在于最后的结果一定不是一个环! ...

最新文章

  1. 整理了一下SQL Server里面可能经常会用到的日期格式转换方法
  2. highcharts图标插件详解一
  3. 正则表达式笔试题php,2017年初级PHP程序员笔试题
  4. 通过Java代码装配bean
  5. Nginx 服务器开启status页面检测服务状态
  6. 路由 RIP 协议 和 滞空路由
  7. ajax中res和req,表达式是否为每个请求提供了不同的req和res对象?
  8. 单片机蜂鸣器发出叮咚c语言程序,单片机门铃程序设计 按键按下触发蜂鸣器门铃“叮咚”响...
  9. USGS官网批量下载卫星数据方法
  10. 【NLP】AI相关比赛汇总(2022)
  11. web版收银系统,支持支付宝,微信扫描枪支付,数字输入
  12. 使用windows系统给C盘分盘
  13. 华为过程可信cib是指_华为EMUI10取消IMEI?数字联盟可信ID——更可靠的移动设备唯一标识...
  14. yxc_第二章 数据结构(一)_链表
  15. 2021年十大最有前途的编程语言,第1名实至名归or名不副实?
  16. 抢先体验TPS轻松过万的SDAG区块链(二)
  17. 【环境问题】基础Web环境搭建问题记录2——Failed to obtain JDBC Connection;
  18. android 绘图之Path与Paint详解
  19. 百度竞价中的精确匹配,短语匹配,广泛匹配有什么区别。
  20. 雷蛇灵刃定制版Win10系统不稳定及处理方案

热门文章

  1. cad2010多个文件并排显示_并排查看Excel工作表只需1个小动作,工作效率大涨百倍!...
  2. 考研c 语言程序设计题库,温州大学c语言程序设计考研复试核心题库(23页)-原创力文档...
  3. 人工智能的炒作_人工智能与网络安全结合从炒作走向现实
  4. python 单例类
  5. python packaging quickstart
  6. TensorFlow tf.keras.callbacks.ModelCheckpoint
  7. opencv-api getAffineTransform()
  8. Oracle归档日志管理
  9. 查看mysql连接数_查看mysql 连接数
  10. 元气骑士机器人旁边建筑_元气骑士:锤落谁家?锤子更适合机器人还是能双持的骑士呢?...