POJ-1698 Alice's Chance 最大流

不会sap,邻接矩阵过。

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

int cap[MAXN+1][MAXN+1], flow[MAXN+1][MAXN+1], N;
int p[MAXN+1], c[MAXN+1];
int seq[25][15], maxflow;

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

inline int min(int a, int b)
{
return a < b ? a : b;
}

void bfs()
{
int finish = 0, pos;
while (!finish) {
memset(c, 0, sizeof (c)); // 记录推送流量
c[0] = INF;
queue<int>q;
q.push(0);
while (!q.empty()) {
if (c[MAXN])
break;
pos = q.front();
q.pop();
for (int i = 0; i <= MAXN; ++i) {
if (!c[i] && flow[pos][i] < cap[pos][i]) {
c[i] = min(c[pos], cap[pos][i]-flow[pos][i]);
p[i] = pos;
q.push(i);
}
}
}
maxflow -= c[MAXN];
if (c[MAXN] == 0) {
finish = 1;
break;
}
pos = MAXN;
while (pos != 0) {
flow[p[pos]][pos] += c[MAXN];
flow[pos][p[pos]] -= c[MAXN];
pos = p[pos];
}
}
}

int main()
{
int T;
scanf("%d", &T);
while (T--) {
init();
scanf("%d", &N);
for (int i = 0; i < N; ++i) {
for (int j = 0; j < 9; ++j) {
scanf("%d", &seq[i][j]);
}
maxflow += seq[i][7];
cap[0][i+1] = seq[i][7];
for (int j = 0; j < 7; ++j) {
if (seq[i][j]) {
for (int k = 0; k < seq[i][8]; ++k) {
cap[i+1][k*7+N+1+j] = 1;
cap[k*7+N+1+j][MAXN] = 1;
}
}
}
}
bfs();
if (maxflow == 0) {
puts("Yes");
}
else {
puts("No");
}
}
return 0;
}



你可能感兴趣的:(poj)