POJ-2584 T-Shirt Gumbo 最大流

简单构图,构造一个S流向 S,M,L,X,T,赛事 T1,T2,T2...TN流向汇点T。

代码如下:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#define INF 0x3f3f3f3f
#define MAXN 25
using namespace std;

int N, cap[40][40], flow[40][40], p[40], c[40], S = 0, T = 38;

int hash[130], maxflow;

void init()
{
maxflow = 0;
memset(cap, 0, sizeof (cap));
memset(flow, 0, sizeof (flow));
memset(p, 0xff, sizeof (p));
}

inline int min(int x, int y)
{
return x < y ? x : y;
}

void bfs()
{
int finish = 0, pos;
while (!finish) {
memset(c, 0, sizeof (c));
c[S] = INF;
queue<int>q;
q.push(S);
while (!q.empty()) {
if (c[T]) {
break;
}
pos = q.front();
q.pop();
for (int i = 1; i <= T; ++i) {
if (!c[i] && cap[pos][i] > flow[pos][i]) {
c[i] = min(c[pos], cap[pos][i] - flow[pos][i]);
p[i] = pos;
q.push(i);
}
}
}
maxflow += c[T];
if (!c[T]) {
finish = 1;
continue;
}
pos = T;
while (pos != 0) {
flow[p[pos]][pos] += c[T];
flow[pos][p[pos]] -= c[T];
pos = p[pos];
}
}
}

int main()
{
char op[20], detail[5];
int x;
hash['S'] = 1, hash['M'] = 2, hash['L'] = 3, hash['X'] = 4, hash['T'] = 5;
while (scanf("%s", op), strcmp(op, "ENDOFINPUT")) {
init();
scanf("%d", &N);
for (int i = 1; i <= N; ++i) {
scanf("%s", detail);
for (int j = hash[detail[0]]; j <= hash[detail[1]]; ++j) {
cap[j][5+i] = INF; // 反向边已经清零
cap[5+i][T] = 1;
}
}
for (int i = 1; i <= 5; ++i) {
scanf("%d", &x);
cap[S][i] = x;
}
scanf("%s", op);
bfs();
if (maxflow == N) {
puts("T-shirts rock!");
}
else {
puts("I'd rather not wear a shirt anyway...");
}
}
return 0;
}



你可能感兴趣的:(poj)