SPOJ SCITIES - Selfish Cities(费用流)

SCITIES - Selfish Cities

#combinatorics #weighted-matching

 

Far, far away there is a world known as Selfishland because of the nature of its inhabitants. Hard times have forced the cities of Selfishland to exchange goods among each other. C1 cities are willing to sell some goods and the other C2 cities are willing to buy some goods (each city can either sell or buy goods, but not both). There would be no problem if not for the selfishness of the cities. Each selling city will sell its goods to one city only, and each buying city will buy goods from one city only. 

Your goal is to connect the selfish cities in such a way that the amount of exchanged goods is maximalized.

Input

The first line contains a positive integer t<=1000 indicating the number of test cases. Each test case is an instance of the problem defined above. The first line of each test case is a pair of positive integers C1 and C2 (the number of cities wanting to sell their goods C1<=100 and the number of cities wanting to buy goods C2<=100). The lines that follow contain a sequence of (c1,c2,g) trios ending with three zeros. (c1,c2,g) means that the city c1 can offer the city c2 the amount of g<=100 goods.

Output

For each test case print the maximal amount of goods exchanged.

Example

Input:
3
3 2
1 1 10
2 1 19
2 2 11
3 2 1
0 0 0
4 4
1 1 6
1 2 6
2 1 8
2 3 9
2 4 8
3 2 8
4 3 7
0 0 0
3 2
1 1 10
2 1 21
2 2 11
3 2 1
0 0 0

Output:
21
29
22

 Submit solution!

题目大意:有A个城市卖东西,B个城市买东西,一个城市只能卖东西给一个城市,一个城市只能从一个城市买东西,问最多有多少货物被交易

源点向那些卖东西的点连上一条容量为1,费用为0的边,表示只能交易一次,所有只能买东西的点向汇点连上一条容量为1,费用为0的边,对于有交易关系的那些城市,就连一条容量为1,费用为给定的边权取负,最后跑mcmf,将答案取负数即可

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxn=510;
const int maxm=100010;
const int inf=0x3f3f3f3f;
struct Node
{
    int to;
    int capa;
    int cost;
    int next;
}edge[maxm];
int cnt;
int source,sink;
int head[maxn];
int num[maxn];
int dis[maxn];
int rec[maxn];
int pre[maxn];
bool vis[maxn];
void init()
{
    memset(head,-1,sizeof(head));
    memset(num,0,sizeof(num));
    cnt=0;
}
void add(int u,int v,int capa,int cost)
{
    edge[cnt].to=v;
    edge[cnt].capa=capa;
    edge[cnt].cost=cost;
    edge[cnt].next=head[u];
    head[u]=cnt++;
    edge[cnt].to=u;
    edge[cnt].capa=0;
    edge[cnt].cost=-cost;
    edge[cnt].next=head[v];
    head[v]=cnt++;
    return;
}
bool spfa()
{
    memset(dis,inf,sizeof(dis));
    memset(vis,false,sizeof(vis));
    memset(rec,-1,sizeof(rec));
    memset(pre,-1,sizeof(pre));
    queue<int> que;
    que.push(source);
    dis[source]=0;
    vis[source]=true;
    while(!que.empty())
    {
        int node=que.front();
        que.pop();
        vis[node]=false;
        for(int i=head[node];~i;i=edge[i].next)
        {
            int v=edge[i].to;
            if(edge[i].capa>0&&dis[v]>dis[node]+edge[i].cost)
            {
                dis[v]=dis[node]+edge[i].cost;
                rec[v]=i;
                pre[v]=node;
                if(!vis[v])
                {
                    vis[v]=true;
                    que.push(v);
                }
            }
        }
    }
    return dis[sink]!=inf;
}
int mcmf()
{
    int maxflow=0;
    int mincost=0;
    while(spfa())
    {
        int flow=inf;
        int node=sink;
        while(node!=source)
        {
            flow=min(flow,edge[rec[node]].capa);
            node=pre[node];
        }
        maxflow+=flow;
        node=sink;
        while(node!=source)
        {
            mincost+=flow*edge[rec[node]].cost;
            edge[rec[node]].capa-=flow;
            edge[rec[node]^1].capa+=flow;
            node=pre[node];
        }
    }
    return mincost;
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int test;
    scanf("%d",&test);
    while(test--)
    {
        init();
        int n,m;
        scanf("%d%d",&n,&m);
        int u,v,w;
        source=0;
        sink=210;
        for(int i=1;i<=n;i++)
        {
            add(source,i,1,0);
        }
        while(~scanf("%d%d%d",&u,&v,&w))
        {
            if(!u&&!v&&!w) break;
            add(u,v+n,1,-w);
        }
        for(int i=1;i<=m;i++)
        {
            add(n+i,sink,1,0);
        }
        printf("%d\n",-mcmf());
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值