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.
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.
For each test case output the minimal times of transactions. If Bob could not reach his goal, output -1 instead.
2 1 1 2 2 1 1 2 4 2 1 3 2 1 3 2 2 3 1 2 3 4
1 -1Author: FENG, Jingyi
#include <bits/stdc++.h> using namespace std; #define MOD 0x3f3f3f3f #define V 1000 + 10 #define E 10000 + 10 int n, m, flow_sum;// 点数 边数 总流量 bool vis[V]; int cnt, dist[V], head[V], pre[V]; struct Edge { int u, v, c, cost, next;//This 'c' means capacity } edge[E<<2]; void init() { cnt = flow_sum = 0; memset(head, -1, (n+3) * sizeof(int)); } void adde(int u, int v, int c, int cost) { edge[cnt].u = u; edge[cnt].v = v; edge[cnt].c = c; edge[cnt].cost = cost; edge[cnt].next = head[u]; head[u] = cnt++; edge[cnt].u = v; edge[cnt].v = u; edge[cnt].c = 0; edge[cnt].cost = -cost; edge[cnt].next = head[v]; head[v] = cnt++; } bool spfa(int begin, int end) { int u, v; queue<int> q; memset(pre, -1, (n+3)*sizeof(int)); memset(vis, 0, (n+3)*sizeof(bool)); memset(dist, 0x3f, (n+3)*sizeof(int)); vis[begin] = 1; dist[begin] = 0; q.push(begin); while(!q.empty()) { u = q.front(); q.pop(); vis[u] = false; for(int i=head[u]; i!=-1; i=edge[i].next) if(edge[i].c > 0) { v = edge[i].v; if(dist[v] > dist[u] + edge[i].cost) { dist[v] = dist[u] + edge[i].cost; pre[v] = i; if(!vis[v]) { vis[v] = true; q.push(v); } } } } return dist[end] != MOD; } int sum; int MCMF(int begin, int end) { int ans = 0, flow; while(spfa(begin, end)) { flow = MOD; for(int i=pre[end]; i!=-1; i=pre[edge[i].u]) flow = min(flow, edge[i].c); for(int i=pre[end]; i!=-1; i=pre[edge[i].u]) { edge[i].c -= flow; edge[i^1].c += flow; } ans += dist[end] * flow; flow_sum += flow; } return ans;//返回最小费用 } int main() { while(~scanf("%d%d", &n, &m)) { init(); int sum1 = 0, sum2 = 0; sum = 0; int a, b; for(int i = 1; i <= n; i++) { scanf("%d%d", &a, &b); sum1 += a; sum2 += b; if(a > b) { adde(0, i, a - b, 0); } else if(a < b) { adde(i, n + 1, b - a, 0); sum += b - a; } } for(int i=0; i<m; i++) { scanf("%d%d", &a, &b); adde(a, b, MOD, 1); adde(b, a, MOD, 1); } if(sum1 != sum2) { printf("-1\n"); continue; } int ans = MCMF(0, n + 1); //cout<<flow_sum<<endl; if(flow_sum == sum) printf("%d\n", ans); else printf("-1\n"); } return 0; }