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



转载于:https://www.cnblogs.com/Lyush/archive/2012/04/05/2433329.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值