ZOJ 1586
2016-01-25 21:33
225 查看
题目链接:[kuangbin带你飞]专题六 最小生成树 E - QS Network
//#include"stdafx.h" #include <iostream> #include <algorithm> using namespace std; struct edge{ int s, e, w; }; edge e[200009]; int p[10009], cost[10009]; bool cmp(edge a, edge b){ return a.w < b.w; } int find(int x){ return p[x] == x ? x : p[x] = find(p[x]); } void kruscal(int n, int cnt) { int ans = 0; int flag = 0; for (int i = 0; i <= n; i++) p[i] = i; sort(e, e + cnt, cmp); for (int i = 0; i < cnt; i++) { int x = find(e[i].s); int y = find(e[i].e); if (x != y) { p[x] = y; ans += e[i].w; flag++; } if (flag == n-1) break; } cout << ans << endl; } int main() { int m; cin >> m; while (m--) { int n, w, cnt = 0; cin >> n; for (int i = 0; i < n; i++) cin >> cost[i]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { cin >> w; if (i < j) { e[cnt].s = i, e[cnt].e = j, e[cnt++].w = w + cost[i] + cost[j]; } } kruscal(n, cnt); } return 0; }
相关文章推荐
- zoj3549 快速幂
- ZOJ 1002
- ZOJ-3861 DFS+回溯
- ZOJ3279 —— 8G island
- zoj_1004-Anagrams by stack
- zoj_2278-Fight for Food
- ZOJ 1001 A+B Problem
- zoj 2710 Two Pipelines
- zoj 386 4000 1 Valid Pattern Lock
- ZOJ 3755 Mines 回溯+剪枝
- ZOJ3750 Dot Dot Dot 枚举状态+BFS
- poj 1514&zoj 1185 Metal Cutting(半平面交)
- ZOJ1101-Gamblers 终于AC。。。
- zoj 1148 The Game 一个晚上终于AC!
- zoj 3420 纯bfs
- PAT 01-复杂度2. Maximum Subsequence Sum (25)&&PAT 01-复杂度1. 最大子列和问题(20)
- 狗狗40题~ (Volume A)
- 狗狗40题~ (Volume B)
- 狗狗40题~ (Volume C)
- ZOJ3319 DP 通过入度出度判非法情况