Submission #3426835


Source Code Expand

#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = (ll)(1e+9) + 7;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-12;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
typedef pair<ld, ld> LDP;
#include<cstring>
struct edge { int to, cap, rev; };
vector<edge> G[100000];
bool used[100000];
void add_edge(int from, int to, int cap) {
	G[from].push_back(edge{ to, cap, (int)G[to].size() });
	G[to].push_back(edge{ from, 0, (int)G[from].size() - 1 });
}
int dfs(int v, int t, int f) {
	if (v == t)return f;
	used[v] = true;
	for (int i = 0; i < (int)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));
			if (d > 0) {
				e.cap -= d;
				G[e.to][e.rev].cap += d;
				return d;
			}
		}
	}
	return 0;
}
int max_flow(int s, int t) {
	int flow = 0;
	for (;;) {
		memset(used, 0, sizeof(used));
		int f = dfs(s, t, mod);
		if (f == 0)return flow;
		flow += f;
	}
}
char lake[100][100];
int main() {
	int h, w; cin >> h >> w;
	int sta, goal;
	rep(i, h) {
		rep(j, w) {
			cin >> lake[i][j];
			if (lake[i][j] == 'S') {
				sta = i * w + j;
			}
			else if (lake[i][j] == 'T') {
				goal = i * w + j;
			}
		}
	}
	int t = h * w;
	rep(i, h) {
		rep(j, w) {
			add_edge(t + i * w + j, i*w + j, 1);
			if (lake[i][j] == '.')continue;
			rep(k, w) {
				if (lake[i][k] == '.')continue;
				add_edge(i*w + j, t+i*w + k, mod);
			}
			rep(k, h) {
				if (lake[k][j] == '.')continue;
				add_edge(i*w + j, t+k*w + j, mod);
			}
		}
	}
	int out = max_flow(sta, t + goal);
	if (out >= (int)mod)cout << -1 << endl;
	else cout << out << endl;
	return 0;
}

Submission Info

Submission Time
Task F - Lotus Leaves
User heno239
Language C++14 (GCC 5.4.1)
Score 800
Code Size 2410 Byte
Status AC
Exec Time 1448 ms
Memory 68096 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 800 / 800
Status
AC × 4
AC × 54
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 2688 KB
0_01.txt AC 2 ms 2688 KB
0_02.txt AC 2 ms 2688 KB
0_03.txt AC 2 ms 2688 KB
1_00.txt AC 2 ms 2688 KB
1_01.txt AC 2 ms 2688 KB
1_02.txt AC 2 ms 2688 KB
1_03.txt AC 2 ms 2688 KB
1_04.txt AC 4 ms 3328 KB
1_05.txt AC 35 ms 19200 KB
1_06.txt AC 1448 ms 68096 KB
1_07.txt AC 4 ms 3328 KB
1_08.txt AC 34 ms 18560 KB
1_09.txt AC 1423 ms 66048 KB
1_10.txt AC 8 ms 4992 KB
1_11.txt AC 21 ms 6144 KB
1_12.txt AC 4 ms 3328 KB
1_13.txt AC 66 ms 11520 KB
1_14.txt AC 60 ms 12160 KB
1_15.txt AC 29 ms 15488 KB
1_16.txt AC 12 ms 4864 KB
1_17.txt AC 11 ms 6528 KB
1_18.txt AC 9 ms 4224 KB
1_19.txt AC 50 ms 8960 KB
1_20.txt AC 25 ms 14976 KB
1_21.txt AC 5 ms 3456 KB
1_22.txt AC 26 ms 14720 KB
1_23.txt AC 41 ms 11392 KB
1_24.txt AC 5 ms 3584 KB
1_25.txt AC 28 ms 16000 KB
1_26.txt AC 24 ms 14208 KB
1_27.txt AC 14 ms 6016 KB
1_28.txt AC 9 ms 4864 KB
1_29.txt AC 25 ms 7552 KB
1_30.txt AC 14 ms 8448 KB
1_31.txt AC 40 ms 16640 KB
1_32.txt AC 148 ms 16256 KB
1_33.txt AC 4 ms 3328 KB
1_34.txt AC 21 ms 11776 KB
1_35.txt AC 60 ms 11520 KB
1_36.txt AC 25 ms 14464 KB
1_37.txt AC 122 ms 16000 KB
1_38.txt AC 20 ms 11648 KB
1_39.txt AC 126 ms 16640 KB
1_40.txt AC 6 ms 3840 KB
1_41.txt AC 38 ms 15872 KB
1_42.txt AC 4 ms 3200 KB
1_43.txt AC 20 ms 6528 KB
1_44.txt AC 4 ms 3200 KB
1_45.txt AC 12 ms 4992 KB
1_46.txt AC 8 ms 4096 KB
1_47.txt AC 4 ms 3200 KB
1_48.txt AC 26 ms 7168 KB
1_49.txt AC 18 ms 10752 KB