ACM Contest and Blackout
来源:互联网 发布:通联支付网络给我打钱 编辑:程序博客网 时间:2024/06/04 23:27
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 longusing namespace std;#define maxn 107const 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;}
阅读全文
0 0
- ACM Contest and Blackout
- ACM Contest and Blackout
- UVa10600 ACM Contest and Blackout
- UVA10600-ACM Contest and Blackout
- ACM Contest and Blackout UVA
- ACM Contest and Blackout UVA
- 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 (prim求次小生成树)
- 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 (次小生成树)
- uva10600 - ACM Contest and Blackout 次小生成树
- java中throw与throws的区别
- hdu 1728 逃离迷宫
- kafka多线程消费和手动提交offset,新版java api
- 抽象类、接口和内部类(上)
- ThinkPhp学习笔记-如何定义Template模板路径
- ACM Contest and Blackout
- C#相对路径转绝对路径,绝对路径转相对路径
- Codeforces Round #422 (Div. 2) C. Hacker, pack your bags!
- 抽象类、接口和内部类(下)
- POJ 3984 迷宫问题
- Java-Java反射
- HDU 2553 N皇后问题
- 51nod 1238 最小公倍数之和 V3
- MFC exe替换大、中、小图标方法