Is There A Second Way Left? - UVA 10462 - 次小生成树
2017-07-03 13:28
260 查看
Is There A Second Way Left? - UVA 10462 - 次小生成树
题意
判断是否有最小生成树,次小生成树,如果有次小生成树,则输出次小生成树的总权值。思路
题意很简单,思路也很直接,无脑写就可以。没想出来prim有什么比较简单的写的方法,用Kruskal写的。代码
// // main.cpp // L // // Created by LucienShui on 2017/7/2. // Copyright © 2017年 LucienShui. All rights reserved. // #include <iostream> #include <algorithm> #include <set> #include <string> #include <vector> #include <queue> #include <map> #include <stack> #include <iomanip> #include <cstdio> #include <cstring> #include <cmath> #include <cctype> #define memset(a,b) memset(a,b,sizeof(a)) #define il inline #define ll long long #define ull unsigned long long using namespace std; #define maxn 107 const int INF = 0x3f3f3f3f; int pre[maxn],used[maxn<<1],result,cnt; struct Edge { int x,y,v; bool operator < (const Edge tmp) const { return v < tmp.v; } }edge[maxn<<1]; void init(int n) { for(int i=0 ; i<=n ; i++) pre[i] = i; } int Find(int x) {//while以及附帶路徑壓縮的版本,遞歸在數據量比較大的時候可能爆 if(x==pre[x]) return x; int p=x,q; while(x!=pre[x]) x=pre[x]; while(p!=pre[p]) q=pre[p],pre[p]=x,p=q; return x; } int kruskal(int n, int m) { int ans = cnt = 0; init(n); sort(edge,edge+m); for(int i=0 ; i<m ; i++) { int p = Find(edge[i].x), q = Find(edge[i].y); if(p!=q) { pre[p] = q; ans += edge[i].v; used[cnt++] = i; } } if(cnt+1 == n) return ans; return INF; } int kruskal_(int n, int m, int del) { int ans = 0, temp = 0; init(n); for(int i=0 ; i<m ; i++) { if(i == del) continue; int p = Find(edge[i].x), q = Find(edge[i].y); if(p!=q) { pre[p] = q; ans += edge[i].v; temp++; } } if(temp+1 == n) return ans; return INF; } int main() { #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); #endif int t,n,m,cas{}; scanf("%d",&t); while(t--) { printf("Case #%d : ",++cas); scanf("%d%d",&n,&m); for(int i=0 ; i<m ; i++) scanf("%d%d%d",&edge[i].x,&edge[i].y,&edge[i].v); if(kruskal(n,m)==INF) { printf("No way\n"); continue; } result = INF; for(int i=0 ; i<cnt ; i++) result = min(result, kruskal_(n,m,used[i])); if(result == INF) printf("No second way\n"); else printf("%d\n",result); } return 0; }
相关文章推荐
- 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)
- 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,次小生成树)
- Uva 10462 Is There A Second Way Left? (次小生成树)
- 【kruskal 求次小生成树存在】Is There A Second Way Left? (UVA 10462)
- 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算法/Prim算法)
- 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?
- D - Is There A Second Way Left? (次小生成树)