UVa 11733 - Airports
来源:互联网 发布:拼模特卡软件 编辑:程序博客网 时间:2024/05/01 14:54
題目:在城市之間建立公路和飛機場,是的每個城市都能到飛機場,求最小花費。
分析:圖論,最小生成樹。kruskal算法,每次選擇加一條變便或者建一個飛機場。
如果邊長不小於飛機場的花費選擇建立新的飛機場;
說明:花費一樣時,選擇最所的飛機場數量。
#include <algorithm>#include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>using namespace std;typedef struct _enode{int point1;int point2;int weight;}enode;enode edge[100001];//union_setint union_sets[10001];int union_rank[10001];void union_inital(int a, int b){for (int i = a; i <= b; ++ i) {union_rank[i] = 0;union_sets[i] = i;}}int union_find(int a){if (a != union_sets[a]) {union_sets[a] = union_find(union_sets[a]);}return union_sets[a];}void union_union(int a, int b){if (union_rank[a] < union_rank[b]) {union_sets[a] = b;}else {if (union_rank[a] == union_rank[b]) {union_rank[a] ++;}union_sets[b] = a;}}//end_union_setint cmp_e(enode a, enode b){return a.weight < b.weight;}int kruskal(int n, int m, int cost){sort(edge, edge+m, cmp_e);union_inital(1, n);int sum = 0;for (int i = 0; i < m; ++ i) {if (edge[i].weight >= cost) {continue;}int A = union_find(edge[i].point1);int B = union_find(edge[i].point2);if (A != B) {union_union(A, B);sum += edge[i].weight;}}int count = 0;for (int i = 1; i <= n; ++ i) {if (i == union_sets[i]) {count ++;}}printf("%d %d\n",sum+cost*count,count);}int main(){int T, n, m, a, b, c, cost;while (~scanf("%d",&T)) {for (int t = 1; t <= T; ++ t) {scanf("%d%d%d",&n,&m,&cost);for (int i = 0; i < m; ++ i) {scanf("%d%d%d",&a,&b,&c);edge[i].point1 = a;edge[i].point2 = b;edge[i].weight = c;}printf("Case #%d: ",t);kruskal(n, m, cost);}}return 0;}
0 0
- Uva 11733 - Airports
- UVa 11733 Airports( MST )
- UVa 11733 - Airports
- UVA 11733 Airports
- Uva 11733 - Airports 最小生成森林..Kruskal
- UVa 11733 Airports(变形的最小生成树)
- Airports
- uva11733 Airports
- UVAlive 7368 Airports(建图+最小路径覆盖)
- codeforces 100959B Airports 曼哈顿距离最大生成树
- uva
- UVA
- UVA
- UVA
- uva
- UVA
- UVA
- UVA
- Exactly one month ago
- Swift_枚举
- SSH基于spring的控制反转理解
- 定时器
- iOS巅峰之实现iOS应用图标右上角数字badge number提醒
- UVa 11733 - Airports
- 链表
- [C#]合并单元格(行、列)
- 虐人之红黑树,B树系列
- Java_Ant详解
- 菜鸟都能理解的Android中View的事件分发机制及滑动冲突处理
- 浅析多线程同步
- SpannableStringBuilder的用法
- 深度剖析:如何实现一个 Virtual DOM 算法