(最小费用最大流)The Exchange of Items (141 - ZOJ Monthly, July 2015 - 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

题意:现在有N个物品,进行物物交换,告诉每个物品 i 的初始时的个数A[i]和最终想得到的个数B[i],M种交换方式,问为了达到目的最少的交换次数是多少。

分析:最小费用最大流。s到i建一条容量为a[i]费用为0的边,i到t建一条容量为b[i]费用为0的边,x[i]到y[i]、y[i]到x[i]各建一条容量无限大费用为1的边。判断最大流大小是否等于a[i]或b[i]的和,如果等于,求最小费用;否则输出-1。

教训:本来还将 i 拆开,之间建一条容量无限大费用为0的边,但这样破坏了点集内部分点之间的连通性(不能保证所有点连通)。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
#define forr(i,f_start,f_end) for(int i=f_start;i<f_end;++i)
#define mem(x,i) memset(x,i,sizeof(x))
const int maxn = 105 << 3;
const int maxm = maxn * 4;
typedef long long LL;
const int INF = 0xfffffff;
int input()
{
	int a;
	scanf("%d", &a);
	return a;
}
int Flow;
///-----------------最小费用流-----------------
struct Edge
{
	int to, next, cap, flow, cost;
}edge[maxm];
struct MCMF
{
	int head[maxn], tol;
	int pre[maxn], dis[maxn];
	bool vis[maxn];
	int N;
	void init(int n)
	{
		N = n;
		tol = 0;
		memset(head, -1, sizeof(head));
	}

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

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

	//返回的是最大流,cost存的是最小费用  
	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 != -1; 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 != -1; i = pre[edge[i ^ 1].to])
			{
				edge[i].flow += Min;
				edge[i ^ 1].flow -= Min;
				cost += edge[i].cost*Min;
			}
			flow += Min;
		}
		return flow;
	}
}mcmf;

///-----------------------------------------
int N, M;
int a[maxn], b[maxn];
int x[maxn], y[maxn];
void solve()
{
	int tmp1 = 0, tmp2 = 0;
	forr(i, 0, N){
		tmp1 += a[i];
		tmp2 += b[i];
	}
	if (tmp1 != tmp2){
		printf("-1\n");
		return;
	}

	int s = 0, t = N + 1;
	mcmf.init(N + 2);
	int all = 0;
	for (int i = 0; i < N; i++){
		if (a[i] > b[i])	mcmf.addedge(s, i+1, a[i] - b[i], 0);
		else if (a[i] < b[i])	mcmf.addedge(i + 1, t, b[i] - a[i], 0), all += b[i] - a[i];
	}
	for (int i = 0; i < M; i++){
		mcmf.addedge(x[i], y[i], INF, 1);
		mcmf.addedge(y[i], x[i], INF, 1);
	}

	int cost;
	int Flow = mcmf.minCostMaxflow(s, t, cost);

	if (Flow != all){
		printf("-1\n");
	}
	else{
		printf("%d\n", cost);
	}
}
int main()
{
	//freopen("f:\\input.txt", "r", stdin);
	while (~scanf("%d%d", &N, &M)){
		forr(i, 0, N)	scanf("%d%d", &a[i], &b[i]);
		forr(i, 0, M)	scanf("%d%d", &x[i], &y[i]);
		solve();
	}
	return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值