POJ 3522
来源:互联网 发布:阿里企业邮箱端口 编辑:程序博客网 时间:2024/06/05 18:02
#include<cstdio>#include<algorithm>#include<vector>using namespace std;const int maxn=1005;struct Edge{int from,to,w;Edge(int from,int to,int w):from(from),to(to),w(w){}bool operator < (const Edge s)const{return w<s.w;}};struct krusckal{int n,ans;int father[maxn];vector<int>path;vector<Edge> edge;int getfather(int x){return x==father[x]?x:father[x]=getfather(father[x]);}void init(int n){this->n=n;path.clear();edge.clear();}void add_edge(int from,int to,int w){edge.push_back(Edge(from,to,w));}bool solve(int front){int x,y,k=front,cnt=0;path.clear();ans=2e9;for(int i=1;i<=n;i++)father[i]=i;while(cnt<n-1&&k<edge.size()){x=getfather(edge[k].from);y=getfather(edge[k].to);if(x!=y){father[x]=y;cnt++;path.push_back(k);}k++;}if(cnt<n-1)return false;ans=edge[path[cnt-1]].w-edge[front].w;return true;}};int main(){int n,i,j,x,m,y,w,ans;while(true){scanf("%d%d",&n,&m);if(!n&&!m)return 0;krusckal solver;solver.init(n);ans=2e9;for(i=1;i<=m;i++){scanf("%d%d%d",&x,&y,&w);solver.add_edge(x,y,w);}sort(solver.edge.begin(),solver.edge.end());for(i=0;i<=m-n+1;i++){if(solver.solve(i))ans=min(ans,solver.ans);}printf("%d\n",ans==2e9?-1:ans);}}
0 0
- POJ 3522
- POJ 3522
- poj 3522
- POJ 3522
- POJ 3522
- POJ 3522 暴力+图论
- POJ 3522 Slim Span
- poj 3522 Slim Span
- POJ 3522 Slim Span
- poj 3522 Slim Span
- poj 3522 Slim Span
- poj 3522 Slim Span
- poj 3522 Slim Span
- poj 3522 Kruskal
- POJ 3522 Slim Span
- poj-3522-Slim
- poj 3522 Slim Span
- POJ 3522 Slim Span
- Android Runnable 运行在那个线程
- iOS中的事件传递和响应者链条
- POJ 1001
- SiteMesh简单引入demo
- 自学微信二次开发(1)
- POJ 3522
- bzoj2154: Crash的数字表格
- HDU 1045.Fire Net【DFS暴力搜索】【3月15】
- RemoteViews(一):应用
- Python3缩进对逻辑的影响
- 明明的随机数
- 用终端安装第三方
- android.os.Build
- Comparable与Comparator浅析