Uva 10462 Is There A Second Way Left? (次小生成树)
2015-02-10 12:39
330 查看
大概题意是找次小生成树的...不过有三种情况...第一种是最小生成树都求不到 既不连通..,第二种是除最小生成树外没有其他生成树的..第三种是除最小生成树有多个生成树只输出最优情况...那么就是次小生成树咯...
#include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<iostream> #include<algorithm> using namespace std; struct node { int x,y,s; }s[2000]; int cmp(node a,node b) { return a.s<b.s; } int f[120]; void inti() { for(int i=0;i<=100;i++) f[i]=i; } int find(int x) { if(x!=f[x]) return f[x]=find(f[x]); return x; } int main() { int T; cin>>T; int c[2000]; for(int cas=1;cas<=T;cas++) { inti(); int flag=1; int n,m; cin>>n>>m; for(int i=0;i<m;i++) { scanf("%d %d %d",&s[i].x,&s[i].y,&s[i].s); } sort(s,s+m,cmp); int ans=0,t=0; for(int i=0;i<m;i++) { int x=find(s[i].x); int y=find(s[i].y); if(x!=y) { f[x]=y; c[t]=i; t++; ans+=s[i].s; } if(t==n-1) break; } if(t!=n-1) { printf("Case #%d : No way\n",cas); continue; } int ansx=0x7fffffff; for(int i=0;i<t;i++) { inti(); int tt=0,an=0; for(int j=0;j<m;j++) { if(j==c[i]) continue; int x=find(s[j].x); int y=find(s[j].y); if(x!=y) { f[x]=y; an+=s[j].s; tt++; } if(tt==n-1) break; } if(tt==n-1) { ansx=min(ansx,an); } } if(ansx==0x7fffffff) printf("Case #%d : No second way\n",cas); else printf("Case #%d : %d\n",cas,ansx); } return 0; }
相关文章推荐
- UVA - 10462 Is There A Second Way Left?(次小生成树)
- UVA 10462 Is There A Second Way Left? (次小生成树+kruskal)
- kuangbin专题八 UVA10462 Is There A Second Way Left?(不错的好题,次小生成树)
- UVA - 10462 Is There A Second Way Left?(次小生成树)
- UVA 10462 Is There A Second Way Left?(次小生成树)
- 【kruskal 求次小生成树存在】Is There A Second Way Left? (UVA 10462)
- Is There A Second Way Left? - UVA 10462 - 次小生成树
- uva10462 Is There A Second Way Left? (次小生成树)
- UVa 10462 Is There A Second Way Left? (Kruskal,次小生成树)
- Is There A Second Way Left? UVA - 10462(次小生成树,Kruskal)
- UVa 10462 Is There A Second Way Left?
- UVA10462-Is There A Second Way Left?
- UVA-10462-Is There A Second Way Left? (次小生成树带重边)
- UVA 10462 - Is There A Second Way Left?
- Uva 10462 Is There A Second Way Left? Kruskal求次小生成树
- UVA10462_Is There A Second Way Left? _kruskal的次小生成树
- UVA 10462 Is There A Second Way Left? 次小生成树
- UVA 10462 —— Is There A Second Way Left?——————【最小生成树、kruskal、重边】
- UVA 10462 Is There A Second Way Left?(Kruskal算法/Prim算法)
- D - Is There A Second Way Left? (次小生成树)