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



/* ***********************************************
Author        :CKboss
Created Time  :2015年08月24日 星期一 13时12分25秒
File Name     :E.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;

const int maxn=220;
const int INF=0x3f3f3f3f;

struct Edge
{
	int to,next,cap,flow,cost;
}edge[maxn*maxn];

int Adj[maxn],Size,N;

void init()
{
	memset(Adj,-1,sizeof(Adj)); Size=0;
}

void addedge(int u,int v,int cap,int cost)
{
	edge[Size].to=v;
	edge[Size].next=Adj[u];
	edge[Size].cost=cost;
	edge[Size].cap=cap;
	edge[Size].flow=0;
	Adj[u]=Size++;
}

void Add_Edge(int u,int v,int cap,int cost)
{
	addedge(u,v,cap,cost);
	addedge(v,u,0,-cost);
}

int dist[maxn],vis[maxn],pre[maxn];

bool spfa(int s,int t)
{
	queue<int> q;
	for(int i=0;i<N;i++)
	{
		dist[i]=INF; vis[i]=false; pre[i]=-1;
	}
	dist[s]=0; vis[s]=true; q.push(s);
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		vis[u]=false;
		for(int i=Adj[u];~i;i=edge[i].next)
		{
			int v=edge[i].to;
			if(edge[i].cap>edge[i].flow&&
					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);
				}
			}
		}
	}
	if(pre[t]==-1) return false;
	return true;
}

int MinCostMaxFlow(int s,int t,int& cost)
{
	int flow=0;
	cost=0;
	while(spfa(s,t))
	{
		int Min=INF;
		for(int i=pre[t];~i;i=pre[edge[i^1].to])
		{
			if(Min>edge[i].cap-edge[i].flow)
			{
				Min=edge[i].cap-edge[i].flow;
			}
		}
		for(int i=pre[t];~i;i=pre[edge[i^1].to])
		{
			edge[i].flow+=Min;
			edge[i^1].flow-=Min;
			cost+=edge[i].cost*Min;
		}
		flow+=Min;
	}
	return flow;
}

int n,m;
int A[maxn],B[maxn];

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

	while(scanf("%d%d",&n,&m)!=EOF)
	{
		int suma=0,sumb=0;
		for(int i=1;i<=n;i++)
		{
			scanf("%d%d",A+i,B+i);
			suma+=A[i]; sumb+=B[i];
		}
		init();
		for(int i=0;i<m;i++)
		{
			int u,v;
			scanf("%d%d",&u,&v);
			/// u--->v
			Add_Edge(u,v,INF,1);
			Add_Edge(v,u,INF,1);
		}

		if(suma!=sumb)
		{
			puts("-1"); continue;
		}

		int S=0,T=n+1;
		for(int i=1;i<=n;i++)
		{
			Add_Edge(S,i,A[i],0);
			Add_Edge(i,T,B[i],0);
		}

		N=n+2;
		int flow,cost;
		flow=MinCostMaxFlow(S,T,cost);

		//cout<<"flow "<<flow<<"cost "<<cost<<endl;
		if(flow==suma)
		{
			printf("%d\n",cost);
		}
		else
		{
			puts("-1"); continue;
		}
	}
    
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值