题目
题解思路
没看清题目,被绕了一下,这里每个人的雇佣费用都是固定的,我们进行要求出能优惠的最大值,根据题目的权值,可以生成一个最大生成树,求出优惠的最大值。
一开始写着写着写成贪心了,忽略了一个人有多个关系的情况,这里用树形的结构就能很好处理了。
AC代码
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
struct node
{
int x,y,w;
}mp[50100];
bool cmp ( node A , node B)
{
return A.w > B.w;
}
long long n,m,r;
long long ans = 0;
int a[20100];
int find2(int x)
{
if ( x != a[x] )
{
a[x] = find2(a[x]);
}
return a[x];
}
int u(int x,int y)
{
int fx = find2(x);
int fy = find2(y);
if ( fx != fy )
{
a[fx] = fy;
return 1;
}
return 0;
}
void ku()
{
ans = 0;
for (int i = 1 ;i <= r; i++ )
{
if (u(mp[i].x,mp[i].y))
{
ans += mp[i].w;
}
}
printf("%lld\n",10000*(n+m)-ans);
}
int main ()
{
ios::sync_with_stdio(false);
int t;
scanf("%d",&t);
while(t--)
{
scanf("%lld%lld%lld",&n,&m,&r);
for (int i = 1 ; i <= n+m ; i++ )
a[i] = i;
for (int i = 1 ; i <= r ; i++ )
{
int t1,t2,t3;
scanf("%d%d%d",&t1,&t2,&t3);
mp[i].x = t1+1;
mp[i].y = t2+n+1;
mp[i].w = t3;
}
sort(mp+1,mp+1+r,cmp);
ku();
}
return 0;
}