Why WA? I've been passing all the test cases.
Code: Select all
#include <bits/stdc++.h>
using namespace std;
char input[32][32];
int res[32][32][2][5];
int src_res[32][32];
int sink_res[32][23];
typedef pair<int, int> pi;
typedef pair<int, pi> pii;
#define INF (1<<28)
class bfs_node{
public:
int row, col;
int flow;
int on_top;
int pred_row, pred_col;
int pred_on_top;
bfs_node(int R, int C, int ON_TOP, int PR, int PC, int PON_TOP, int FLOW){
row = R;
col = C;
on_top = ON_TOP;
pred_row = PR;
pred_col = PC;
pred_on_top = PON_TOP;
flow = FLOW;
}
};
pii pred[32][32][2];
bool visited[32][32][2];
int dy[] = {-1, 0, 1, 0, 0};
int dx[] = {0, 1, 0, -1, 0};
int get_dir(int curI, int curJ, int preI, int preJ){
for(int dir = 0; dir<5; dir++){
if(preI + dy[dir] == curI && preJ + dx[dir] == curJ){
return dir;
}
}
return -1;
}
int main(){
int numRows, numCols, numCap;
while(scanf("%d %d %d", &numRows, &numCols, &numCap)!=EOF){
for(int i = 0; i<=numRows+1; i++){
for(int j = 0; j<=numCols+1; j++){
src_res[i][j] = 0;
sink_res[i][j] = 0;
for(int dir = 0; dir<5; dir++){
res[i][j][0][dir] = 0;
res[i][j][1][dir] = INF;
}
res[i][j][1][4] = 0;
}
}
vector<pi> sources;
vector<pi> sinks;
for(int i = 1; i<=numRows; i++){
for(int j = 1; j<=numCols; j++){
scanf(" %c", &input[i][j]);
if(input[i][j] == '#'){
sinks.push_back(pi(i,j));
sink_res[i][j] = numCap;
res[i][j][0][4] = INF;
}
if(input[i][j] == '*'){
sources.push_back(pi(i,j));
src_res[i][j] = 1;
res[i][j][0][4] = 1;
}
if(input[i][j] == '.'){
res[i][j][0][4] = 1;
}
if(input[i][j] == '@'){
res[i][j][0][4] = INF;
}
if(input[i][j] == '~'){
res[i][j][0][4] = 0;
}
}
}
int ans_cnt = 0;
while(1){
queue<bfs_node> que;
que.push(bfs_node(0,0,0,-1,-1,0,INF));
for(int i = 0; i<=numRows+1; i++){
for(int j = 0; j<=numCols+1; j++){
for(int k = 0; k<2; k++){
visited[i][j][k] = false;
pred[i][j][k] = pii(-1,pi(-1,-1));
}
}
}
bool found_path = false;
int chosen_flow = -1;
// printf("Finding path\n");
while(!que.empty()){
bfs_node b = que.front();
que.pop();
int row = b.row;
int col = b.col;
int flow = b.flow;
int on_top = b.on_top;
int pred_row = b.pred_row;
int pred_col = b.pred_col;
int pred_on_top = b.pred_on_top;
if(visited[row][col][on_top]){
continue;
}
visited[row][col][on_top] = true;
pred[row][col][on_top] = pii(pred_on_top, pi(pred_row, pred_col));
// printf("Visited (%d,%d)_%d from (%d,%d)_%d\n", row, col, on_top, pred_row, pred_col, pred_on_top);
if(row == numRows+1 && col == numCols+1){
found_path = true;
chosen_flow = flow;
break;
}
if(row == 0 && col == 0){
for(auto src : sources){
if(src_res[src.first][src.second]){
que.push(bfs_node({
src.first, src.second, 0,
0,0,0,
INF
}));
}
}
continue;
}
for(int dir = 0; dir<5; dir++){
int I = row + dy[dir];
int J = col + dx[dir];
if(I<=0 || I>=numRows+1 || J <= 0 || J>=numCols+1){
continue;
}
int next_on_top = on_top ^ 1;
if(input[I][J] == '~' || visited[I][J][next_on_top]){
continue;
}
if(res[row][col][on_top][dir]){
int next_flow = min(flow, res[row][col][on_top][dir]);
que.push(bfs_node({
I,J,next_on_top,
row, col, on_top,
next_flow
}));
}
}
if(sink_res[row][col] && on_top){
que.push(bfs_node({
numRows+1, numCols+1, 0,
row,col, on_top,
flow
}));
}
}
if(found_path){
// printf("Found path\n");
ans_cnt++;
int cur_row = numRows+1;
int cur_col = numCols+1;
int cur_on_top = 0;
while(cur_row || cur_col || cur_on_top){
int pre_row = pred[cur_row][cur_col][cur_on_top].second.first;
int pre_col = pred[cur_row][cur_col][cur_on_top].second.second;
int pre_on_top = pred[cur_row][cur_col][cur_on_top].first;
int dir = get_dir(cur_row, cur_col, pre_row, pre_col);
int from_dir = (dir+2) % 4;
if(cur_row == pre_row && cur_col == pre_col){
from_dir = dir;
}
// printf("Got (%d,%d)_%d -> (%d,%d)_%d\n", pre_row, pre_col, pre_on_top, cur_row, cur_col, cur_on_top);
if(cur_row == numRows +1 && cur_col == numCols+1){
sink_res[pre_row][pre_col] -= chosen_flow;
} else if (!pre_row && !pre_col){
src_res[cur_row][cur_col] -= chosen_flow;
} else {
res[pre_row][pre_col][pre_on_top][dir] -= chosen_flow;
res[cur_row][cur_col][cur_on_top][from_dir] += chosen_flow;
}
cur_row = pre_row;
cur_col = pre_col;
cur_on_top = pre_on_top;
}
} else {
break;
}
}
printf("%d\n", ans_cnt);
}
return 0;
}