這道題也是,輸入很坑,每組之間也空格結束,最後以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] ); }}