ZOJ 3885 -- The Exchange of Items [maximum minimum fee stream & amp; graph], zoj3885 --

Source: Internet
Author: User

ZOJ 3885 -- The Exchange of Items [maximum minimum fee stream & amp; graph], zoj3885 --

The Exchange of Items Time Limit: 2 Seconds Memory Limit: 65536 KB

Bob lives in an existing ent 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 your agers.

At first, Bob hasNKinds of items indexed from 1N, And each item hasAi. There areMWays to exchanges items. ForITh way (Xi,Yi), Bob can exchange oneXiTh item to oneYiTh item, vice versa. Now Bob wants that hisITh item has exactlyBi, 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:NAndM(1 <=N,M<= 100 ).
The nextNLines contains two integers:AiAndBi(1 <=Ai,Bi<= 10,000 ).
FollowingMLines contains two integers:XiAndYi(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 cocould 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


Question:

Bob has N kinds of items, each of which has Ai items, he wants to switch to Bi items, and now there are M kinds of exchange methods (a type of I item can be replaced with a type of j item, and vice versa). Now, I want to ask Bob the minimum number of exchanges required to achieve the goal. If it is impossible to complete the exchange, output-1.


Analysis: A good idea. Set a super source, link to all items, the capacity is Ai, the cost is 0, set a super collection point, make all items connected to the super collection point, the capacity is the cost of Bi is 0, for each exchange method, the capacity of both parties (two-way) is INF and the fee is 1. After creating a graph, run the tip to check whether the final maximum stream is equal to the sum of Bi (full stream). If yes, the minimum cost will be output.

#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#define maxn 110#define maxm 110000#define INF 0x3f3f3f3fusing namespace std;int n, m;int inset, outset;int sum;struct node {    int u, v, cap, flow, cost, next;};node edge[maxm];int head[maxn], cnt;int dist[maxn], vis[maxn];int per[maxn];void init(){    cnt = 0;    memset(head, -1, sizeof(head));}void add(int u, int v, int w, int c){    node E1 = {u, v, w, 0, c, head[u]};    edge[cnt] = E1;    head[u] = cnt++;    node E2 = {v, u, 0, 0, -c, head[v]};    edge[cnt] = E2;    head[v] = cnt++;}void getmap(){    int a, b;    sum = 0;    outset = 0;    inset = n + 1;    for(int i = 1; i <= n; ++i){        scanf("%d%d", &a, &b);        sum += b;        add(outset, i, a, 0);        add(i, inset, b, 0);    }    for(int i = 1; i <= m; ++i){        scanf("%d%d", &a, &b);        add(a, b, INF, 1);        add(b, a, INF, 1);    }}bool SPFA(int st, int ed){    queue<int>q;    for(int i = 0; i <= inset; ++i){        dist[i] = INF;        vis[i] = 0;        per[i] = -1;    }    dist[st] = 0;    vis[st] = 1;    q.push(st);    while(!q.empty()){        int u = q.front();        q.pop();        vis[u] = 0;        for(int i = head[u]; i != -1; i = edge[i].next){            node E = edge[i];            if(dist[E.v] > dist[u] + E.cost && E.cap > E.flow){                dist[E.v] = dist[u] + E.cost;                per[E.v] = i;                if(!vis[E.v]){                    vis[E.v] = 1;                    q.push(E.v);                }            }        }    }    return per[ed] != -1;}void MCMF(int st, int ed, int &cost, int &flow){    flow = 0;    cost = 0;    while(SPFA(st, ed)){        int mins = INF;        for(int i = per[ed]; i != -1; i = per[edge[i ^ 1].v]){            mins = min(mins, edge[i].cap - edge[i].flow);        }        for(int i = per[ed]; i != -1; i = per[edge[i ^ 1].v]){            edge[i].flow += mins;            edge[i ^ 1].flow -= mins;            cost += edge[i].cost * mins;        }        flow += mins;    }}int main (){    while(scanf("%d%d", &n, &m) != EOF){        init();        getmap();        int cost ,flow;        MCMF(outset, inset, cost, flow);        if(flow == sum)            printf("%d\n", cost);        else            printf("-1\n");    }    return 0;}


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.