ZOJ 3885 the Exchange of Items
Description
Bob lives in an ancient village, where transactions is done by one item exchange with another. Bob is very clever and he knows "what items would become more valuable later on." So, Bob had decided to does some business with villagers.
At first, Bob had N kinds of items indexed from 1 to N, and each item has Ai. There is M ways to exchanges items. For the ith (Xi, Yi), Bob can exchange one XIth item to one Yith item, vice versa. Now Bob wants this his ith item have exactly Bi, and he wonders what the minimal times of transactions are.
Input
There is multiple test cases.
For each test case:the first line contains the Integers:n and M (1 <= N, M <= 100).
The next N lines contains, Integers:ai and bi (1 <= Ai, bi <= 10,000).
Following M lines contains, 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 has n kinds of items, each item has AI, he wants to switch to BI, now have M exchange (one item I can be replaced by a J item, and vice versa, two-way), now ask, Bob to achieve the goal of the minimum number of exchanges, when it is impossible to complete the exchange, output-1. Problem-solving ideas: Set up a super source point, even to all items, the capacity of AI, the cost is 0, set a super meeting point, so that all items connected to the Super meeting point, the capacity for BI fee of 0, each Exchange method, the connection between the two sides (bidirectional) capacity of the INF, the cost is 1. After the plot to run a minimum fee, see the final maximum flow will be equal to the sum of BI, will then output the minimum cost.
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>#include <queue>using namespace STD;Const intN = the;Const intM =30000;Const intINF =0x3f3f3f3f;typedef Long LongllintN, M, S, t;intA[n], b[n], sumb;intPre[n], inq[n]; ll A[n], d[n];structedge{intFrom, to; ll cap, flow; llCos;}; vector<Edge>Edges vector<int>G[M];voidInit () { for(inti =0; i < M; i++) g[i].clear (); Edges.clear ();}voidAddedge (intFromintTo, ll cap, ll flow, LLCos{Edges.push_back (Edge) {from, to, Cap,0,Cos}); Edges.push_back (Edge) {To, from,0,0, -Cos});intm = Edges.size (); G[from].push_back (M-2); G[to].push_back (M-1);}intBF (intSintT, ll& Flow, ll& cost) { Queue<int>Q;memset(INQ,0,sizeof(INQ));memsetA0,sizeof(a));memset(Pre,0,sizeof(pre)); for(inti =0; i < N; i++) D[i] = INF; D[s] =0; A[s] = INF; Inq[s] =1;intFlag =1; Pre[s] =0; Q.push (s); while(! Q.empty ()) {intU = Q.front (); Q.pop (); Inq[u] =0; for(inti =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(intu = t; U! = S; U = edges[pre[u]].from) {Edges[pre[u]].flow + = a[t]; edges[pre[u]^1].flow-= a[t]; }return 1;}intMCMF (intSintT, ll& cost) {ll flow =0; Cost =0; while(BF (S, t, flow, cost));returnFlow;}voidInput () {intA, B; s =0, t = the; for(inti =1; I <= N; i++) {scanf("%d%d", &a, &b); Addedge (S, I, A,0,0); Addedge (i, T, B,0,0); Sumb + = b; } for(inti =1; I <= m; i++) {scanf("%d%d", &a, &b); Addedge (A, B, INF,0,1); Addedge (b, A, INF,0,1); }}intMain () { 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;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission can also be reproduced, but to indicate the source oh.
ZOJ 3885 The Exchange of Items (charge flow)