ZOJ 3885 The Exchange of Items(费用流)
来源:互联网 发布:新三板 软件 编辑:程序博客网 时间:2024/06/06 01:10
ZOJ 3885 The Exchange of Items
Description
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
题目大意:Bob有N种物品,每种物品有Ai个,他想换成Bi个,现在有M种交换方式(一个第i种物品可以换成一个第j种物品,反之亦然,双向的),现在问,Bob达成目标所需的最小交换次数,当不可能完成交换时,输出-1。
解题思路:设置一个超级源点,连向所有的物品,容量为Ai,费用为0,设置一个超级汇点,使所有物品连向超级汇点,容量为Bi费用为0,每种交换方式,连接交换双方(双向)容量为INF,费用为1。建完图跑一发最小费,看最后的最大流会不会等于Bi的和,会的话输出最小费用。
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>#include <queue>using namespace std;const int N = 115;const int M = 30000;const int INF = 0x3f3f3f3f;typedef long long ll;int n, m, s, t;int A[N], B[N], sumB;int pre[N], inq[N]; ll a[N], d[N];struct Edge{ int from, to; ll cap, flow; ll cos;};vector<Edge> edges;vector<int> G[M];void init() { for (int i = 0; i < M; i++) G[i].clear(); edges.clear();}void addEdge(int from, int to, ll cap, ll flow, ll cos) { edges.push_back((Edge){from, to, cap, 0, cos}); edges.push_back((Edge){to, from, 0, 0, -cos}); int m = edges.size(); G[from].push_back(m - 2); G[to].push_back(m - 1);}int BF(int s, int t, ll& flow, ll& cost) { queue<int> Q; memset(inq, 0, sizeof(inq)); memset(a, 0, sizeof(a)); memset(pre, 0, sizeof(pre)); for (int i = 0; i < N; i++) d[i] = INF; d[s] = 0; a[s] = INF; inq[s] = 1; int flag = 1; pre[s] = 0; Q.push(s); while (!Q.empty()) { int u = Q.front(); Q.pop(); inq[u] = 0; for (int i = 0; i < G[u].size(); i++) { Edge &e = edges[G[u][i]]; if (e.cap > e.flow && d[e.to] > d[u] + e.cos) { d[e.to] = d[u] + e.cos; a[e.to] = min(a[u], e.cap - e.flow); pre[e.to] = G[u][i]; if (!inq[e.to]) { inq[e.to] = 1; Q.push(e.to); } } } flag = 0; } if (d[t] == INF) return 0; flow += a[t]; cost += (ll)d[t] * (ll)a[t]; for (int u = t; u != s; u = edges[pre[u]].from) { edges[pre[u]].flow += a[t]; edges[pre[u]^1].flow -= a[t]; } return 1;}int MCMF(int s, int t, ll& cost) { ll flow = 0; cost = 0; while (BF(s, t, flow, cost)); return flow;}void input() { int a, b; s = 0, t = 105; for (int i = 1; i <= n; i++) { scanf("%d %d", &a, &b); addEdge(s, i, a, 0, 0); addEdge(i, t, b, 0, 0); sumB += b; } for (int i = 1; i <= m; i++) { scanf("%d %d", &a, &b); addEdge(a, b, INF, 0, 1); addEdge(b, a, INF, 0, 1); }}int main() { while (scanf("%d %d", &n, &m) == 2) { sumB = 0; init(); input(); ll cost; if (MCMF(s, t, cost) != sumB) { printf("-1\n"); } else printf("%lld\n", cost); } return 0;}
- ZOJ 3885 The Exchange of Items(费用流)
- 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【最小费用最大流 && 建图】
- 费用流 zoj3885 The 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 2601 Warehouse Keeper(费用流)
- zoj 3362(最大费用)
- The order of items in struts-config.xml
- chapter 2 measure the similarity of two items
- Can I change the order of items in category views?
- ZOJ 3231 Apple Transportation(上下界费用流)
- ZOJ 3231 - Apple Transportation(上下界费用流)
- Android基础之px,dp,sp对比以及应用
- hy8uhhui
- 装箱,拆箱&&枚举
- Codeforces445A DZY Loves Chessboard
- LotusScript查询指定字符在字符串中的位置
- ZOJ 3885 The Exchange of Items(费用流)
- Python笔记
- Socket 通信原理(Android客户端和服务器以TCP&&UDP方式互通)
- Cake (hdu 5355 构造+dfs)
- java 泛型
- android 网络框架 volley 初识
- hdu 2191 多重背包(物品的件数固定)
- 命名要恰如其分
- 自己整理的ADS1118的寄存器,跟io430.统一风格