poj 1679 The Unique MST
来源:互联网 发布:2012年网络炒作事件 编辑:程序博客网 时间:2024/06/05 18:35
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;struct data{ int u,v,w; int same; int use; int del;}e[10000+5];int n,m,bin[1000+5],f;bool cmp(data a,data b){ return a.w<b.w;}int Find(int x){ int s; for(s=x;bin[s]>=0;s=bin[s]); while(s!=x) { int t=bin[x]; bin[x]=s; x=t; } return s;}void Union(int x1,int x2){ int f1=Find(x1),f2=Find(x2); int t=bin[f1]+bin[f2]; if(bin[f1]>bin[f2]) { bin[f1]=f2; bin[f2]=t; } else { bin[f2]=f1; bin[f1]=t; }}int kruskal(){ int i,num=0,u,v,ans=0; for(i=0;i<=n+5;i++) bin[i]=-1; for(i=0;i<m;i++) { //printf("%d\n",i); if(e[i].del==1) continue; u=e[i].u; v=e[i].v; if(Find(u)!=Find(v)) { Union(u,v); num++; ans+=e[i].w; if(f) e[i].use=1; } if(num>=n-1) break; } return ans;}int main(){ int _,i,j,k,ans1,ans2; scanf("%d",&_); while(_--) { scanf("%d%d",&n,&m); for(i=0;i<m;i++) { scanf("%d %d %d",&e[i].u,&e[i].v,&e[i].w); e[i].u--; e[i].v--; e[i].use=0; e[i].same=0; e[i].del=0; } for(i=0;i<m;i++) for(j=0;j<m;j++) { if(i==j) continue; if(e[i].w==e[j].w) e[i].same=e[j].same=1; } f=1; sort(e,e+m,cmp); ans1=kruskal(); f=0; for(i=0;i<m;i++) { if(e[i].same==1&&e[i].use==1) { e[i].del=1; ans2=kruskal(); if(ans2==ans1) { printf("Not Unique!\n"); break; } e[i].del=0; } } if(i>=m) printf("%d\n",ans1); } return 0;}
0 0
- POJ 1679 The Unique MST
- poj 1679 The Unique MST
- poj 1679 The Unique MST
- POJ 1679 The Unique MST
- poj 1679 The Unique MST
- poj 1679 The Unique MST
- POJ-1679 The Unique MST
- POJ 1679 The Unique MST
- POJ 1679 The Unique MST
- POJ-1679-The Unique MST
- Poj 1679 The Unique MST
- poj 1679 The Unique MST
- POJ-1679-The Unique MST
- POJ 1679 The Unique MST
- POJ 1679 The Unique MST
- POJ-1679-The Unique MST
- poj 1679 The Unique MST
- poj 1679-The Unique MST
- Android TextView文字过多,添加滚动条
- SolrJ、Request Handler
- ASP.NET 4.0 安裝在 IIS6 最常遇到的四個問題
- MapReduce: 提高MapReduce性能的建议(1)
- 六、Solr高亮与Field权重
- poj 1679 The Unique MST
- sessionListener 实现解决一个账号不同时在线需求。
- JSON详解
- NSInvocation简单使用
- 五种 JSP页面跳转方法详解
- 文章标题
- 并发编程之GCD(来自Xcode Dev 亮了的原创开发技术博客)
- 七、Solr服务部署和安全
- linux之return和exit引发的大问题(vfork和fork)