首页 > 代码库 > POJ 3723.Conscription 最小生成树

POJ 3723.Conscription 最小生成树

Conscription
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 13438 Accepted: 4699

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, NM and R.
Then R lines followed, each contains three integers xiyi and di.
There is a blank line before each test case.

1 ≤ NM ≤ 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

25 5 84 3 68311 3 45830 0 65920 1 30633 3 49751 3 20494 2 21042 2 7815 5 102 4 98203 2 62363 1 88642 4 83262 0 51562 0 14634 1 24390 4 43733 4 88892 4 3133

Sample Output

7107154223
题目链接:http://poj.org/problem?id=3723
题意:招收n个女的和m个男的,每招收一个人付出10000元,现在他(她)们之间有关系,如果x,y之间的关系为d,当招收了x后在招收y,
则y的招收费用为10000-d;同理,当招收了y之后再招收x,则x的招收费用为10000-d。因为招收顺序不同,招收总费用也会不同,求出招收费用最低。
思路:关系之间建立一条无向边,利用某些关系招收会形成一个生成树,当要招收总费用最低,那么需要生成树的边权值最大。如果关系图的权值取负数,
就是求最小生成树。
代码:
技术分享
#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>#include<map>#include<queue>#include<stack>#include<vector>#include<set>using namespace std;typedef pair<int,int> P;typedef long long ll;const int maxn=1e5+100,inf=0x3f3f3f3f,mod=1e9+7;const ll INF=1e13+7;struct edge{    int from,to;    int cost;};edge es[maxn];vector<edge>G[maxn];int used[maxn];priority_queue<P,vector<P>,greater<P> >que;void addedge(int i,int u,int v,int w){    es[i].from=u,es[i].to=v,es[i].cost=w;    G[u].push_back(es[i]);}int prim(int s){    int ans=0;    que.push(P(0,s));    while(!que.empty())    {        P p=que.top();        que.pop();        int u=p.second,d=p.first;        if(used[u]) continue;        used[u]=1;        ans+=d;        for(int i=0; i<G[u].size(); i++)        {            edge e=G[u][i];            if(used[e.to]) continue;            que.push(P(e.cost,e.to));        }    }    return ans;}int main(){    int t;    int n,m,r;    scanf("%d",&t);    while(t--)    {        scanf("%d%d%d",&n,&m,&r);        for(int i=0;i<n+m;i++)        {            G[i].clear();            used[i]=0;        }        for(int i=1; i<=r; i++)        {            int u,v,w;            scanf("%d%d%d",&u,&v,&w);            addedge(i,u,v+n,-w);            addedge(i,v+n,u,-w);        }        int ans=0;        for(int i=0; i<n+m; i++)            if(!used[i]) ans+=prim(i);        printf("%d\n",(n+m)*10000+ans);    }    return 0;}
最小生成树prim算法

 

POJ 3723.Conscription 最小生成树