HDU-5624 -KK's Reconstruction(並查集)
阿新 • • 發佈:2018-11-16
Problem Description
Our lovely KK has a difficult Social problem.
A big earthquake happened in his area.
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) 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), which indicates the number of test cases.
For each test case,The first line includes two integers N(2≤N≤2000),M(0≤M≤15000).
The next M lines include three integers a,b,c(a≠b,1≤c≤2∗109),indicating there is a undirected edge between a and b,the cost is c.
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
題意:有N個城市,M 條路,讓你去找路使得所有的城市都連通,並且求所找的路使得最大的邊和最小的邊的差值最小。 思路:沒明白題意之前以為這個題是最小生成樹...明白題意之後才恍然大悟。就是遍歷一遍每個可能通的情況的最大的邊和最小的邊的差值,然後判斷一下。最後輸出答案就行。 技巧:結構體組移位遍歷。 坑點:這個題給了6s,G++能過,C++過不了~
A big earthquake happened in his area.
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) 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.
For each test case,The first line includes two integers N(2≤N≤2000),M(0≤M≤15000).
The next M lines include three integers a,b,c(a≠b,1≤c≤2∗109),indicating there is a undirected edge between a
題意:有N個城市,M 條路,讓你去找路使得所有的城市都連通,並且求所找的路使得最大的邊和最小的邊的差值最小。 思路:沒明白題意之前以為這個題是最小生成樹...明白題意之後才恍然大悟。就是遍歷一遍每個可能通的情況的最大的邊和最小的邊的差值,然後判斷一下。最後輸出答案就行。 技巧:結構體組移位遍歷。 坑點:這個題給了6s,G++能過,C++過不了~
1#include<cstdio> 2 #include<algorithm> 3 using namespace std; 4 int pre[2005]; 5 6 struct node{ 7 int a,b,len; 8 }l[15005]; 9 10 int find(int x){ 11 while(pre[x]!=x){ 12 int r=pre[x]; 13 pre[x]=pre[r]; 14 x=r; 15 } 16 return x; 17 } 18 19 void merge(int x,int y){ 20 int fx=find(x); 21 int fy=find(y); 22 if(fx!=fy) pre[fx]=fy; 23 } 24 25 int cmp(node x,node y){ 26 return x.len<y.len; 27 } 28 29 int main(){ 30 int T,N,M,a,b,len; 31 scanf("%d",&T); 32 while(T--){ 33 scanf("%d%d",&N,&M); 34 for(int i=0;i<M;i++) 35 scanf("%d%d%d",&l[i].a,&l[i].b,&l[i].len); 36 sort(l,l+M,cmp); 37 int ans=2e9,flag=0; 38 for(int j=0;j<M;j++){ 39 for(int i=1;i<=N;i++) 40 pre[i]=i; 41 int cnt=0,min=2e9,max=0,val; 42 for(int i=j,k=0;k<M;i++,k++){ 43 if(i>=M) i=0; //技巧 44 if(find(l[i].a)==find(l[i].b)) continue ; 45 if(l[i].len>max) max=l[i].len; 46 if(l[i].len<min) min=l[i].len; 47 merge(l[i].a,l[i].b); 48 cnt++; 49 } 50 if(cnt==N-1) flag=1; 51 val=max-min; 52 if(val>=0&&val<ans) ans=val; 53 } 54 if(flag==0) printf("-1\n"); 55 else printf("%d\n",ans); 56 } 57 return 0; 58 }