zoj 月赛 E 费用流
来源:互联网 发布:疯狂安卓讲义源码 编辑:程序博客网 时间:2024/04/28 16:07
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 11 22 11 24 21 32 13 22 31 23 4
Sample Output
1-1
Author: FENG, Jingyi
Source: ZOJ Monthly, July 2017
#include <bits/stdc++.h>using namespace std;#define MOD 0x3f3f3f3f#define V 1000 + 10#define E 10000 + 10int 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;}
- zoj 月赛 E 费用流
- ZOJ3885 2015July月赛 E 费用流
- ZOJ Monthly, January 2014 E Diagonal (浙江大学月赛)
- ZOJ 3764 Easy billiards 月赛E题
- zoj 3802 Easy 2048 Again (14.8 浙大月赛 E)
- The Exchange of Items (141 - ZOJ Monthly, July 2015 - E 最小费用最大流)
- (最小费用最大流)The Exchange of Items (141 - ZOJ Monthly, July 2015 - E )
- zoj博弈月赛
- zoj 月赛
- zoj 月赛
- zoj 月赛
- codeforces 277E 最小费用最大流
- Codeforces 237E Build String 【费用流】
- ZOJ 3362 Beer Problem(最小费用流)
- ZOJ 3308 最小费用最大流
- ZOJ 3362 无向图费用流
- 【ZOJ】3362 Beer Problem 最小费用流
- ZOJ 2601 Warehouse Keeper(费用流)
- 【Objective-C编程】深入浅出字符串NSString
- kvm配置桥接网卡
- debug调试
- centos6.6设置samba和nginx访问用户家目录
- UVA524-Prime Ring Problem
- zoj 月赛 E 费用流
- 走迷宫
- PHP配置文件详解php.ini
- NSMutableArray和NSArray
- 性能优化相关T-code
- 优化Linux下的内核TCP参数来提高服务器负载能力
- 按钮,alert,EditText
- LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- InteIlij idea/Android studio 使用记录