zoukankan      html  css  js  c++  java
  • 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 (XiYi), 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 <= NM <= 100).
    The next N lines contains two integers: Ai and Bi (1 <= AiBi <= 10,000).
    Following M lines contains two integers: Xi and Yi (1 <= XiYi <= 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

    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[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[u]);
    17     head[u] = 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[u] = false;
    34         for(int i = head[u]; ~i; i = e[i].next){
    35             if(e[i].flow && d[e[i].to] > d[u] + e[i].cost){
    36                 d[e[i].to] = d[u] + e[i].cost;
    37                 p[e[i].to] = i;
    38                 if(!in[e[i].to]){
    39                     in[e[i].to] = true;
    40                     q.push(e[i].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[i].flow);
    53         for(int i = p[T]; ~i; i = p[e[i^1].to]){
    54             e[i].flow -= minF;
    55             e[i^1].flow += minF;
    56         }
    57         flow += minF;
    58         cost += d[T]*minF;
    59     }
    60     if(sum == flow) printf("%d
    ",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
  • 相关阅读:
    聊一聊所谓的B端C化
    NetCore3.1IIS其他网站出现HTTP503无法访问解决办法
    技术方案模板 fn
    技术方案模板
    composer的常用操作(composer 2.2.1)
    thinkphp6: 用validate验证参数合法性(thinkphp 6.0.9/php 8.0.14)
    thinkphp6: 从6.0.9升级(php 8.0.14)到 6.0.10lts版本(php 8.1.1)
    thinkphp6: 自定义配置文件(php 8.1.1 / thinkphp v6.0.10LTS)
    linux(ubuntu21.10):为chrome安装jsonvue(chrome 96)
    thinkphp6:mysql数据库使用事务(php 8.1.1 / thinkphp v6.0.10LTS)
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4755639.html
Copyright © 2011-2022 走看看