Submission #1297740
Source Code Expand
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
//---------------------------------------------------------------------------------------------------
template<class V> struct MaxFlow {
struct edge { int to, reve; V cap; edge(int t, int r, V c) : to(t), reve(r), cap(c) {} };
int MV; vector<vector<edge>> E; vector<int> itr, lev;
MaxFlow(int n) { init(n); } MaxFlow() { }
void init(int n) { MV = n; itr = vector<int>(MV), lev = vector<int>(MV); E = vector<vector<edge>>(MV); }
void add_edge(int x, int y, V cap, bool undir = false) {
E[x].push_back(edge(y, (int)E[y].size(), cap));
E[y].push_back(edge(x, (int)E[x].size() - 1, undir ? cap : 0));
}
void bfs(int cur) {
rep(i, 0, MV) lev[i] = -1; queue<int> q; lev[cur] = 0; q.push(cur);
while (q.size()) {
int v = q.front(); q.pop();
for (auto e : E[v]) if (e.cap>0 && lev[e.to]<0) lev[e.to] = lev[v] + 1, q.push(e.to);
}
}
V dfs(int from, int to, V cf) {
if (from == to) return cf;
for (; itr[from]<E[from].size(); itr[from]++) {
edge* e = &E[from][itr[from]]; if (e->cap>0 && lev[from]<lev[e->to]) {
V f = dfs(e->to, to, min(cf, e->cap));
if (f>0) { e->cap -= f; E[e->to][e->reve].cap += f; return f; }
}
} return 0;
}
V maxflow(int from, int to) {
V fl = 0, tf;
while (1) {
bfs(from); if (lev[to]<0) return fl;
rep(i, 0, MV) itr[i] = 0; while ((tf = dfs(from, to, numeric_limits<V>::max()))>0) fl += tf;
}
}
};
/*---------------------------------------------------------------------------------------------------
∧_∧
∧_∧ (´<_` ) Welcome to My Coding Space!
( ´_ゝ`) / ⌒i
/ \ | |
/ / ̄ ̄ ̄ ̄/ |
__(__ニつ/ _/ .| .|____
\/____/ (u ⊃
---------------------------------------------------------------------------------------------------*/
int H, W;
string A[100];
//---------------------------------------------------------------------------------------------------
vector<int> tate[101], yoko[101];
int solve() {
int sx, sy, tx, ty;
rep(y, 0, H) rep(x, 0, W) {
if (A[y][x] == 'S') sx = x, sy = y, tate[x].push_back(y), yoko[y].push_back(x);
if (A[y][x] == 'T') tx = x, ty = y, tate[x].push_back(y), yoko[y].push_back(x);
if (A[y][x] == 'o') tate[x].push_back(y), yoko[y].push_back(x);
}
if (sx == tx) return -1;
if (sy == ty) return -1;
MaxFlow<int> mf(H * W * 2);
rep(y, 0, H) if(2 <= yoko[y].size()){
rep(i, 0, yoko[y].size()) rep(j, 0, yoko[y].size()) if(i != j){
mf.add_edge(y * W + yoko[y][i], y * W + yoko[y][j] + H * W, 1);
}
}
rep(x, 0, W) if (2 <= tate[x].size()) {
rep(i, 0, tate[x].size()) rep(j, 0, tate[x].size()) if(i != j){
mf.add_edge(tate[x][i] * W + x, tate[x][j] * W + x + H * W, 1);
}
}
rep(x, 0, W) rep(y, 0, H) mf.add_edge(y * W + x + H*W, y * W + x, 1);
return mf.maxflow(sy * W + sx, ty * W + tx + W * H);
}
//---------------------------------------------------------------------------------------------------
void _main() {
cin >> H >> W;
rep(y, 0, H) cin >> A[y];
cout << solve() << endl;
}
Submission Info
Submission Time |
|
Task |
F - Lotus Leaves |
User |
hamayanhamayan |
Language |
C++14 (GCC 5.4.1) |
Score |
800 |
Code Size |
3683 Byte |
Status |
AC |
Exec Time |
129 ms |
Memory |
61824 KB |
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
800 / 800 |
Status |
|
|
Set Name |
Test Cases |
Sample |
0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt |
All |
0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt, 1_18.txt, 1_19.txt, 1_20.txt, 1_21.txt, 1_22.txt, 1_23.txt, 1_24.txt, 1_25.txt, 1_26.txt, 1_27.txt, 1_28.txt, 1_29.txt, 1_30.txt, 1_31.txt, 1_32.txt, 1_33.txt, 1_34.txt, 1_35.txt, 1_36.txt, 1_37.txt, 1_38.txt, 1_39.txt, 1_40.txt, 1_41.txt, 1_42.txt, 1_43.txt, 1_44.txt, 1_45.txt, 1_46.txt, 1_47.txt, 1_48.txt, 1_49.txt |
Case Name |
Status |
Exec Time |
Memory |
0_00.txt |
AC |
1 ms |
256 KB |
0_01.txt |
AC |
1 ms |
256 KB |
0_02.txt |
AC |
1 ms |
256 KB |
0_03.txt |
AC |
1 ms |
256 KB |
1_00.txt |
AC |
1 ms |
256 KB |
1_01.txt |
AC |
1 ms |
256 KB |
1_02.txt |
AC |
1 ms |
256 KB |
1_03.txt |
AC |
1 ms |
256 KB |
1_04.txt |
AC |
3 ms |
1536 KB |
1_05.txt |
AC |
25 ms |
16640 KB |
1_06.txt |
AC |
129 ms |
61824 KB |
1_07.txt |
AC |
1 ms |
256 KB |
1_08.txt |
AC |
1 ms |
384 KB |
1_09.txt |
AC |
1 ms |
384 KB |
1_10.txt |
AC |
7 ms |
2688 KB |
1_11.txt |
AC |
10 ms |
3840 KB |
1_12.txt |
AC |
3 ms |
1408 KB |
1_13.txt |
AC |
18 ms |
8320 KB |
1_14.txt |
AC |
22 ms |
8960 KB |
1_15.txt |
AC |
22 ms |
12288 KB |
1_16.txt |
AC |
6 ms |
2432 KB |
1_17.txt |
AC |
8 ms |
4352 KB |
1_18.txt |
AC |
5 ms |
2048 KB |
1_19.txt |
AC |
16 ms |
6272 KB |
1_20.txt |
AC |
20 ms |
12032 KB |
1_21.txt |
AC |
3 ms |
1536 KB |
1_22.txt |
AC |
19 ms |
11520 KB |
1_23.txt |
AC |
20 ms |
8064 KB |
1_24.txt |
AC |
3 ms |
1536 KB |
1_25.txt |
AC |
19 ms |
13312 KB |
1_26.txt |
AC |
16 ms |
11392 KB |
1_27.txt |
AC |
9 ms |
3712 KB |
1_28.txt |
AC |
7 ms |
2688 KB |
1_29.txt |
AC |
13 ms |
5120 KB |
1_30.txt |
AC |
9 ms |
5760 KB |
1_31.txt |
AC |
26 ms |
13824 KB |
1_32.txt |
AC |
36 ms |
13312 KB |
1_33.txt |
AC |
3 ms |
1536 KB |
1_34.txt |
AC |
17 ms |
8832 KB |
1_35.txt |
AC |
22 ms |
8832 KB |
1_36.txt |
AC |
20 ms |
11520 KB |
1_37.txt |
AC |
29 ms |
12928 KB |
1_38.txt |
AC |
13 ms |
8960 KB |
1_39.txt |
AC |
37 ms |
13568 KB |
1_40.txt |
AC |
1 ms |
256 KB |
1_41.txt |
AC |
1 ms |
384 KB |
1_42.txt |
AC |
1 ms |
256 KB |
1_43.txt |
AC |
1 ms |
256 KB |
1_44.txt |
AC |
1 ms |
256 KB |
1_45.txt |
AC |
1 ms |
256 KB |
1_46.txt |
AC |
1 ms |
256 KB |
1_47.txt |
AC |
1 ms |
256 KB |
1_48.txt |
AC |
1 ms |
256 KB |
1_49.txt |
AC |
1 ms |
384 KB |