Description
Input
Output
Sample Input
1
3
1 1 0
0 1 0
0 1 1
1 0 0
1 0 0
Sample Output
ˆ ˆ
HINT
对于30% 的数据满足1 ≤ n ≤ 12。
对于100% 的数据满足1 ≤ n ≤ 50,1 ≤ T ≤ 20。
Source
网络流水题。
拿来联系网络流模板。
具体怎么见图,相信大家看代码就能明白啦!
于是就不说了(博主太懒了,不,是题太简单)
#include <bits/stdc++.h>
#define N 102
#define T 101
#define M 50010
#define inf 0x7f7f7f7f
using namespace std;
struct Edge
{
int next, to, v;
}e[M];
int n, m, cnt = 1, tot, ans, h[N], head[N], cur[N], q[M], sc[N];
inline int read()
{
int x = 0, f = 1; char ch = getchar();
while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
return x * f;
}
void ins(int u, int v, int w)
{
e[++ cnt].next = head[u];
e[cnt].to = v;
e[cnt].v = w;
head[u] = cnt;
}
void insert(int u, int v, int w)
{
ins(u, v, w);
ins(v, u, 0);
}
int bfs()
{
int t = 0, w = 1;
memset(h, -1, sizeof(h));
q[0] = 0;
h[0] = 0;
while(t != w)
{
int now = q[t ++];
if(t == M) t = 0;
for(int i = head[now]; i; i = e[i].next)
{
int y = e[i].to;
if(h[y] == -1 && e[i].v)
{
h[y] = h[now] + 1;
q[w ++] = y;
if(w == M) w = 0;
}
}
}
if(h[T] == -1) return 0;
else return 1;
}
int dfs(int x, int f)
{
if(x == T) return f;
int w, used = 0;
for(int i = cur[x]; i; i = e[i].next)
{
int y = e[i].to;
if(h[y] == h[x] + 1 && e[i].v)
{
w = dfs(y, min(f - used, e[i].v));
used += w;
e[i].v -= w;
if(e[i].v) cur[x] = i;
e[i ^ 1].v += w;
if(f == used) return f;
}
}
if(used == 0) h[x] = -1;
return used;
}
void dinic()
{
while(bfs())
{
for(int i = 0; i <= T; i ++)
cur[i] = head[i];
ans += dfs(0, inf);
}
}
int main()
{
int test = read();
while(test --)
{
tot = ans = 0;
cnt = 1;
memset(head,0,sizeof(head));
n = read();
for(int i = 1; i <= n; i ++)
{
sc[i] = read();
if(sc[i])
insert(i + n, T, 1);
}
int x;
for(int i = 1; i <= n; i ++)
{
x = read();
if((sc[i] && !x) || !sc[i])
{
insert(0, i, 1);
tot ++;
}
}
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= n; j ++)
{
x = read();
if(x || i == j) insert(i, j + n, 1);
}
dinic();
if(ans == tot)puts("^_^");
else puts("T_T");
}
return 0;
}