Submission #3422284
Source Code Expand
//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_6_A&lang=jp
#include<bits/stdc++.h>
#pragma warning(disable:4996)
#ifdef _MSC_VER
# define __builtin_popcount __popcnt
#endif
#define int long long
using namespace std;
using ll = long long;
using ld = long double;
const int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 };
inline void my_io() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
cout << fixed << setprecision(10);
}
int h, w;
vector<vector<int>>v;
int INF = 1000000;
struct edge {
int from, to, cap, rev;//revは逆辺のindex to:行き先,cap:容量,rev:逆(G[e.to][e.rev]で逆辺の構造体にアクセスできる。)
};
vector<vector<edge>>g;
vector<bool>used;
void add_edge(int from, int to, int cap) {
g[from].push_back(edge{ from, to, cap, (int)g[to].size() });//g[to].size()で次の行で取得される
g[to].push_back(edge{ to, from, 0, (int)g[from].size() - 1 });
}
int dfs(int v, int t, int f) {//vからtに流せる最大
if (v == t) {//終点までたどり着いたら
return f;
}
used[v] = true;
for (int i = 0; i < g[v].size(); i++) {
edge &e = g[v][i];//今見ている頂点から伸びている辺を参照している(コピーではない!!)
if (!used[e.to] && e.cap>0) {//その辺の行き先をまだ調べていなくて、辺の容量にも余裕がある
int d = dfs(e.to, t, min(f, e.cap));//capを超えない目一杯を流す
if (d > 0) {//流しているなら
e.cap -= d;//流した分capから引く
g[e.to][e.rev].cap += d;//流した分逆辺のcapを増やす
return d;
}
}
}
return 0;//何も流せなかった
}
int max_flow(int s, int t) {//sからtへの最大流
int flow = 0;
for (;;) {
for (int i = 0; i<used.size(); i++) {
used[i] = false;
}
int f = dfs(s, t, numeric_limits<signed>::max());
if (f == 0) {
return flow;//もう何も流せないので今まで流した量の和を返す
}
flow += f;
}
}
signed main(){
my_io();
cin >> h >> w;
v.resize(h);
g.resize(h*w);
used.resize(h*w);
int sx, sy, gx, gy;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
char ch;
cin >> ch;
v[i].push_back(ch);
if (ch == 'S') {
sx = i;
sy = j;
}
else if (ch == 'T') {
gx = i;
gy = j;
}
}
}
//横の辺を張る
for (int i = 0; i < h; i++) {
vector<int>vertex;//頂点を持っておくvector
for (int j = 0; j < w; j++) {
if (v[i][j] == 'S' || v[i][j] == 'o'||v[i][j]=='T') {
vertex.push_back(j);
}
}
for (int k = 0; k < vertex.size(); k++) {
for (int l = 0; l < vertex.size(); l++) {
if (k != l) {
if (v[i][vertex[k]] == 'S'&& v[i][vertex[l]] == 'T') {
add_edge(i*w + vertex[k], i*w + vertex[l], INF);
add_edge(i*w + vertex[l], i*w + vertex[k], INF);
}
else if(v[i][vertex[k]] == 'o' && v[i][vertex[l]] == 'o'|| v[i][vertex[k]] == 'S' && v[i][vertex[l]] == 'o'|| v[i][vertex[k]] == 'T' && v[i][vertex[l]] == 'o'){
add_edge(i*w + vertex[k], i*w + vertex[l], 1);
add_edge(i*w + vertex[l], i*w + vertex[k], 1);
}
}
}
}
}
//縦の辺を張る
for (int j = 0; j < w; j++) {
vector<int>vertex;
for (int i = 0; i < h; i++) {
if (v[i][j] == 'S' || v[i][j] == 'o'||v[i][j]=='T') {
vertex.push_back(i);
}
}
for (int k = 0; k < vertex.size(); k++) {
for (int l = 0; l < vertex.size(); l++) {
if (k != l) {
if (v[vertex[k]][j] == 'S'&& v[vertex[l]][j] == 'T') {
add_edge( vertex[k]*w+j,vertex[l]*w+j, INF);
add_edge(vertex[l] * w + j, vertex[k] * w + j, INF);
}
else if(v[vertex[k]][j] == 'o' && v[vertex[l]][j] == 'o' || v[vertex[k]][j] == 'S' && v[vertex[l]][j] == 'o' || v[vertex[k]][j] == 'T' && v[vertex[l]][j] == 'o'){
add_edge(vertex[k] * w + j, vertex[l] * w + j, 1);
add_edge(vertex[l] * w + j, vertex[k] * w + j, 1);
}
}
}
}
}
int ans = max_flow(sx*w + sy, gx*w + gy);
if (ans>=INF) {
cout << -1 << endl;
}
else cout << ans<< endl;
return 0;
}
/*
10 10
.o...o..o.
....o.....
....oo.oo.
..oooo..o.
....oo....
..o..o....
o..o....So
o....T....
....o.....
........oo
*/
//5
Submission Info
Submission Time |
|
Task |
F - Lotus Leaves |
User |
doikimihiro |
Language |
C++14 (GCC 5.4.1) |
Score |
0 |
Code Size |
4283 Byte |
Status |
WA |
Exec Time |
2127 ms |
Memory |
308224 KB |
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
0 / 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 |
WA |
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 |
2 ms |
640 KB |
1_05.txt |
AC |
77 ms |
81024 KB |
1_06.txt |
TLE |
2127 ms |
308096 KB |
1_07.txt |
AC |
2 ms |
640 KB |
1_08.txt |
AC |
93 ms |
78848 KB |
1_09.txt |
TLE |
2126 ms |
308224 KB |
1_10.txt |
WA |
13 ms |
7680 KB |
1_11.txt |
AC |
25 ms |
15488 KB |
1_12.txt |
AC |
2 ms |
640 KB |
1_13.txt |
AC |
100 ms |
34944 KB |
1_14.txt |
WA |
73 ms |
36224 KB |
1_15.txt |
WA |
152 ms |
55296 KB |
1_16.txt |
WA |
14 ms |
7936 KB |
1_17.txt |
AC |
17 ms |
17792 KB |
1_18.txt |
AC |
9 ms |
5248 KB |
1_19.txt |
AC |
58 ms |
24704 KB |
1_20.txt |
WA |
222 ms |
56704 KB |
1_21.txt |
AC |
3 ms |
1536 KB |
1_22.txt |
WA |
138 ms |
50432 KB |
1_23.txt |
WA |
85 ms |
32512 KB |
1_24.txt |
WA |
4 ms |
2560 KB |
1_25.txt |
AC |
60 ms |
61312 KB |
1_26.txt |
AC |
49 ms |
51584 KB |
1_27.txt |
WA |
25 ms |
13184 KB |
1_28.txt |
WA |
12 ms |
7168 KB |
1_29.txt |
WA |
50 ms |
20736 KB |
1_30.txt |
AC |
22 ms |
23040 KB |
1_31.txt |
WA |
226 ms |
63488 KB |
1_32.txt |
AC |
226 ms |
62080 KB |
1_33.txt |
AC |
2 ms |
640 KB |
1_34.txt |
WA |
92 ms |
35072 KB |
1_35.txt |
AC |
76 ms |
36224 KB |
1_36.txt |
WA |
138 ms |
51328 KB |
1_37.txt |
AC |
176 ms |
61696 KB |
1_38.txt |
AC |
35 ms |
36736 KB |
1_39.txt |
WA |
226 ms |
62464 KB |
1_40.txt |
AC |
5 ms |
2816 KB |
1_41.txt |
AC |
220 ms |
59136 KB |
1_42.txt |
AC |
2 ms |
640 KB |
1_43.txt |
AC |
37 ms |
16896 KB |
1_44.txt |
AC |
2 ms |
512 KB |
1_45.txt |
AC |
14 ms |
7936 KB |
1_46.txt |
AC |
8 ms |
4736 KB |
1_47.txt |
AC |
2 ms |
640 KB |
1_48.txt |
AC |
40 ms |
20096 KB |
1_49.txt |
AC |
51 ms |
30720 KB |