HDU 5624 KK's Reconstruction
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 5624 KK's Reconstruction相关的知识,希望对你有一定的参考价值。
?????????lib lines scan play out ext ram make 1.0
KK???s Reconstruction
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 360 Accepted Submission(s): 151
Problem Description
Our lovely KK has a difficult Social problem.
A big earthquake happened in his area.
N 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 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 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 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, which indicates the number of test cases.
For each test case,The first line includes two integers N???M.
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???M.
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
???
???????????????????????????????????????????????????????????????????????? ????????????
???????????????????????????????????????????????????????????????????????? ????????????
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <vector> #include <queue> #include <cstdlib> #include <iomanip> #include <cmath> #include <ctime> #include <map> #include <set> using namespace std; #define lowbit(x) (x&(-x)) #define max(x,y) (x>y?x:y) #define min(x,y) (x<y?x:y) #define MAX 100000000000000000 #define MOD 1000000007 #define pi acos(-1.0) #define ei exp(1) #define PI 3.141592653589793238462 #define ios() ios::sync_with_stdio(false) #define INF 0x3f3f3f3f #define mem(a) (memset(a,0,sizeof(a))) typedef long long ll; int f[2009]; int n,m,t; struct Node { ll u; ll v; ll w; friend bool operator<(const Node&a,const Node &b) { return a.w<b.w; } }e[15009]; int find(int x) { return f[x]==x?x:find(f[x]); } ll kruskal() { ll ans=INF,minn,maxn; int q; for(int i=0;i<m;i++) { minn=e[i].w,maxn=e[i].w; q=1; for(int k=1;k<=n;k++) f[k]=k; for(int j=i;j<m;j++) { int u=find(e[j].u); int v=find(e[j].v); if(u!=v) { maxn=max(maxn,e[j].w); if(u<v) f[u]=v; else f[v]=u; q++; if(q==n) break; } } if(q==n) ans=min(ans,maxn-minn); } if(ans==INF) return -1; return ans; } int main() { scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(int i=0;i<m;i++) { scanf("%lld%lld%lld",&e[i].u,&e[i].v,&e[i].w); } sort(e,e+m); printf("%lld\n",kruskal()); } return 0; }
以上是关于HDU 5624 KK's Reconstruction的主要内容,如果未能解决你的问题,请参考以下文章