标签:
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4096 Accepted Submission(s): 1768
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<algorithm> #include<cstdlib> using namespace std; #define INF 0x7fffffff int n,m,q,set[1010]; struct node { int x,y,w; }e[1010]; bool cmp(node a,node b) { return a.w<b.w; } int find(int x) { if(x!=set[x]) set[x]=find(set[x]); return set[x]; } int main() { while(scanf("%d%d",&n,&m)!=EOF) { memset(e,0,sizeof(e)); for(int i=0;i<m;i++) { scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].w); } sort(e,e+m,cmp); scanf("%d",&q); while(q--) { int a,b,minn; minn=INF; scanf("%d%d",&a,&b); for(int i=0;i<m;i++) { for(int j=0;j<=n;j++) set[j]=j; for(int j=i;j<m;j++) { int fx,fy; fx=find(e[j].x); fy=find(e[j].y); if(fx!=fy) set[fx]=fy; if(find(a)==find(b)) { if(minn>e[j].w-e[i].w) minn=e[j].w-e[i].w; break; } } } if(minn==INF) printf("-1\n"); else printf("%d\n",minn); } } return 0; }
标签:
原文地址:http://www.cnblogs.com/a972290869/p/4259059.html