设为首页 收藏本站
查看: 853|回复: 0

[经验分享] ZOJ 3885 The Exchange of Items

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-9-11 09:43:54 | 显示全部楼层 |阅读模式
The Exchange of Items




Time Limit: 2000ms

Memory Limit: 65536KB
This problem will be judged on ZJU. Original ID: 3885
64-bit integer IO format: %lld      Java class name: Main





  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



Source


ZOJ Monthly, July 2017
Author


FENG, Jingyi



解题:费用流



两种建图方式

第一种,源点向i连流为Ai费用为0的边,i向汇点连流为Bi费用为0的边 可以交换的物品之间连费用为1流量无穷的双向边



第二种 对于ai < bi的 i向汇点连流量为bi-ai 费用为0的边,ai > bi的 源点向i连流量为ai-bi 费用为0的边 可以交换的物品间 建立流量无穷费用1的双向边



看是否满流





DSC0000.gif DSC0001.gif


1 #include <bits/stdc++.h>
2 using namespace std;
3 const int INF = 0x3f3f3f3f;
4 const int maxn = 500;
5 struct arc{
6     int to,flow,cost,next;
7     arc(int x = 0,int y = 0,int z = 0,int nxt = -1){
8         to = x;
9         flow = y;
10         cost = z;
11         next = nxt;
12     }
13 }e[maxn*maxn];
14 int head[maxn],p[maxn],tot;
15 void add(int u,int v,int flow,int cost){
16     e[tot] = arc(v,flow,cost,head);
17     head = tot++;
18     e[tot] = arc(u,0,-cost,head[v]);
19     head[v] = tot++;
20 }
21 bool in[maxn];
22 int d[maxn],S,T;
23 bool spfa(){
24     queue<int>q;
25     memset(d,0x3f,sizeof d);
26     memset(in,false,sizeof in);
27     memset(p,-1,sizeof p);
28     d[S] = 0;
29     q.push(S);
30     while(!q.empty()){
31         int u = q.front();
32         q.pop();
33         in = false;
34         for(int i = head; ~i; i = e.next){
35             if(e.flow && d[e.to] > d + e.cost){
36                 d[e.to] = d + e.cost;
37                 p[e.to] = i;
38                 if(!in[e.to]){
39                     in[e.to] = true;
40                     q.push(e.to);
41                 }
42             }
43         }
44     }
45     return p[T] > -1;
46 }
47 void solve(int sum){
48     int cost = 0,flow = 0;
49     while(spfa()){
50         int minF = INF;
51         for(int i = p[T]; ~i; i = p[e[i^1].to])
52             minF = min(minF,e.flow);
53         for(int i = p[T]; ~i; i = p[e[i^1].to]){
54             e.flow -= minF;
55             e[i^1].flow += minF;
56         }
57         flow += minF;
58         cost += d[T]*minF;
59     }
60     if(sum == flow) printf("%d\n",cost);
61     else puts("-1");
62 }
63 int main(){
64     int n,m,u,v,sum;
65     bool flag = false;
66     while(~scanf("%d%d",&n,&m)){
67         //if(flag) puts("");
68         memset(head,-1,sizeof head);
69         sum = S = tot = 0;
70         T = n + 1;
71         for(int i = 1; i <= n; ++i){
72             scanf("%d%d",&u,&v);
73             add(S,i,u,0);
74             add(i,T,v,0);
75             sum += v;
76         }
77         for(int i = 0; i < m; ++i){
78             scanf("%d%d",&u,&v);
79             add(u,v,INF,1);
80             add(v,u,INF,1);
81         }
82         solve(sum);
83     }
84     return 0;
85 }
View Code  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-112184-1-1.html 上篇帖子: [转] .NET中使用Exchange 2007 Webservice来读取邮件 下篇帖子: 纯手工全删除域内最后一个EXCHANGE--How to Manually Uninstall Last Exchange 2010 Server from O
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表