Submission #306342
Source Code Expand
#include<queue>
#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
typedef long long LL;
typedef vector<int> VI;
#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i)
const int dx[] = {0,1,0,-1};
const int dy[] = {1,0,-1,0};
template<class T> inline void amin(T &a, const T &b) { if (a>b) a=b; }
template<class T> inline void amax(T &a, const T &b) { if (a<b) a=b; }
struct MinCostMaxFlow {
typedef int Flow;
typedef int Cost;
static const Flow FLOW_INF = 1<<29;
static const Cost COST_INF = 1<<29;
struct Edge {
int src, dst;
Cost cst;
Flow cap;
int rev;
int id;
bool operator<(const Edge&y) const {
return cst > y.cst;
}
};
typedef vector<vector<Edge> > Graph;
Graph G;
MinCostMaxFlow(int N) : G(N) {}
// directed
void add_edge(int u, int v, Cost x, Flow f, int id=-1) {
G[u].push_back((Edge){ u, v, x, f, (int)G[v].size(), id });
G[v].push_back((Edge){ v, u, -x, 0, (int)G[u].size()-1, -1 });
}
Flow flow;
Cost cost;
Flow solve(int s, int t, Flow limit = FLOW_INF) {
flow = 0;
cost = 0;
vector<Cost>len, h(G.size(), 0);
vector<int> prev, prev_num;
while (limit > 0) {
priority_queue<Edge> Q;
Q.push((Edge){-2, s, 0, 0, 0});
len.assign(G.size(), COST_INF);
prev.assign(G.size(), -1); prev_num.assign(G.size(), -1);
len[s] = 0;
while (!Q.empty()) {
Edge e = Q.top(); Q.pop();
for (int i=0; i<(int)G[e.dst].size(); i++) {
const Edge &f = G[e.dst][i];
if (f.cap > 0 && len[f.dst] > len[f.src] + f.cst + h[f.src] - h[f.dst]) {
len[f.dst] = len[f.src] + f.cst + h[f.src] - h[f.dst];
Q.push((Edge){ f.src, f.dst, len[f.dst], 0, 0 });
prev[f.dst] = f.src; prev_num[f.dst] = i;
}
}
}
if (prev[t] == -1) return flow;
for (int i=0; i<(int)G.size(); i++) h[i] += len[i];
Flow f = limit;
for (int v=t; v!=s; v=prev[v])
f = min(f, G[prev[v]][prev_num[v]].cap);
for (int v=t; v!=s; v=prev[v]) {
Edge &e = G[prev[v]][prev_num[v]];
e.cap -= f;
G[e.dst][e.rev].cap += f;
}
limit -= f;
flow += f;
cost += f * h[t];
}
return flow;
}
};
const MinCostMaxFlow::Flow MinCostMaxFlow::FLOW_INF;
const MinCostMaxFlow::Cost MinCostMaxFlow::COST_INF;
int H, W;
char F[55][55];
int get(int y, int x, bool out) {
return 2 * (y * W + x) + (int)(out);
}
pair<int, int>dec(int n) { // (y, x)
return make_pair(n / 2 / W, n / 2 % W);
}
int L[55][55];
void solve() {
int sx, sy;
REP (i, H) REP (j, W) {
if (F[i][j] == 'S') {
sx = j;
sy = i;
}
}
memset(L, 0x3f, sizeof L);
queue<int> qu;
qu.push(sx); qu.push(sy);
L[sy][sx] = 0;
while (qu.size()) {
int x = qu.front(); qu.pop();
int y = qu.front(); qu.pop();
REP (d, 4) {
int xx = x + dx[d];
int yy = y + dy[d];
if (0 <= xx && xx < W && 0 <= yy && yy < H &&
F[yy][xx] != '#' &&
L[yy][xx] > L[y][x] + 1) {
L[yy][xx] = L[y][x] + 1;
qu.push(xx);
qu.push(yy);
}
}
}
}
int main() {
scanf("%d%d", &H, &W);
REP (i, H) scanf("%s", F[i]);
solve();
int cost = 0;
REP (i, H) REP (j, W) {
if (F[i][j] == 'A' || F[i][j] == 'B') cost += L[i][j];
}
int source = H*W*2, sink = source + 1;
MinCostMaxFlow X(sink+1);
REP (i, H) {
REP (j, W) {
int v = get(i, j, 0);
int w = get(i, j, 1);
if (F[i][j] == 'A' || F[i][j] == 'B') {
X.add_edge(v, w, 0, 1);
X.add_edge(w, sink, 0, 1);
} else if (F[i][j] == 'S') {
X.add_edge(v, w, 0, 2);
X.add_edge(source, v, 0, 2);
} else if (F[i][j] != '#') {
X.add_edge(v, w, 0, 1);
}
if (i) X.add_edge(w, get(i-1, j, 0), 1, 1, 1);
if (i<H-1) X.add_edge(w, get(i+1, j, 0), 1, 1, 1);
if (j) X.add_edge(w, get(i, j-1, 0), 1, 1, 1);
if (j<W-1) X.add_edge(w, get(i, j+1, 0), 1, 1);
}
}
X.solve(source, sink);
if (X.flow != 2 || X.cost != cost) {
puts("NA");
return 0;
}
REP (i, X.G.size()) {
vector<MinCostMaxFlow::Edge> &v = X.G[i];
REP (j, v.size()) {
if (v[j].src < source && v[j].dst < source && v[j].cap && v[j].dst < v[j].src) {
int y = dec(v[j].src).first;
int x = dec(v[j].src).second;
int y2 = dec(v[j].dst).first;
int x2 = dec(v[j].dst).second;
if (x == x2 && y == y2) {
if (F[y][x] == '.') F[y][x] = 'a';
}
}
if (i == sink) {
int y = dec(v[j].dst).first;
int x = dec(v[j].dst).second;
if (F[y][x] == 'A') {
v[j].cap = 0;
}
}
}
}
X.solve(sink, source);
REP (i, X.G.size()) {
vector<MinCostMaxFlow::Edge> v = X.G[i];
REP (j, v.size()) {
if (v[j].src < source && v[j].dst < source && v[j].cap && v[j].dst > v[j].src) {
int y = dec(v[j].src).first;
int x = dec(v[j].src).second;
int y2 = dec(v[j].dst).first;
int x2 = dec(v[j].dst).second;
if (x == x2 && y == y2) {
if (F[y][x] == 'a') F[y][x] = 'b';
}
}
}
}
REP (i, H) puts(F[i]);
return 0;
}
Submission Info
Submission Time
2014-12-21 17:03:17+0900
Task
D - Maze
User
natsugiri
Language
C++ (G++ 4.6.4)
Score
100
Code Size
5318 Byte
Status
AC
Exec Time
37 ms
Memory
2332 KB
Compile Error
./Main.cpp: In function ‘int main()’:
./Main.cpp:130:26: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
./Main.cpp:131:33: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
Judge Result
Set Name
all
Score / Max Score
100 / 100
Status
Set Name
Test Cases
all
manual_j1.txt, manual_j10.txt, manual_j11.txt, manual_j12.txt, manual_j13.txt, manual_j14.txt, manual_j15.txt, manual_j2.txt, manual_j3.txt, manual_j4.txt, manual_j5.txt, manual_j6.txt, manual_j7.txt, manual_j8.txt, manual_j9.txt, manual_k1.txt, manual_k2.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_max_01.txt, random_max_02.txt, random_max_03.txt, random_max_04.txt, random_max_05.txt, random_max_06.txt, random_max_07.txt, random_max_08.txt, random_max_09.txt, random_max_10.txt, sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt, white_01.txt, white_02.txt, white_03.txt, white_04.txt
Case Name
Status
Exec Time
Memory
manual_j1.txt
AC
27 ms
1104 KB
manual_j10.txt
AC
26 ms
1108 KB
manual_j11.txt
AC
34 ms
2332 KB
manual_j12.txt
AC
27 ms
1044 KB
manual_j13.txt
AC
27 ms
1048 KB
manual_j14.txt
AC
28 ms
1044 KB
manual_j15.txt
AC
28 ms
1240 KB
manual_j2.txt
AC
35 ms
2324 KB
manual_j3.txt
AC
26 ms
1048 KB
manual_j4.txt
AC
27 ms
1048 KB
manual_j5.txt
AC
27 ms
1048 KB
manual_j6.txt
AC
28 ms
1052 KB
manual_j7.txt
AC
28 ms
1048 KB
manual_j8.txt
AC
27 ms
1048 KB
manual_j9.txt
AC
27 ms
1044 KB
manual_k1.txt
AC
37 ms
2268 KB
manual_k2.txt
AC
32 ms
2200 KB
random_01.txt
AC
31 ms
2196 KB
random_02.txt
AC
33 ms
2196 KB
random_03.txt
AC
32 ms
2072 KB
random_04.txt
AC
33 ms
2072 KB
random_05.txt
AC
30 ms
1684 KB
random_06.txt
AC
29 ms
1688 KB
random_max_01.txt
AC
36 ms
2324 KB
random_max_02.txt
AC
34 ms
2324 KB
random_max_03.txt
AC
31 ms
2324 KB
random_max_04.txt
AC
33 ms
2196 KB
random_max_05.txt
AC
37 ms
2200 KB
random_max_06.txt
AC
37 ms
2200 KB
random_max_07.txt
AC
33 ms
2200 KB
random_max_08.txt
AC
33 ms
2192 KB
random_max_09.txt
AC
32 ms
2076 KB
random_max_10.txt
AC
32 ms
2068 KB
sample_01.txt
AC
26 ms
1048 KB
sample_02.txt
AC
26 ms
1048 KB
sample_03.txt
AC
26 ms
1052 KB
sample_04.txt
AC
26 ms
1044 KB
white_01.txt
AC
34 ms
2328 KB
white_02.txt
AC
34 ms
2324 KB
white_03.txt
AC
33 ms
2324 KB
white_04.txt
AC
33 ms
2324 KB