标签:
并查集+枚举,深搜TLE,感觉广搜有有点戏,没心情试
#include<iostream> #include<algorithm> #define maxn 1010 using namespace std; struct stu { int a,b,l; }; stu mapp[maxn]; int f[210]; int n,m; bool cmp(stu x,stu y) { return x.l<y.l; } int dfs(int x) { if(f[x]!=x) f[x]=dfs(f[x]); return f[x]; } void build(int x,int y) { if(dfs(x)!=dfs(y)) f[dfs(x)]=dfs(y); } int main() { while(cin>>n>>m) { for(int i=0;i<m;i++) cin>>mapp[i].a>>mapp[i].b>>mapp[i].l; sort(mapp,mapp+m,cmp); int t; cin>>t; while(t--) { int re=1<<30; int x,y; cin>>x>>y; for(int i=0;i<m;i++) { for(int s=0;s<=n;s++) f[s]=s; for(int j=i;j<m;j++) { build(mapp[j].a,mapp[j].b); //cout<<dfs(x)<<"~"<<dfs(y)<<endl; if(dfs(x)==dfs(y)) { re=min(mapp[j].l-mapp[i].l,re); break; } } } if(re==1<<30) cout<<"-1"<<endl; else cout<<re<<endl; } } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
hdu 1598 find the most comfortable road
标签:
原文地址:http://blog.csdn.net/zafkiel_nightmare/article/details/47992103