【ZOJ - 3885】 The Exchange of Items 【费用流】

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 (Xi, Yi), 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 <= N, M <= 100).
The next N lines contains two integers: Ai and Bi (1 <= Ai, Bi <= 10,000).
Following M lines contains two integers: Xi and Yi (1 <= Xi, Yi <= 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

题意:n个项目,每个项目都有a[i]个人,有些项目之间的人员可以相互交换,问最后是否可以确切的让每个项目有b[i],如果有请输出最下的交换次数,交换一个人算一次。
分析
很明显的流问题,人员的调动,由原来的状态转到另一个状态,都是常用流来解决。又因为牵扯到次数,我们可以用费用流
建图如下(假如只有1->2)
这里写图片描述
除了蓝线的费用为1,其他的费用都是0 。中间的线的容量都是inf。
其实你会发现,没有必要拆点,容量都是INF,但是我觉得这样更清晰吧。(都可以的,不拆点也行)
代码

#include<bits/stdc++.h>
using namespace std;
#define  LL long long
#define fread() freopen("in.txt","r",stdin)
#define fwrite() freopen("out.txt","w",stdout)
#define CLOSE() ios_base::sync_with_stdio(false)

const int MAXN = (100+11)*5;
const int MAXM = 100000;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;

struct Edge {
    int from,to,next,cap,flow,cost;
}edge[MAXM];
int head[MAXN],top;
void init(){
    memset(head,-1,sizeof(head));
    top=0;
} 
void addedge(int a,int b,int w,int c){
    edge[top].from=a,edge[top].to=b,edge[top].cap=w,edge[top].flow=0,edge[top].cost=c,edge[top].next=head[a];
    head[a]=top++;
    edge[top].from=b,edge[top].to=a,edge[top].cap=0,edge[top].flow=0,edge[top].cost=-c,edge[top].next=head[b];
    head[b]=top++;
}
int n,m;
int S,T;
int sum;
int a[MAXN],b[MAXN];
void getmap(){
    sum=0;
    for(int i=1;i<=n;i++)
        scanf("%d%d",&a[i],&b[i]),sum+=b[i];
    S=0,T=2*n+1;
    for(int i=1;i<=n;i++){
        addedge(S,i,a[i],0);
        addedge(i,i+n,inf,0);
        addedge(i+n,T,b[i],0);
    }
    while(m--){
        int x,y;scanf("%d%d",&x,&y);
        addedge(x+n,y,inf,1);
        addedge(y+n,x,inf,1);
    }
}
bool vis[MAXN];int dis[MAXN];
int pre[MAXN];
bool spfa(int s,int e){
    queue<int>Q;
    memset(vis,0,sizeof(vis));
    memset(dis,0x3f,sizeof(dis));
    memset(pre,-1,sizeof(pre));
    dis[s]=0;vis[s]=1;Q.push(s);
    while(!Q.empty()){
        int now=Q.front();Q.pop();vis[now]=0;
        for(int i=head[now];i!=-1;i=edge[i].next){
            Edge  e=edge[i];
            if(e.cap>e.flow&&dis[e.to]>dis[now]+e.cost){
                dis[e.to]=dis[now]+e.cost;
                pre[e.to]=i;
                if(!vis[e.to]){
                    vis[e.to]=1;
                    Q.push(e.to);
                }
            }
        }
    }
    return pre[e]!=-1;
}
int MCMF(int s,int t,int &cost,int &flow){
    flow=cost=0;
    while(spfa(s,t)){
        int Min=inf;
        for(int i=pre[t];i!=-1;i=pre[edge[i^1].to]){
            Min=min(Min,edge[i].cap-edge[i].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(){
    CLOSE();
//  fread();
//  fwrite();
    while(scanf("%d%d",&n,&m)!=EOF){
        init();
        getmap();
        int flow,ans;
        MCMF(S,T,ans,flow);
        if(flow!=sum)puts("-1");
        else 
        printf("%d\n",ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值