POJ-3723 Conscription

来源:互联网 发布:java char 编辑:程序博客网 时间:2024/05/20 05:06

题目传送门
Conscription
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 14389 Accepted: 5013
Description

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
Source

POJ Monthly Contest – 2009.04.05, windy7926778

题意:
需要征募男兵N人,女兵M人。每征募一个人需要花费10000美元。但是如果已经征募的人中有一些关系亲密的人,那么可以少花一点钱。题目中给你R个男女之间的亲密度关系,如果n号男和m号女有亲密度关系,那么只要现在招募到他们中的一个人,那么招募另外一个人的花费将变为10000-他们之间的亲密度。然后要求你求出招募到所有人的最小花费。

因为全部人不一定都连接在一起,也就是全部人连接起来不一定是一棵树,可能是森林,像下面的图一样。

这里写图片描述
所以题目就是求这个森林的最大权,用最小生成树kruskal算法可求,最小值怎么变成最大值?正权边取反变成负权边,用负权边求最小值,再加绝对值不就是最大值吗?所以建图很明确了。
代码:

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define MEM(a,x) memset(a,x,sizeof(a))const int maxn=4*1e4+10;struct Edge{    int from,to,len;    bool operator<(const Edge &a)const    {        return a.len>len;    }}edge[10*maxn];int pre[maxn],Rank[maxn];int n,m,r,tot,cnt; void init(){    tot=cnt=0;    MEM(Rank,0);    for(int i=1;i<=n+m;i++)    pre[i]=i;}void Add(int from,int to,int len){    edge[tot].from=from;    edge[tot].to=to;    edge[tot++].len=len;    edge[tot].from=to;    edge[tot].to=from;    edge[tot++].len=len;}int Find(int x){    if(x!=pre[x])    pre[x]=Find(pre[x]);    return pre[x];} void unite(int x,int y){    int fx=Find(x);    int fy=Find(y);    if(Rank[fx]>Rank[fy])    pre[fy]=fx;    else    {        pre[fx]=fy;        if(Rank[fx]==Rank[fy])        Rank[fy]++;    }}int kruskal(){    int Mintree=0;    sort(edge,edge+tot);    for(int i=0;i<tot;i++)    {        Edge e=edge[i];        int u=e.from;        int v=e.to;        if(Find(u)!=Find(v))        {            unite(u,v);            Mintree+=e.len;        }    }    return Mintree;}int main(){    int T;    scanf("%d",&T);    while(T--)    {        scanf("%d%d%d",&n,&m,&r);        init();        for(int i=0;i<r;i++)        {            int u,v,len;            scanf("%d%d%d",&u,&v,&len);            u++;            v++;            Add(u,n+v,-len);        }        printf("%d\n",10000*(n+m)+kruskal());    }} 
原创粉丝点击