标签:
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
#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(费用流)
标签:
原文地址:http://blog.csdn.net/llx523113241/article/details/47379495