N - 嘤嘤嘤 (并查集+枚举)
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;
}
N - 嘤嘤嘤 (并查集+枚举)的更多相关文章
- poj2912(种类并查集+枚举)
题目:http://poj.org/problem?id=2912 题意:n个人进行m轮剪刀石头布游戏(0<n<=500,0<=m<=2000),接下来m行形如x, y, ch ...
- 2019.01.22 zoj3583 Simple Path(并查集+枚举)
传送门 题意简述:给出一张图问不在从sss到ttt所有简单路径上的点数. 思路: 枚举删去每个点然后把整张图用并查集处理一下,同时不跟sss和ttt在同一个连通块的点就是满足要求的点(被删去的不算). ...
- 1050. [HAOI2006]旅行【并查集+枚举】
Description 给你一个无向图,N(N<=500)个顶点, M(M<=5000)条边,每条边有一个权值Vi(Vi<30000).给你两个顶点S和T,求 一条路径,使得路径上最 ...
- BZOJ 1050 旅行comf 并查集+枚举下界
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1050 题目大意: 给你一个无向图,N(N<=500)个顶点, M(M<=5 ...
- POJ - 2912 Rochambeau (带权并查集+枚举)
题意:有N个人被分为了三组,其中有一个人是开了挂的.同组的人的关系是‘=’,不同组的人关系是‘<’或'>',但是开了挂的人可以给出自己和他人任意的关系.现在要根据M条关系找出这个开了挂的人 ...
- hdu 1598 find the most comfortable road(并查集+枚举)
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- POJ2912 Rochambeau —— 种类并查集 + 枚举
题目链接:http://poj.org/problem?id=2912 Rochambeau Time Limit: 5000MS Memory Limit: 65536K Total Submi ...
- USACO环绕岛屿Surround the Islands 并查集 枚举暴力
题目描述 Farmer John has bought property in the Caribbean and is going to try to raise dairy cows on a b ...
- poj2912(带权并查集+枚举)
题目链接:http://poj.org/problem?id=2912 题意:给n个人,m组关系,玩石头剪刀布的游戏,n个人中除一个人judge以外,其他人属于3个group(即石头.剪刀.布),他们 ...
随机推荐
- DAY10-MYSQL数据类型
一 介绍 存储引擎决定了表的类型,而表内存放的数据也要有不同的类型,每种数据类型都有自己的宽度,但宽度是可选的 详细参考: http://www.runoob.com/mysql/mysql-data ...
- 什么是个CDN???CDN是干什么的??
1.什么是CDN??? CDN的全称是Content Delivery Network,即内容分发网络.其目的是通过在现有的Internet中增加一层新的网络架构,将网站的内容发布到最接近用户的网络& ...
- JAVA基础知识总结14(String、StringBuffer、StringBuilder)
1.String字符串: java中用String类进行描述.对字符串进行了对象的封装.这样的好处是可以对字符串这种常见数据进行方便的操作.对象封装后,可以定义N多属性和行为. 如何定义字符串对象呢? ...
- java的类型转换问题。int a = 123456;short b = (short)a;System.out.println(b);为什么结果是-7616?
这个从二进制来解释: int 是 32 位,也就是最多能表示长度为 32 位的二进制整数.第一位是符号位,表示正负,0 表示正的,1 表示负的.当首位为 1(为负)时,把所有的位取反(0 变成 1,1 ...
- Shell表达式,如${file##*/}
Shell表达式,如${file##*/} 2017年10月26日 15:24:40 阅读数:343 今天看一个脚本文件的时候有一些地方不太懂,找了一篇文章看了一些,觉得不错,保留下来. 假设我们定义 ...
- Android的性能优化
ArrayList和Vector ArrayList和Vector都是内部以数组实现的List,它们两唯一的区别就是对多线程的支持,ArrayList是线程不安全的,而Vector内部对大多数方法都做 ...
- ZOJ - 3057 D - Beans Game(三堆威佐夫博弈)
Beans Game Time Limit: 5 Seconds Memory Limit: 32768 KB There are three piles of beans. TT and DD pi ...
- DjVu转PDG的方法与步骤
作者:马健邮箱:stronghorse_mj@hotmail.com发布:2008.08.03更新:2008.08.24 补充说明:此文成文较早,当时PDG浏览器只支持纯正PDG,不支持名为PDG,实 ...
- 通过委托更新UI(异步加载)
来自:http://blog.csdn.net/gongzhe2011/article/details/27351853 using System.Windows.Forms; using Syste ...
- (转)C# TextBox ReadOnly / Enabled 时,后台无法取值问题
当页面上的某个TextBox 设置了属性ReadOnly = "True" 或 Enabled = "False" 时,在客户端为其赋值后,在后台代码中却无法获 ...