ZOJ 3885--The Exchange of Items【最小費用最大流 && 建圖】,zoj3885--the

來源:互聯網
上載者:User

ZOJ 3885--The Exchange of Items【最小費用最大流 && 建圖】,zoj3885--the

The Exchange of ItemsTime 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 hasAi. There areM ways to exchanges items. For theith 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: 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 11 22 11 24 21 32 13 22 31 23 4
Sample Output
1-1


題意:

Bob有N種物品,每種物品有Ai個,他想換成Bi個,現在有M種交換方式(一個第i種物品可以換成一個第j種物品,反之亦然,雙向的),現在問,Bob達成目標所需的最小交換次數,當不可能完成交換時,輸出-1。


解析:比較好想的思路。設定一個超級源點,連向所有的物品,容量為Ai,費用為0,設定一個超級匯點,使所有物品連向超級匯點,容量為Bi費用為0,每種交換方式,串連交換雙方(雙向)容量為INF,費用為1。建完圖跑最小費,看最後的最大流會不會等於Bi的和(滿流),會的話輸出最小費用。

#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;}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.