Submission #1298650


Source Code Expand

#include <stdio.h>
#include <vector>
#include <limits.h>
#include <queue>
#include <string.h>
#include <algorithm>
#include <functional>
using namespace std;

//total # of nodes
const int MAXN = 20005;

struct Maxflow {
	struct E {
		int x, inv, cap, flow;
		E(int t, int c, int z) : x(t), inv(z), cap(c), flow(0) {}
		int rest() const { return cap - flow; }
	};
	int V, d[MAXN], e_try[MAXN];
	vector <E> g[MAXN];
	Maxflow(int V) : V(V) {}
	void addedge(int a, int b, int a2b, int b2a = 0) {
		int as = g[a].size(), bs = g[b].size();
		g[a].push_back({ b, a2b, bs });
		g[b].push_back({ a, b2a, as });
	}
	bool bfs(int S, int T) {
		memset(d, -1, sizeof(d));
		queue <int> q; q.push(S);
		d[S] = 0;
		while (!q.empty()) {
			int x = q.front(); q.pop();
			for (auto &e : g[x]) {
				if (d[e.x] == -1 && e.rest() > 0) {
					d[e.x] = d[x] + 1;
					q.push(e.x);
				}
			}
		}
		return d[T] != -1;
	}
	int dfs(int x, int T, int flow) {
		if (x == T) return flow;
		for (int& i = e_try[x]; i < g[x].size(); i++) {
			E &e = g[x][i];
			if (e.rest() > 0 && d[e.x] == d[x] + 1) {
				int t = dfs(e.x, T, min(flow, e.rest()));
				if (t > 0) {
					E &re = g[e.x][e.inv];
					e.flow += t;
					re.flow = -e.flow;
					return t;
				}
			}
		}
		return 0;
	}
	int go(int S, int T) {
		int res = 0, t = 0;
		while (bfs(S, T)) {
			memset(e_try, 0, sizeof(e_try));
			do {
				t = dfs(S, T, numeric_limits<int>::max());
				res += t;
			} while (t);
		}
		return res;
	}
};
int N, M, sx, sy, tx, ty, n, c[105][105];
char S[105][105];
int main() {
	scanf("%d%d", &N, &M);
	for (int i = 1; i <= N; i++) {
		scanf("%s", S[i] + 1);
		for (int j = 1; j <= M; j++) {
			if (S[i][j] == 'S') sx = i, sy = j, c[i][j] = ++n;
			if (S[i][j] == 'T') tx = i, ty = j, c[i][j] = ++n;
			if (S[i][j] == 'o') c[i][j] = ++n;
		}
	}
	Maxflow f(2 * n + 1);
	if (sx == tx || sy == ty) puts("-1");
	else {
		for (int i = 1; i <= n; i++) {
			if (i == c[sx][sy] || i == c[tx][ty]) continue;
			f.addedge(2 * i - 1, 2 * i, 1, 0);
		}
		f.addedge(2 * c[sx][sy] - 1, 2 * c[sx][sy], n, 0);
		f.addedge(2 * c[tx][ty] - 1, 2 * c[tx][ty], n, 0);
		for (int i = 1; i <= N; i++) {
			for (int j = 1; j <= M; j++) {
				if (c[i][j]) {
					int x = c[i][j];
					for (int k = 1; k <= M; k++) {
						if (j == k) continue;
						if (c[i][k]) {
							int y = c[i][k];
							f.addedge(2 * x, 2 * y - 1, 1, 0);
						}
					}
					for (int k = 1; k <= N; k++) {
						if (i == k) continue;
						if (c[k][j]) {
							int y = c[k][j];
							f.addedge(2 * x, 2 * y - 1, 1, 0);
						}
					}
				}
			}
		}
		int res = f.go(2 * c[sx][sy] - 1, 2 * c[tx][ty]);
		printf("%d", res);
	}
}

Submission Info

Submission Time
Task F - Lotus Leaves
User hongjun7
Language C++14 (GCC 5.4.1)
Score 800
Code Size 2755 Byte
Status AC
Exec Time 178 ms
Memory 83072 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:73:23: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &N, &M);
                       ^
./Main.cpp:75:24: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   scanf("%s", S[i] + 1);
                        ^

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 1 ms 896 KB
0_01.txt AC 1 ms 768 KB
0_02.txt AC 1 ms 768 KB
0_03.txt AC 1 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 1 ms 768 KB
1_05.txt AC 34 ms 21888 KB
1_06.txt AC 178 ms 83072 KB
1_07.txt AC 1 ms 768 KB
1_08.txt AC 1 ms 768 KB
1_09.txt AC 1 ms 768 KB
1_10.txt AC 7 ms 2816 KB
1_11.txt AC 11 ms 4864 KB
1_12.txt AC 2 ms 896 KB
1_13.txt AC 23 ms 11392 KB
1_14.txt AC 27 ms 11520 KB
1_15.txt AC 29 ms 16512 KB
1_16.txt AC 7 ms 2944 KB
1_17.txt AC 9 ms 5376 KB
1_18.txt AC 5 ms 2176 KB
1_19.txt AC 20 ms 7680 KB
1_20.txt AC 26 ms 16256 KB
1_21.txt AC 3 ms 1152 KB
1_22.txt AC 24 ms 15488 KB
1_23.txt AC 24 ms 10624 KB
1_24.txt AC 3 ms 1408 KB
1_25.txt AC 25 ms 17536 KB
1_26.txt AC 21 ms 15232 KB
1_27.txt AC 10 ms 4224 KB
1_28.txt AC 7 ms 2688 KB
1_29.txt AC 15 ms 6272 KB
1_30.txt AC 12 ms 7424 KB
1_31.txt AC 34 ms 18304 KB
1_32.txt AC 48 ms 17792 KB
1_33.txt AC 2 ms 1024 KB
1_34.txt AC 22 ms 11392 KB
1_35.txt AC 28 ms 11392 KB
1_36.txt AC 26 ms 15616 KB
1_37.txt AC 39 ms 17408 KB
1_38.txt AC 17 ms 11648 KB
1_39.txt AC 48 ms 18048 KB
1_40.txt AC 1 ms 768 KB
1_41.txt AC 1 ms 768 KB
1_42.txt AC 1 ms 768 KB
1_43.txt AC 1 ms 768 KB
1_44.txt AC 1 ms 768 KB
1_45.txt AC 1 ms 768 KB
1_46.txt AC 1 ms 768 KB
1_47.txt AC 1 ms 768 KB
1_48.txt AC 1 ms 768 KB
1_49.txt AC 1 ms 768 KB