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.
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.
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++过不了~。G++快啊!懒得剪枝了~
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 if(cnt==N-1) break; 50 } 51 if(cnt==N-1) flag=1; 52 val=max-min; 53 if(val>=0&&val<ans) ans=val; 54 } 55 if(flag==0) printf("-1\n"); 56 else printf("%d\n",ans); 57 } 58 return 0; 59 }