zoj 3885 The Exchange of Items 【最小费用最大流】
来源:互联网 发布:directx12优化 编辑:程序博客网 时间:2024/06/04 19:35
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 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 <= 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 11 22 11 24 21 32 13 22 31 23 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 0x3f3f3f3fusing 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;}
- zoj -- 3885 The Exchange of Items(最小费用最大流)
- zoj 3885 The Exchange of Items 最小费用最大流
- ZOJ 3885The Exchange of Items 最小费用最大流
- zoj 3885 The Exchange of Items 【最小费用最大流】
- ZOJ 3885--The Exchange of Items【最小费用最大流 && 建图】
- The Exchange of Items (141 - ZOJ Monthly, July 2015 - E 最小费用最大流)
- (最小费用最大流)The Exchange of Items (141 - ZOJ Monthly, July 2015 - E )
- ZOJ 3885 The Exchange of Items(费用流)
- 费用流 zoj3885 The Exchange of Items
- ZOJ 3885 The Exchange of Items
- ZOJ 3885 The Exchange of Items
- ZOJ 3308 最小费用最大流
- ZOJ 3362 Beer Problem 最小费用最大流
- ZOJ 3362 Beer Problem(最小费用最大流)
- ZOJ 2404 Going Home(最小费用最大流)
- 最小费用最大流
- 最小费用最大流
- 最小费用最大流
- 三尺考研路———2015/8/26
- ub 网络框架的几种线程模型
- Permutations
- jquery 当值改变时触发方法
- 数字组合
- zoj 3885 The Exchange of Items 【最小费用最大流】
- Android文件存储
- make menuconfig出现的错误
- hdu2768
- Http Basic Authentication has some limitations, maybe nginx could do some help...
- Communication Patterns
- IOS 将类的声明,类的定义,主函数分开放的代码。
- 题目:买卖股票的最佳时机 II
- GCD(Grand Central Dispatch)