zoj 3885 The Exchange of Items 【最小费用最大流】
2015-08-27 00:40
453 查看
The Exchange of Items
Time Limit: 2 Seconds Memory Limit: 65536 KB
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 oneYith item, vice versa. Now Bob wants that his ith item has exactly Bi, and he wonders
what the minimal times of transactions is.
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.
Author: FENG, Jingyi
Source: ZOJ Monthly, July 2017
题意:Bob有N种物品,已知每种物品有Ai个,现在他想换成Bi个。给出有M种交换方式<Xi,Yi>(表示Xi物品和Yi物品可以互相转换)。问你——Bob达成目标所需要的最小交换次数,若不可能达成目标输出-1。
思路:最小费用最大流。记录目标状态的总流量sum,判断流入汇点的总流量是否等于sum。若等于说明目标状态可达,输出最小费用,反之不可达,输出-1。
建图:设置超级源点source,超级汇点sink。
1,source向所有物品建边,容量为物品的初始数目,费用为0;
2,所有物品向sink建边,容量为物品的目标数目,费用为0;
3,M种转换关系建双向边,容量为INF(为了能够达到目标状态,我们可以任意选择该边的流量),费用为1。
再跑一遍最小费用最大流,若最后的最大流flow等于目标状态的总流量则输出最小费用cost,反之输出-1。
AC代码:
Time Limit: 2 Seconds Memory Limit: 65536 KB
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 oneYith 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
Author: FENG, Jingyi
Source: ZOJ Monthly, July 2017
题意:Bob有N种物品,已知每种物品有Ai个,现在他想换成Bi个。给出有M种交换方式<Xi,Yi>(表示Xi物品和Yi物品可以互相转换)。问你——Bob达成目标所需要的最小交换次数,若不可能达成目标输出-1。
思路:最小费用最大流。记录目标状态的总流量sum,判断流入汇点的总流量是否等于sum。若等于说明目标状态可达,输出最小费用,反之不可达,输出-1。
建图:设置超级源点source,超级汇点sink。
1,source向所有物品建边,容量为物品的初始数目,费用为0;
2,所有物品向sink建边,容量为物品的目标数目,费用为0;
3,M种转换关系建双向边,容量为INF(为了能够达到目标状态,我们可以任意选择该边的流量),费用为1。
再跑一遍最小费用最大流,若最后的最大流flow等于目标状态的总流量则输出最小费用cost,反之输出-1。
AC代码:
#include <cstdio> #include <cstring> #include <queue> #include <stack> #include <vector> #include <algorithm> #define MAXN 110 #define MAXM 1000 #define INF 0x3f3f3f3f using namespace std; struct Edge { int from, to, cap, flow, cost, next; }; Edge edge[MAXM]; int head[MAXN], edgenum; int pre[MAXN], dist[MAXN]; bool vis[MAXN]; int source, sink;//超级源点 超级汇点 int N, M; int sum;//记录目标状态总流量 void init() { edgenum = 0; memset(head, -1, sizeof(head)); } void addEdge(int u, int v, int w, int c) { Edge E1 = {u, v, w, 0, c, head[u]}; edge[edgenum] = E1; head[u] = edgenum++; Edge E2 = {v, u, 0, 0, -c, head[v]}; edge[edgenum] = E2; head[v] = edgenum++; } void getMap() { int a, b; sum = 0; source = 0, sink = N+1; for(int i = 1; i <= N; i++) { scanf("%d%d", &a, &b); addEdge(source, i, a, 0);//超级源点连i 容量为a 费用为0 addEdge(i, sink, b, 0);//i连超级汇点 容量为b 费用为0 sum += b;//记录目标状态总流量 } for(int i = 1; i <= M; i++) scanf("%d%d", &a, &b), addEdge(a, b, INF, 1), addEdge(b, a, INF, 1);//互建边 容量无穷大 费用1 } bool SPFA(int s, int t) { queue<int> Q; memset(dist, INF, sizeof(dist)); memset(vis, false, sizeof(vis)); memset(pre, -1, sizeof(pre)); dist[s] = 0; vis[s] = true; Q.push(s); while(!Q.empty()) { int u = Q.front(); Q.pop(); vis[u] = false; for(int i = head[u]; i != -1; i = edge[i].next) { Edge E = edge[i]; if(dist[E.to] > dist[u] + E.cost && E.cap > E.flow) { dist[E.to] = dist[u] + E.cost; pre[E.to] = i; if(!vis[E.to]) { vis[E.to] = true; Q.push(E.to); } } } } return pre[t] != -1; } void MCMF(int s, int t, int &cost, int &flow) { cost = flow = 0; while(SPFA(s, t)) { int Min = INF; for(int i = pre[t]; i != -1; i = pre[edge[i^1].to]) { Edge E = edge[i]; Min = min(Min, E.cap - E.flow); } for(int i = pre[t]; i != -1; i = pre[edge[i^1].to]) { edge[i].flow += Min; edge[i^1].flow -= Min; cost += edge[i].cost * Min; } flow += Min; } } int main() { while(scanf("%d%d", &N, &M) != EOF) { init(); getMap(); int cost, flow; MCMF(source, sink, cost, flow); if(flow == sum)//若流入汇点的总流量 等于 目标状态总流量 说明可行 printf("%d\n", cost); else printf("-1\n"); } return 0; }
相关文章推荐
- 十四、第三章再续:快速选择SELECT算法的深入分析与实现
- 十三、通过浙大上机复试试题学SPFA 算法
- 十五、多项式乘法与快速傅里叶变换
- shiro android
- Linux磁盘与文件系统管理详细总结
- Filter 编码问题
- 快速排序算法
- 十二之续、快速排序算法的深入分析
- Opengl中矩阵和perspective/ortho的相互转换
- iOS部分-UI基础控件 - 01天 入门 第04课 加法计算器小结
- c 语言用递归法倒序字符串
- 十二之再续:快速排序算法之所有版本的c/c++实现
- iOS部分-UI基础控件 - 01天 入门 第03课 IBAction&IBOutlet
- 数制
- 优化资源配置读后感
- 猜拳游戏 3.0升级版
- 输入3个字符串,按字符串由短到长的顺序输出
- Hadoop(二)——分布式集群搭建
- Android Api Demos登顶之路(五十)Notification-->Service Controller
- OC语言 关于description的探讨