POJ - 3723 Conscription (最小生成树,Kruskal)

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 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 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
问题链接: http://poj.org/problem?id=3723
问题简述: 每征一个兵要10000,,需要n个男兵m个女兵。如果两个男女兵之间有关系并且你已经征了男女兵中的一个,则征召价钱为(10000-关系),求最优的征召顺序来花费最少的价钱。(我TM刚开始以为所有的兵加起来大于n+m,于是觉得很难)
问题分析: 有n男,m女,则把第i个女的编号为i+n,从0到n+m,以(10000-关系)为权值用kruskal建立k课最小生成树,即可得答案为k*10000+所有树的边权合。(征召每课树第一个人没有任何关系可用,所以必须花费10000)(第一次我用选择排序O(n2)直接TLE,改成sort函数才A…)
AC通过的C++语言程序如下:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#include<bitset>
#include<utility>
#include<functional>
#include<iomanip>
#include<sstream>
#define ll long long
#define endl '\n'
#include<ctime>
using namespace std;

const int N=60000;

int parent[N];
struct road
{
    int a,b;
    int value;
}r[N];

int n,m,flag=0;
int Find(int x)
{
    if(parent[x]!=x)
        parent[x]=Find(parent[x]);
    return parent[x];
}

bool cmp(road x,road y)
{
    return x.value<y.value;
}

void join(int x,int y)
{
    int fx=Find(x);
    int fy=Find(y);
    if(fx!=fy)
    {
        parent[fx]=fy;
        flag=1;
    }
    return;
}
/*
void xsort(road&x,road&y)
{
    if(x.value>y.value)
    {
        road t=x;
        x=y;
        y=t;
    }
    return;
}
*/
int main()
{
    //ios::sync_with_stdio(false);
    int k;
    scanf("%d",&k);
    while(k--)
    {
        memset(parent,0,sizeof(parent));
        int u,v,m;
        scanf("%d%d%d",&u,&v,&m);
        n=u+v;
        for(int i=0;i<m;i++)
        {
            int val,c,d;
            scanf("%d%d%d",&c,&d,&val);
            r[i].a=c;
            r[i].b=d+u;
            r[i].value=10000-val;
        }
        sort(r,r+m,cmp);
        for(int i=0;i<n+1;i++)
            parent[i]=i;
        int sum=0;
        for(int i=0;i<m;i++)
        {
            flag=0;
            join(r[i].a,r[i].b);
            if(flag)
            {
                sum+=r[i].value;
            }
        }
        for(int i=0;i<n;i++)
            if(parent[i]==i)
                sum+=10000;
        printf("%d\n",sum);
    }
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44012745/article/details/89038286