标签:style blog os io for 2014 amp log
先把每条边的权值从大到小排序,在一个个枚举,利用并查集判断是否在一个集合中。。。
<span style="font-size:24px;">#include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> #include<stdlib.h> #include<cmath> using namespace std; const int maxn=205; const int maxm=1005; const int inf=(0x7f7f7f7f); #define min(a,b) ((a)<(b)?(a):(b)) int fa[maxn]; int n,m; struct node { int x,y,z; }p[maxm]; bool cmp(node g,node h) { return g.z<h.z; } int find(int x) { while (fa[x] != x) x = fa[x]; return x; } int main() { int s,t; while(~scanf("%d%d",&n,&m)) { int i,j; for(i=0;i<m;i++) scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].z); sort(p,p+m,cmp); int q; scanf("%d",&q); while(q--) { scanf("%d%d",&s,&t); int min1=inf; for(j=0;j<m;j++){ for(i=1;i<=n;i++) fa[i]=i; for(i=j;i<m;i++) { int fy=find(p[i].y); int fx=find(p[i].x); if(fy!=fx) fa[fy]=fx; if(find(s)==find(t)) { min1=min(min1,p[i].z-p[j].z); break; } } } if(min1==inf) puts("-1"); else printf("%d\n",min1); } } return 0; }</span>
HDU 1598 find the most comfortable road(并查集+枚举),布布扣,bubuko.com
HDU 1598 find the most comfortable road(并查集+枚举)
标签:style blog os io for 2014 amp log
原文地址:http://blog.csdn.net/asuxiexie/article/details/38510749