2012 icpc 成都赛区的一题, 被坑了..唉往事不堪重提
#include <cstdio>
#include <cstring>
#include <queue>
#define MAXVERTEX 803
#define MAXARC 170000
#define INF 0x7fffffff
using namespace std;
struct Edge{
int v, val, next;
}edge[MAXARC];
int e_cnt;
char map[MAXVERTEX][MAXVERTEX];
int head[MAXVERTEX], dis[MAXVERTEX], gap[MAXVERTEX], cur[MAXVERTEX], pre[MAXVERTEX];
void insert_arc(int u, int v, int w)
{
edge[e_cnt].v = v; edge[e_cnt].val = w;
edge[e_cnt].next = head[u]; head[u] = e_cnt ++;
swap(u, v);
edge[e_cnt].v = v; edge[e_cnt].val = 0;
edge[e_cnt].next = head[u]; head[u] = e_cnt ++;
}
void bfs(int des, int NV)
{
int u, v;
queue<int> q;
memset(dis, -1, sizeof(dis));
memset(gap, 0, sizeof(gap));
q.push(des);
dis[des] = 0; gap[0] = 1;
while( !q.empty() ) {
u = q.front(); q.pop();
for(int i = head[u]; i != -1; i = edge[i].next) {
v = edge[i].v;
if( edge[i].val || dis[v] != -1 ) continue;
q.push(v); dis[v] = dis[u]+1; gap[dis[v]] ++;
}
}
}
int i_sap(int src, int des, int VN)
{
int u(src), v, min_dis, max_flow(0), path_flow(INF);
bfs(des,VN); pre[u] = u;
for(int i = 0; i <= VN; i ++)
cur[i] = head[i];
while( dis[src] < VN ) {
loop:
for(int &i = cur[u]; i != -1; i = edge[i].next) {
v = edge[i].v;
if( !edge[i].val || dis[u] != dis[v]+1 ) continue;
pre[v] = u; path_flow = min(path_flow, edge[i].val);
u = v;
if( v == des ) {
for(u = pre[u]; v != src; v = u, u = pre[u]) {
edge[cur[u]].val -= path_flow; edge[cur[u]^1].val += path_flow;
}
max_flow += path_flow; path_flow = INF;
}
goto loop;
}
min_dis = VN;
for(int i = head[u]; i != -1; i = edge[i].next) {
v = edge[i].v;
if( edge[i].val && min_dis > dis[v] ) {
cur[u] = i; min_dis = dis[v];
}
}
gap[dis[u]] --;
if( !gap[dis[u]] ) break;
dis[u] = min_dis+1;
gap[dis[u]] ++; u = pre[u];
}
return max_flow;
}
int main(int argc, char const *argv[])
{
int vertex, fc, dc, v, src, des, VN, base;
register int i, j;
while( ~scanf("%d %d %d", &vertex, &fc, &dc) ) {
src = 2*vertex+fc+dc+1; des = src+1; VN = des;
e_cnt = 0; memset(head, -1, sizeof(head));
for(i = 1; i <= fc; i ++) {
scanf("%d", &v); insert_arc(src, i, v);
}
for( i = 1; i <= dc; i ++) {
scanf("%d", &v); insert_arc(fc+i, des, v);
}
base = fc+dc;
for( i = 0; i < vertex; i ++)
scanf("%s", map[i]);
for( i = 1; i <= vertex; i ++) {
insert_arc(base+i, base+i+vertex, 1);
for( j = 1; j <= fc; j ++) {
if( 'Y' == map[i-1][j-1] )
insert_arc(j, base+i, 1);
}
}
for( i = 0; i < vertex; i ++)
scanf("%s", map[i]);
base += vertex;
for( i = 1; i <= vertex; i ++) {
for( j = 1; j <= dc; j ++) {
if( 'Y' == map[i-1][j-1] )
insert_arc(base+i, j+fc, 1);
}
}
printf("%d\n", i_sap(src, des, VN));
}
return 0;
}