Submission #3425454


Source Code Expand

#include <cstdio>
#include <vector>
#include <algorithm>
#include <functional>
#define repeat(i,n) for (int i = 0; (i) < int(n); ++(i))
#define _CRT_SECURE_NO_WARNIGS
#pragma warning (disable:4996)
using namespace std;
template <typename X, typename T> auto vectors(X x, T a) { return vector<T>(x, a); }
template <typename X, typename Y, typename Z, typename... Zs> auto vectors(X x, Y y, Z z, Zs... zs) { auto cont = vectors(y, z, zs...); return vector<decltype(cont)>(x, cont); }

struct edge_t { int to, cap, rev; };
int maximum_flow_destructive(int s, int t, vector<vector<edge_t> > & g) { // ford fulkerson, O(EF)
	int n = g.size();
	vector<bool> used(n);
	function<int(int, int)> dfs = [&](int i, int f) {
		if (i == t) return f;
		used[i] = true;
		for (edge_t & e : g[i]) {
			if (used[e.to] or e.cap <= 0) continue;
			int nf = dfs(e.to, min(f, e.cap));
			if (nf > 0) {
				e.cap -= nf;
				g[e.to][e.rev].cap += nf;
				return nf;
			}
		}
		return 0;
	};
	int result = 0;
	while (true) {
		used.clear(); used.resize(n);
		int f = dfs(s, numeric_limits<int>::max());
		if (f == 0) break;
		result += f;
	}
	return result;
}
void add_edge(vector<vector<edge_t> > & g, int from, int to, int cap) {
	g[from].push_back(edge_t{ to, cap, int(g[to].size()) });
	g[to].push_back(edge_t { from, 0, int(g[from].size() - 1) });
}

int main() {
	// input
	int h, w; scanf("%d%d", &h, &w);
	int inf = 100000000;
	vector<vector<char> > f = vectors(h, w, char());
	repeat(y, h) repeat(x, w) scanf(" %c", &f[y][x]);
	// maximum flow
	const int src = 0;//S
	const int dst = 1;//T
	//f[y][x]に対応する 行 列を取得する関数
	auto node_y = [&](int y) { return 2 + y; };
	auto node_x = [&](int x) { return 2 + h + x; };
	vector<vector<edge_t> > g(2 + h + w);//頂点0がS 頂点番号1がT あとはH0 H1 H2....W0 W1 W2....
	repeat(y, h) repeat(x, w) {
		if (f[y][x] == 'S') {
			add_edge(g, src, node_y(y), inf);
			add_edge(g, src, node_x(x), inf);
		}
		else if (f[y][x] == 'T') {
			add_edge(g, node_y(y), dst, inf);
			add_edge(g, node_x(x), dst, inf);
		}
		if (f[y][x] == 'o') {
			add_edge(g, node_y(y), node_x(x), 1);
			add_edge(g, node_x(x), node_y(y), 1);
		}
	}
	int flow = maximum_flow_destructive(src, dst, g);
	// output
	if (flow >= inf) flow = -1;
	printf("%d\n", flow);
	return 0;
}

Submission Info

Submission Time
Task F - Lotus Leaves
User doikimihiro
Language C++14 (GCC 5.4.1)
Score 800
Code Size 2385 Byte
Status AC
Exec Time 10 ms
Memory 896 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:46:33: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  int h, w; scanf("%d%d", &h, &w);
                                 ^
./Main.cpp:49:50: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  repeat(y, h) repeat(x, w) scanf(" %c", &f[y][x]);
                                                  ^

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 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 2 ms 256 KB
1_05.txt AC 2 ms 512 KB
1_06.txt AC 10 ms 896 KB
1_07.txt AC 2 ms 256 KB
1_08.txt AC 2 ms 512 KB
1_09.txt AC 6 ms 896 KB
1_10.txt AC 2 ms 384 KB
1_11.txt AC 2 ms 384 KB
1_12.txt AC 2 ms 256 KB
1_13.txt AC 2 ms 512 KB
1_14.txt AC 2 ms 512 KB
1_15.txt AC 2 ms 512 KB
1_16.txt AC 2 ms 384 KB
1_17.txt AC 2 ms 384 KB
1_18.txt AC 2 ms 384 KB
1_19.txt AC 3 ms 384 KB
1_20.txt AC 2 ms 512 KB
1_21.txt AC 2 ms 256 KB
1_22.txt AC 2 ms 512 KB
1_23.txt AC 2 ms 512 KB
1_24.txt AC 1 ms 256 KB
1_25.txt AC 2 ms 512 KB
1_26.txt AC 2 ms 512 KB
1_27.txt AC 2 ms 384 KB
1_28.txt AC 2 ms 384 KB
1_29.txt AC 2 ms 384 KB
1_30.txt AC 2 ms 384 KB
1_31.txt AC 2 ms 640 KB
1_32.txt AC 4 ms 512 KB
1_33.txt AC 1 ms 256 KB
1_34.txt AC 2 ms 512 KB
1_35.txt AC 3 ms 512 KB
1_36.txt AC 2 ms 512 KB
1_37.txt AC 3 ms 512 KB
1_38.txt AC 2 ms 512 KB
1_39.txt AC 3 ms 640 KB
1_40.txt AC 2 ms 256 KB
1_41.txt AC 3 ms 640 KB
1_42.txt AC 1 ms 256 KB
1_43.txt AC 2 ms 384 KB
1_44.txt AC 1 ms 256 KB
1_45.txt AC 2 ms 384 KB
1_46.txt AC 2 ms 384 KB
1_47.txt AC 1 ms 256 KB
1_48.txt AC 2 ms 384 KB
1_49.txt AC 2 ms 512 KB