标签:des style color os io for ar art
4 4 1 2 2 2 3 4 1 4 1 3 4 2 2 1 3 1 2
1 0
# include<iostream> # include<cstdio> # include<algorithm> using namespace std; const int maxn=1000+5; struct node{ int st; int ed; int fu; }road[maxn]; int p[maxn]; int st,ed; int miin; int inf=1000000+5; bool cmp(node x,node y) { return x.fu<y.fu; } int find(int x) { return p[x]== x ? x:p[x]=find(p[x]); } int main() { int n,m; while(cin>>n>>m) { for(int i=1;i<=m;i++) scanf("%d%d%d",&road[i].st,&road[i].ed,&road[i].fu); sort(road+1,road+m+1,cmp); int tot; cin>>tot; while(tot--) { miin=inf; scanf("%d%d",&st,&ed); for(int i=1;i<=m;i++) { for(int i=1;i<=n;i++) p[i]=i; for(int j=i;j<=m;j++) { int x=find(road[j].st); int y=find(road[j].ed); if(x!=y) p[x]=y; if(find(st)==find(ed)) { int sum=road[j].fu-road[i].fu; if(sum<miin) miin=sum; break; } } } if(miin==inf) printf("-1\n"); else printf("%d\n",miin); } } return 0; }
HDU 1598 find the most comfortable road (枚举+Kruskal) 最短路,布布扣,bubuko.com
HDU 1598 find the most comfortable road (枚举+Kruskal) 最短路
标签:des style color os io for ar art
原文地址:http://blog.csdn.net/u013514722/article/details/38510551