POJ 3723 Conscription
来源:互联网 发布:微信营销软件 编辑:程序博客网 时间:2024/05/17 03:15
Description
Windy has a country, and he wants to build an army to protect his country. He has picked upN girls and M boys and wants to collect them to be his soldiers. To collect a soldier without any privilege, he must pay 10000 RMB. There are some relationships between girls and boys and Windy can use these relationships to reduce his cost. If girl x and boy y have a relationship d and one of them has been collected, Windy can collect the other one with 10000-d RMB. Now given all the relationships between girls and boys, your assignment is to find the least amount of money Windy has to pay. Notice that only one relationship can be used when collecting one soldier.
Input
The first line of input is the number of test case.
The first line of each test case contains three integers, N, M andR.
Then R lines followed, each contains three integers xi,yi and di.
There is a blank line before each test case.
1 ≤ N, M ≤ 10000
0 ≤ R ≤ 50,000
0 ≤ xi < N
0 ≤ yi < M
0 < di < 10000
Output
Sample Input
25 5 84 3 68311 3 45830 0 65920 1 30633 3 49751 3 20494 2 21042 2 7815 5 102 4 98203 2 62363 1 88642 4 83262 0 51562 0 14634 1 24390 4 43733 4 88892 4 3133
Sample Output
7107154223
Kruskal + 并查集求最大生成树
#include <stdio.h>#include <stdlib.h>int N, M, R;int group[20005];struct node{int u;int v;int w;} map[50005];int cmp(const void *a, const void *b){return (*(node*)b).w - (*(node*)a).w;}int find(int x){if(group[x] == x)return x;elsereturn group[x] = find(group[x]);}bool unite(int i, int j){i = find(i);j = find(j);if(i != j){group[i] = j;return false;}return true;}int main(){int T, A, B, C, i, sum;scanf("%d", &T);while(T--){scanf("%d%d%d", &N, &M, &R);for(i = 1; i <= R; i++){scanf("%d%d%d", &A, &B, &C);map[i].u = A + 1;map[i].v = B + N + 1;map[i].w = C;}qsort(&map[1], R, sizeof(map[1]), cmp);for(i = 1; i <= N + M; i++)group[i] = i;sum = 10000 * (N + M);for(i = 1; i <= R; i++)if(!unite(map[i].u, map[i].v))sum -= map[i].w;printf("%d\n", sum);}return 0;}
- poj 3723 Conscription
- POJ 3723 Conscription
- POJ 3723 Conscription
- POJ-3723-Conscription
- POJ 3723 Conscription
- POJ 3723 Conscription MST
- poj 3723 Conscription
- poj 3723 Conscription
- POJ 3723 Conscription
- POJ 3723 Conscription
- poj 3723 Conscription
- POJ--3723 Conscription
- POJ 3723 Conscription
- POJ 3723 Conscription
- POJ 3723Conscription
- POJ 3723 Conscription
- Conscription POJ - 3723
- POJ 3723 Conscription
- 25.JAVA编程思想——标准Java违例
- java内部比较器、外部比较器实现
- 26.JAVA编程思想——创建自己的违例
- 27.JAVA编程思想——违例的限制
- 让EditText不能自动获取焦点
- POJ 3723 Conscription
- HDU 1032 The 3n + 1 problem(暴力)
- MySql 表管理常用的sql语句
- CString类
- 利用朴素贝叶斯算法实现垃圾邮件的过滤,并结合Adaboost改进该算法
- PHP引入文件的路径问题
- JAVA IO 专题
- NYOJ 117 求逆序数
- 剑指offer(七)之二进制中1的个数