Conscription
Windy has a country, and he wants to build an army to protect his country. He has picked up N 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 dand 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 and R.
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
For each test case output the answer in a single line.
Sample Input
2
5 5 8
4 3 6831
1 3 4583
0 0 6592
0 1 3063
3 3 4975
1 3 2049
4 2 2104
2 2 781
5 5 10
2 4 9820
3 2 6236
3 1 8864
2 4 8326
2 0 5156
2 0 1463
4 1 2439
0 4 4373
3 4 8889
2 4 3133
Sample Output
71071
54223
Source
POJ Monthly Contest – 2009.04.05, windy7926778
题目类型:MST
算法分析:看上去需要使用二分图来求,事实上直接将每个边权取负,求解一个MST即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
#pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7FFFFFFF; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 5e5 + 66; struct node{ int u, v, w;}; bool operator < (const node &a, const node &b) {return a.w < b.w;} node aa[maxn]; int parent[maxn], n1, n2, m, t; int UnFind (int val) { if (parent[val] == val) return val; else return parent[val] = UnFind (parent[val]); } int PP () { int re = n1 + n2, res = 0; for (int i = 1; i <= m && re > 1; i++) { if (UnFind (aa[i].u) != UnFind(aa[i].v)) { parent[UnFind(aa[i].u)] = UnFind (aa[i].v); res += aa[i].w; re--; } } return res; } int main() { // CFF; scanf ("%d", &t); while (t--) { for (int i = 0; i < maxn; i++) parent[i] = i; scanf ("%d %d %d", &n1, &n2, &m); for (int i = 1; i <= m; i++) { scanf ("%d %d %d", &aa[i].u, &aa[i].v, &aa[i].w); aa[i].v += n1; aa[i].w = -aa[i].w; } sort (aa + 1, aa + 1 + m); printf ("%d\n", 10000 * (n1 + n2) + PP ()); } return 0; } |