标签:des style blog class code java
4 4 1 2 2 2 3 4 1 4 1 3 4 2 2 1 3 1 2
1 0
kruskal算法+枚举每一种情况
#include"stdio.h" #include"string.h" #include"stdlib.h" #define N 205 #define M 1005 const int inf=(int)1e10; int fa[N]; struct node { int a,b,v; }e[M]; int cmp(const void*a,const void*b) { return (*(struct node*)a).v-(*(struct node*)b).v; } int Min(int a,int b) { return a<b?a:b; } int Max(int a,int b) { return a>b?a:b; } int find(int k) { if(k!=fa[k]) fa[k]=find(fa[k]); return fa[k]; } int main() { int n,m,i,j; while(scanf("%d%d",&n,&m)!=-1) { for(i=0;i<m;i++) { scanf("%d%d%d",&e[i].a,&e[i].b,&e[i].v); } qsort(e,m,sizeof(e[0]),cmp); int q,a,b; scanf("%d",&q); while(q--) { scanf("%d%d",&a,&b); int ans=inf; for(i=0;i<m;i++) { int min=inf,max=0; for(j=1;j<=n;j++) { fa[j]=j; } for(j=i;j<m;j++) { int f1=find(e[j].a); int f2=find(e[j].b); if(f1!=f2) { fa[f1]=f2; max=Max(max,e[j].v); min=Min(min,e[j].v); } if(find(a)==find(b)) { ans=Min(ans,max-min); break; } } } if(ans==inf) printf("-1\n"); else printf("%d\n",ans); } } return 0; }
hdu 1598 find the most comfortable road,布布扣,bubuko.com
hdu 1598 find the most comfortable road
标签:des style blog class code java
原文地址:http://blog.csdn.net/u011721440/article/details/25184267