Submission #2575256
Source Code Expand
#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#define INF 10000000000000000LL
using namespace std;
typedef long long int ll;
struct edge {int to; long long int cap; int rev;} ;
vector<edge> G[20002];
int level[20002];
int iter[20002];
void add_edge(int from, int to, long long int cap){
edge e;
e.to=to, e.cap=cap, e.rev=G[to].size();
G[from].push_back(e);
e.to=from, e.cap=0, e.rev=G[from].size()-1;
G[to].push_back(e);
}
void bfs(int s){
memset(level, -1, sizeof(level));
queue<int> que;
level[s]=0;
que.push(s);
while(!que.empty()){
int v=que.front();
que.pop();
for(int i=0; i<G[v].size(); i++){
edge e=G[v][i];
if(e.cap>0 && level[e.to]<0){
level[e.to]=level[v]+1;
que.push(e.to);
}
}
}
}
long long int dfs(int v, int t, long long int f){
if(v==t) return f;
for(int &i=iter[v]; i<G[v].size(); i++){
edge &e=G[v][i];
if(e.cap>0 && level[v]<level[e.to]){
long long int d=dfs(e.to, t, min(f, e.cap));
if(d>0){
e.cap-=d;
G[e.to][e.rev].cap+=d;
return d;
}
}
}
return 0;
}
long long int max_flow(int s, int t){
long long int flow=0;
while(1){
bfs(s);
if(level[t]<0) return flow;
memset(iter, 0, sizeof(iter));
long long int f;
while((f=dfs(s, t, INF))>0){
flow+=f;
}
}
}
int main()
{
int h, w;
scanf("%d %d", &h, &w);
char a[100][100];
for(int i=0; i<h; i++){
scanf("%s", a[i]);
}
int s, t, xs, ys, xt, yt;
for(int i=0; i<h; i++){
for(int j=0; j<w; j++){
if(a[i][j]=='S') s=2*(i*w+j)+1, xs=i, ys=j;
if(a[i][j]=='T') t=2*(i*w+j), xt=i, yt=j;
if(a[i][j]=='o' || a[i][j]=='S'){
if(a[i][j]=='o') add_edge(2*(i*w+j), 2*(i*w+j)+1, 1);
for(int k=0; k<w; k++){
if(k!=j && (a[i][k]=='o' || a[i][k]=='T')){
add_edge(2*(i*w+j)+1, 2*(i*w+k), 1);
}
}
for(int k=0; k<h; k++){
if(k!=i && (a[k][j]=='o' || a[k][j]=='T')){
add_edge(2*(i*w+j)+1, 2*(k*w+j), 1);
}
}
}
}
}
if(xs==xt || ys==yt){
printf("%d\n", -1);
return 0;
}
printf("%lld\n", max_flow(s, t));
return 0;
}
Submission Info
Submission Time |
|
Task |
F - Lotus Leaves |
User |
chocorusk |
Language |
C++14 (GCC 5.4.1) |
Score |
800 |
Code Size |
2335 Byte |
Status |
AC |
Exec Time |
216 ms |
Memory |
124416 KB |
Compile Error
./Main.cpp: In function ‘int main()’:
./Main.cpp:83:24: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &h, &w);
^
./Main.cpp:86:20: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%s", a[i]);
^
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 |
2 ms |
896 KB |
0_01.txt |
AC |
1 ms |
768 KB |
0_02.txt |
AC |
1 ms |
768 KB |
0_03.txt |
AC |
2 ms |
896 KB |
1_00.txt |
AC |
1 ms |
768 KB |
1_01.txt |
AC |
1 ms |
768 KB |
1_02.txt |
AC |
1 ms |
768 KB |
1_03.txt |
AC |
1 ms |
896 KB |
1_04.txt |
AC |
2 ms |
768 KB |
1_05.txt |
AC |
43 ms |
32384 KB |
1_06.txt |
AC |
216 ms |
124416 KB |
1_07.txt |
AC |
1 ms |
768 KB |
1_08.txt |
AC |
39 ms |
30720 KB |
1_09.txt |
AC |
162 ms |
124288 KB |
1_10.txt |
AC |
8 ms |
3712 KB |
1_11.txt |
AC |
13 ms |
6656 KB |
1_12.txt |
AC |
2 ms |
896 KB |
1_13.txt |
AC |
26 ms |
16384 KB |
1_14.txt |
AC |
31 ms |
16640 KB |
1_15.txt |
AC |
35 ms |
24192 KB |
1_16.txt |
AC |
8 ms |
3840 KB |
1_17.txt |
AC |
11 ms |
7424 KB |
1_18.txt |
AC |
6 ms |
2688 KB |
1_19.txt |
AC |
23 ms |
10880 KB |
1_20.txt |
AC |
34 ms |
23808 KB |
1_21.txt |
AC |
3 ms |
1280 KB |
1_22.txt |
AC |
29 ms |
22656 KB |
1_23.txt |
AC |
27 ms |
15232 KB |
1_24.txt |
AC |
3 ms |
1664 KB |
1_25.txt |
AC |
31 ms |
25856 KB |
1_26.txt |
AC |
27 ms |
22400 KB |
1_27.txt |
AC |
11 ms |
5760 KB |
1_28.txt |
AC |
8 ms |
3456 KB |
1_29.txt |
AC |
18 ms |
8832 KB |
1_30.txt |
AC |
14 ms |
10624 KB |
1_31.txt |
AC |
42 ms |
26880 KB |
1_32.txt |
AC |
59 ms |
26112 KB |
1_33.txt |
AC |
2 ms |
896 KB |
1_34.txt |
AC |
26 ms |
16512 KB |
1_35.txt |
AC |
32 ms |
16512 KB |
1_36.txt |
AC |
33 ms |
22784 KB |
1_37.txt |
AC |
48 ms |
25600 KB |
1_38.txt |
AC |
21 ms |
17024 KB |
1_39.txt |
AC |
55 ms |
26496 KB |
1_40.txt |
AC |
3 ms |
1664 KB |
1_41.txt |
AC |
28 ms |
25344 KB |
1_42.txt |
AC |
2 ms |
768 KB |
1_43.txt |
AC |
10 ms |
7040 KB |
1_44.txt |
AC |
2 ms |
768 KB |
1_45.txt |
AC |
6 ms |
3712 KB |
1_46.txt |
AC |
4 ms |
2304 KB |
1_47.txt |
AC |
2 ms |
768 KB |
1_48.txt |
AC |
12 ms |
8192 KB |
1_49.txt |
AC |
18 ms |
14208 KB |