ibaobei 发表于 2015-9-11 09:43:54

ZOJ 3885 The Exchange of Items

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的双向边



看是否满流








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;
14 int head,p,tot;
15 void add(int u,int v,int flow,int cost){
16   e = arc(v,flow,cost,head);
17   head = tot++;
18   e = arc(u,0,-cost,head);
19   head = tot++;
20 }
21 bool in;
22 int d,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 = 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.to] > d + e.cost){
36               d.to] = d + e.cost;
37               p.to] = i;
38               if(!in.to]){
39                     in.to] = true;
40                     q.push(e.to);
41               }
42             }
43         }
44   }
45   return p > -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; ~i; i = p.to])
52             minF = min(minF,e.flow);
53         for(int i = p; ~i; i = p.to]){
54             e.flow -= minF;
55             e.flow += minF;
56         }
57         flow += minF;
58         cost += d*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]
查看完整版本: ZOJ 3885 The Exchange of Items