UVa 259 Software Allocation ( 最大流 )

來源:互聯網
上載者:User

這道題也是,輸入很坑,每組之間也空格結束,最後以EOF結束

建圖其實有一種比這個好的方法,不用拆點,就是源點和computer相連,容量1, computer和program連,容量1, program和匯點連,容量為相應數字

但是一下代碼,是拆點做的,把computer拆了,因為computer的容量為1,下面代碼也是對照這個建圖方法建的,program和源點連,容量為相應數字,和computer連,容量為1,computer容量為1, computer拆出來的點,和匯點連,容量INF

代碼如下:

//by Molly//Maximum Flow#include <cstdio>#include <cstring>#include <queue>#include <algorithm>#include <string>#include <iostream>using namespace std;//標號0為超級源點,標號1-26為26中應用程式,27-46(因為每個computer每天只能做一個程式,所以為computer是有容量的,要拆成兩個點)computer的編號,48為超級匯點const int INF = 1000000;const int N = 100;int cap[N][N], flow[N][N], a[N], p[N];int s, t, sum;char ap, num, com[N], ans[15];bool maxFlow() {    queue <int> q;    memset(flow, 0, sizeof(flow));    int f = 0;    while ( 1 ) {        for ( int i = 0; i <= t; p[i++] = -1 );        memset(a, 0, sizeof(a));        a[s] = INF;        q.push(s);        while ( !q.empty() ) {            int u = q.front(); q.pop();            for ( int v = 0; v <= t; ++v ) {                if ( !a[v] && cap[u][v] > flow[u][v] ) {                    p[v] = u;                    q.push(v);                    a[v] = min( a[u], cap[u][v] - flow[u][v] );                }            }        }        if ( a[t] == 0 ) break;        for ( int u = t; u != s; u = p[u] ) {            if ( u < 37 && u > 26 ) if ( p[u] != -1 ) ans[u-27] = p[u] + 'A' - 1;            flow[p[u]][u] += a[t];            flow[u][p[u]] -= a[t];        }        f += a[t];    }    if ( f >= sum ) return true;    else return false;}void build () {    int u = ap-'A'+1, c = num-'0';    cap[s][u] += c;    for ( int v = 0; com[v] != ';'&& v < strlen(com); ++v ){         cap[u][com[v]-'0'+27] += 1;     }}void init() {    for ( int i = 27; i < 37; ++i ) {        cap[i][i+10] += 1; cap[i+10][t] += INF;    }} int main(){// freopen("input.txt","r", stdin);// freopen("output.txt", "w", stdout);     while ( cin >> ap >> num >> com ) {        memset(cap, 0, sizeof(cap));        getchar();        build();        s = 0, t = 47, sum = num-'0';        init();        while ( ( ap = getchar() ) != 0xa && ap != EOF ) {            scanf("%c %s", &num, com);            getchar();            sum += num - '0';            build();        }        for ( int i = 0; i < 10; ans[i++] = '_' );        if ( !maxFlow() ) cout << "!" << endl;        else {            for ( int i = 0; i < 10; ++i )                cout << ans[i];            cout << endl;        }        //for ( int i = 0; i <= t; printf("\n"), ++i )           //  for ( int j = 0; j <= t; ++j ) printf("%d ", cap[i][j] );    }} 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.