zoj 3885 The Exchange of Items 【最小费用最大流】

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 oneYith 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



题意:Bob有N种物品,已知每种物品有Ai个,现在他想换成Bi个。给出有M种交换方式<Xi,Yi>(表示Xi物品和Yi物品可以互相转换)。问你——Bob达成目标所需要的最小交换次数,若不可能达成目标输出-1。


思路:最小费用最大流。记录目标状态的总流量sum,判断流入汇点的总流量是否等于sum。若等于说明目标状态可达,输出最小费用,反之不可达,输出-1。


建图:设置超级源点source,超级汇点sink。

1,source向所有物品建边,容量为物品的初始数目,费用为0;

2,所有物品向sink建边,容量为物品的目标数目,费用为0;

3,M种转换关系建双向边,容量为INF(为了能够达到目标状态,我们可以任意选择该边的流量),费用为1。

再跑一遍最小费用最大流,若最后的最大流flow等于目标状态的总流量则输出最小费用cost,反之输出-1。



AC代码:

#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#define MAXN 110
#define MAXM 1000
#define INF 0x3f3f3f3f
using namespace std;
struct Edge
{
    int from, to, cap, flow, cost, next;
};
Edge edge[MAXM];
int head[MAXN], edgenum;
int pre[MAXN], dist[MAXN];
bool vis[MAXN];
int source, sink;//超级源点 超级汇点
int N, M;
int sum;//记录目标状态总流量
void init()
{
    edgenum = 0;
    memset(head, -1, sizeof(head));
}
void addEdge(int u, int v, int w, int c)
{
    Edge E1 = {u, v, w, 0, c, head[u]};
    edge[edgenum] = E1;
    head[u] = edgenum++;
    Edge E2 = {v, u, 0, 0, -c, head[v]};
    edge[edgenum] = E2;
    head[v] = edgenum++;
}
void getMap()
{
    int a, b;
    sum = 0;
    source = 0, sink = N+1;
    for(int i = 1; i <= N; i++)
    {
        scanf("%d%d", &a, &b);
        addEdge(source, i, a, 0);//超级源点连i 容量为a 费用为0
        addEdge(i, sink, b, 0);//i连超级汇点 容量为b 费用为0
        sum += b;//记录目标状态总流量
    }
    for(int i = 1; i <= M; i++)
        scanf("%d%d", &a, &b),
        addEdge(a, b, INF, 1), addEdge(b, a, INF, 1);//互建边 容量无穷大 费用1
}
bool SPFA(int s, int t)
{
    queue<int> Q;
    memset(dist, INF, sizeof(dist));
    memset(vis, false, sizeof(vis));
    memset(pre, -1, sizeof(pre));
    dist[s] = 0;
    vis[s] = true;
    Q.push(s);
    while(!Q.empty())
    {
        int u = Q.front();
        Q.pop();
        vis[u] = false;
        for(int i = head[u]; i != -1; i = edge[i].next)
        {
            Edge E = edge[i];
            if(dist[E.to] > dist[u] + E.cost && E.cap > E.flow)
            {
                dist[E.to] = dist[u] + E.cost;
                pre[E.to] = i;
                if(!vis[E.to])
                {
                    vis[E.to] = true;
                    Q.push(E.to);
                }
            }
        }
    }
    return pre[t] != -1;
}
void MCMF(int s, int t, int &cost, int &flow)
{
    cost = flow = 0;
    while(SPFA(s, t))
    {
        int Min = INF;
        for(int i = pre[t]; i != -1; i = pre[edge[i^1].to])
        {
            Edge E = edge[i];
            Min = min(Min, E.cap - E.flow);
        }
        for(int i = pre[t]; i != -1; i = pre[edge[i^1].to])
        {
            edge[i].flow += Min;
            edge[i^1].flow -= Min;
            cost += edge[i].cost * Min;
        }
        flow += Min;
    }
}
int main()
{
    while(scanf("%d%d", &N, &M) != EOF)
    {
        init();
        getMap();
        int cost, flow;
        MCMF(source, sink, cost, flow);
        if(flow == sum)//若流入汇点的总流量 等于 目标状态总流量 说明可行
            printf("%d\n", cost);
        else
            printf("-1\n");
    }
    return 0;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值