#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <queue>
using namespace std;
typedef long long LL;
typedef pair<int , int> P;
#define maxn 10000+10
int rank[maxn<<1];
int p[maxn<<1];
int n, m, R;
struct edge
{
int u;
int v;
int w;
edge(){}
edge(int u_, int v_, int w_)
{
u = u_;
v = v_;
w = w_;
}
}e[5 * maxn];
int cmp(edge a, edge b)
{
return a.w < b.w;
}
void init()
{
for(int i=0; i< n + m; i++) p[i] = i;
memset(rank, 0, sizeof(rank));
}
int find(int x)
{
return p[x] == x ? x : p[x] = find(p[x]);
}
void unionset(int x, int y)
{
if(rank[x] > rank[y]) p[y] = x;
else
{
p[x] = y;
if(rank[x] == rank[y]) rank[y]++;
}
}
int kruskal()
{
int ans = 0;
sort(e, e+R, cmp);
init();
for(int i=0; i<R; i++)
{
int x = find(e[i].u);
int y = find(e[i].v);
if(x != y)
{
ans += e[i].w;
unionset(x, y);
}
}
return ans;
}
int main()
{
int T;
cin>>T;
while(T--)
{
scanf("%d%d%d", &n, &m, &R);
int a, b, c;
for(int i=0; i<R; i++)
{
cin>>a>>b>>c;
e[i] = edge(a, n + b, -c);
}
int ans = 10000 * (n + m) + kruskal();
printf("%d\n", ans);
}
return 0;
}
poj 3723
最新推荐文章于 2021-07-04 20:24:40 发布