zoj 月赛 E 费用流

The Exchange of Items

Time Limit: 2 Seconds      Memory Limit: 65536 KB

Bob lives in an ancient village, where transactions are done by one item exchange with another. Bob is very clever and he knows what items will become more valuable later on. So, Bob has decided to do some business with villagers.

At first, Bob has N kinds of items indexed from 1 to N, and each item has Ai. There are M ways to exchanges items. For the ith way (XiYi), Bob can exchange one Xith item to one Yith item, vice versa. Now Bob wants that his ith item has exactly Bi, and he wonders what the minimal times of transactions is.

Input

There are multiple test cases. 
For each test case: the first line contains two integers: N and M (1 <= NM <= 100).
The next N lines contains two integers: Ai and Bi (1 <= AiBi <= 10,000).
Following M lines contains two integers: Xi and Yi (1 <= XiYi <= N).
There is one empty line between test cases.

Output

For each test case output the minimal times of transactions. If Bob could not reach his goal, output -1 instead.

Sample Input
2 1
1 2
2 1
1 2

4 2
1 3
2 1
3 2
2 3
1 2
3 4
Sample Output
1
-1

Author: FENG, Jingyi
Source: ZOJ Monthly, July 2017

#include <bits/stdc++.h>

using namespace std;
#define MOD 0x3f3f3f3f
#define V 1000 + 10
#define E 10000 + 10
int n, m, flow_sum;// 点数 边数 总流量
bool vis[V];
int cnt, dist[V], head[V], pre[V];
struct Edge
{
    int u, v, c, cost, next;//This 'c' means capacity
} edge[E<<2];
void init()
{
    cnt = flow_sum = 0;
    memset(head, -1, (n+3) * sizeof(int));
}
void adde(int u, int v, int c, int cost)
{
    edge[cnt].u = u;
    edge[cnt].v = v;
    edge[cnt].c = c;
    edge[cnt].cost = cost;
    edge[cnt].next = head[u];
    head[u] = cnt++;
    edge[cnt].u = v;
    edge[cnt].v = u;
    edge[cnt].c = 0;
    edge[cnt].cost = -cost;
    edge[cnt].next = head[v];
    head[v] = cnt++;
}
bool spfa(int begin, int end)
{
    int u, v;
    queue<int> q;
    memset(pre, -1, (n+3)*sizeof(int));
    memset(vis, 0, (n+3)*sizeof(bool));
    memset(dist, 0x3f, (n+3)*sizeof(int));
    vis[begin] = 1;
    dist[begin] = 0;
    q.push(begin);
    while(!q.empty())
    {
        u = q.front();
        q.pop();
        vis[u] = false;
        for(int i=head[u]; i!=-1; i=edge[i].next)
            if(edge[i].c > 0)
            {
                v = edge[i].v;
                if(dist[v] > dist[u] + edge[i].cost)
                {
                    dist[v] = dist[u] + edge[i].cost;
                    pre[v] = i;
                    if(!vis[v])
                    {
                        vis[v] = true;
                        q.push(v);
                    }
                }
            }
    }
    return dist[end] != MOD;
}

int sum;

int MCMF(int begin, int end)
{
    int ans = 0, flow;
    while(spfa(begin, end))
    {
        flow = MOD;
        for(int i=pre[end]; i!=-1; i=pre[edge[i].u])
            flow = min(flow, edge[i].c);
        for(int i=pre[end]; i!=-1; i=pre[edge[i].u])
        {
            edge[i].c -= flow;
            edge[i^1].c += flow;
        }
        ans += dist[end] * flow;
        flow_sum += flow;
    }
    return ans;//返回最小费用
}

int main()
{
    while(~scanf("%d%d", &n, &m))
    {
        init();
        int sum1 = 0, sum2 = 0;
        sum = 0;
        int a, b;
        for(int i = 1; i <= n; i++)
        {
            scanf("%d%d", &a, &b);
            sum1 += a;
            sum2 += b;
            if(a > b) {
                adde(0, i, a - b, 0);
            }
            else if(a < b) {
                adde(i, n + 1, b - a, 0);
                sum += b - a;
            }
        }
        for(int i=0; i<m; i++)
        {
            scanf("%d%d", &a, &b);
            adde(a, b, MOD, 1);
            adde(b, a, MOD, 1);
        }
        if(sum1 != sum2)
        {
            printf("-1\n");
            continue;
        }
        int ans = MCMF(0, n + 1);
        //cout<<flow_sum<<endl;
        if(flow_sum  == sum) printf("%d\n", ans);
        else printf("-1\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值