ACM Contest and Blackout - UVA 10600 - 次小生成树
2017-07-03 12:32
459 查看
ACM Contest and Blackout - UVA 10600 - 次小生成树
题意
n个点,m条边,求最小生成树的值和次小生成树的值。思路
无脑写次小生成树就OK,因为比较简单我就尝试着用Kruskal写了一下,本以为耗时会很长的,结果是0ms,感觉可能是数据水的原因。代码
// // 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 T,n,m,ans,ans2,pre[maxn],fa[maxn]; struct node { int a, b, c; bool operator < (const node& tmp) const { return c < tmp.c; } }a[maxn*maxn]; vector<node> used; int find(int x, int pre[]) { return pre[x] == x ? x : pre[x] = find(pre[x], pre); } void solve() { sort(a, a+m); int cnt = 1; ans = 0; used.clear(); for(int i=1;i<=n;i++) pre[i] = i; for(int i=0;i<m;i++) { int x = find(a[i].a, pre), y = find(a[i].b, pre); if(x != y) { pre[x] = y; used.push_back(a[i]); ++cnt; ans += a[i].c; } if(cnt == n) break; } ans2 = INF; for(int i=0;i<used.size();i++) { node v = used[i]; int cur = 0, cnt = 1; for(int j=1;j<=n;j++) fa[j] = j; for(int j=0;j<m;j++) { if(v.a == a[j].a && v.b == a[j].b && v.c == a[j].c) continue; int x = find(a[j].a, fa), y = find(a[j].b, fa); if(x != y) { fa[x] = y; ++cnt; cur += a[j].c; } if(cnt == n) break; } if(cnt == n) ans2 = min(ans2, cur); } } int main() { #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); #endif scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); for(int i=0;i<m;i++) scanf("%d%d%d",&a[i].a,&a[i].b,&a[i].c); solve(); printf("%d %d\n",ans,ans2); } return 0; }
相关文章推荐
- UVA 10600 - ACM Contest and Blackout 次小生成树
- UVA 10600 - ACM Contest and Blackout 次小生成树
- UVA 10600 ACM Contest and Blackout(最小生成树and次小生成树)
- UVa 10600 - ACM Contest and Blackout(最小生成树)
- uva10600 - ACM Contest and Blackout 次小生成树
- 【UVA 10600】 ACM Contest and Blackout(最小生成树和次小生成树)
- UVA 10600 ACM Contest and Blackout (次小生成树 Kruskal 封装)
- UVA 10600 - ACM Contest and Blackout(最小生成树&次小生成树)
- (beginer) 最小生成树 UVA 10600 ACM Contest and Blackout
- 【UVa】10600 ACM Contest and Blackout 次小生成树
- UVA 10600 ACM contest and Blackout(次小生成树)
- UVA 10600 ACM Contest and Blackout 次小生成树/裸
- UVA 10600 ACM Contest and Blackout 次小生成树/裸
- UVa 10600 ACM contest and Blackout( 次小生成树)
- UVa 10600 ACM contest and Blackout( 次小生成树)
- UVA 10600 ACM Contest and Blackout (次小生成树)
- UVA 10600 ACM Contest and Blackout
- UVa 10600 - ACM Contest and Blackout
- UVA 10600 ACM Contest and Blackout (次小生成树)
- UVa:10600 ACM Contest and Blackout