Submission #1867935
Source Code Expand
#include <bits/stdc++.h> using namespace std; const int inf = 1e9; struct MaxFlow { struct edge { int u; int v; int c; int f; edge(int u=0, int v=0, int c=0, int f=0): u(u), v(v), c(c), f(f) {} }; int n, s, t; // nNode, source, sink vector< vector<int> > G; vector<edge> e; MaxFlow(int n=0, int s=0, int t=0): n(n), s(s), t(t) { G.assign(n + 1, vector<int>()); } void add(int u, int v, int c) { G[u].push_back(e.size()); e.push_back(edge(u, v, c, 0)); G[v].push_back(e.size()); e.push_back(edge(v, u, 0, 0)); } vector<int> d; // dist vector<int> ptr; // pointer bool bfs() { d.assign(n + 1, -1); ptr.assign(n + 1, 0); queue <int> q; q.push(s); d[s] = 0; while(!q.empty()) { int u = q.front(); q.pop(); for (int id : G[u]) if (d[e[id].v] == -1 && e[id].c > e[id].f) { d[e[id].v] = d[u] + 1; q.push(e[id].v); } } return d[t] != -1; } int dfs(int u, int fl) { if (!fl || u == t) return fl; for (int &i = ptr[u]; i < G[u].size(); ++i) { int id = G[u][i]; if (e[id].f >= e[id].c || d[e[id].v] != d[u] + 1) continue; int nxt = dfs(e[id].v, min(fl, e[id].c - e[id].f)); if (nxt) { e[id].f += nxt; e[id ^ 1].f -= nxt; return nxt; } } return 0; } int maxFlow() { int ans = 0; while(bfs()) { while(int cur = dfs(s, inf)) ans += cur; } return ans; } } mf; const int N = 105; int H, W; int st, en; char a[N][N]; vector < pair<int,int> > cells; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> H >> W; for (int i = 1; i <= H; ++i) { for (int j = 1; j <= W; ++j) { cin >> a[i][j]; if (a[i][j] != '.') cells.push_back(make_pair(i,j)); } } for (int i = 0; i < cells.size(); ++i) { int x = cells[i].first, y = cells[i].second; if (a[x][y] == 'S') st = i; else if (a[x][y] == 'T') en = i; } // create Max Flow mf = MaxFlow(cells.size() * 2 + 10, 2 * st + 1, 2 * en); for (int i = 0; i < cells.size(); ++i) { int x = cells[i].first, y = cells[i].second; mf.add(2 * i, 2 * i + 1, 1); for (int j = 0; j < cells.size(); ++j) if (j != i) { if (cells[i].first == cells[j].first || cells[i].second == cells[j].second) { mf.add(2 * i + 1, 2 * j, inf); } } } // find mincut int ans = mf.maxFlow(); if (ans >= inf) printf("-1\n"); else printf("%d\n", ans); }
Submission Info
Submission Time | |
---|---|
Task | F - Lotus Leaves |
User | cheater2k |
Language | C++14 (GCC 5.4.1) |
Score | 800 |
Code Size | 2390 Byte |
Status | AC |
Exec Time | 389 ms |
Memory | 85696 KB |
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 | 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 | 1 ms | 256 KB |
1_05.txt | AC | 67 ms | 21732 KB |
1_06.txt | AC | 362 ms | 85312 KB |
1_07.txt | AC | 1 ms | 256 KB |
1_08.txt | AC | 78 ms | 21352 KB |
1_09.txt | AC | 389 ms | 85696 KB |
1_10.txt | AC | 10 ms | 2804 KB |
1_11.txt | AC | 18 ms | 6896 KB |
1_12.txt | AC | 1 ms | 256 KB |
1_13.txt | AC | 33 ms | 11244 KB |
1_14.txt | AC | 43 ms | 11980 KB |
1_15.txt | AC | 50 ms | 22608 KB |
1_16.txt | AC | 10 ms | 2804 KB |
1_17.txt | AC | 15 ms | 6768 KB |
1_18.txt | AC | 7 ms | 1588 KB |
1_19.txt | AC | 33 ms | 11816 KB |
1_20.txt | AC | 47 ms | 22208 KB |
1_21.txt | AC | 3 ms | 640 KB |
1_22.txt | AC | 40 ms | 12232 KB |
1_23.txt | AC | 37 ms | 10568 KB |
1_24.txt | AC | 3 ms | 1084 KB |
1_25.txt | AC | 47 ms | 20408 KB |
1_26.txt | AC | 38 ms | 20796 KB |
1_27.txt | AC | 16 ms | 7280 KB |
1_28.txt | AC | 11 ms | 2804 KB |
1_29.txt | AC | 25 ms | 5996 KB |
1_30.txt | AC | 21 ms | 10988 KB |
1_31.txt | AC | 58 ms | 21352 KB |
1_32.txt | AC | 77 ms | 21564 KB |
1_33.txt | AC | 1 ms | 384 KB |
1_34.txt | AC | 36 ms | 11328 KB |
1_35.txt | AC | 42 ms | 11428 KB |
1_36.txt | AC | 46 ms | 22352 KB |
1_37.txt | AC | 64 ms | 21444 KB |
1_38.txt | AC | 29 ms | 10800 KB |
1_39.txt | AC | 76 ms | 22340 KB |
1_40.txt | AC | 4 ms | 1084 KB |
1_41.txt | AC | 62 ms | 22088 KB |
1_42.txt | AC | 1 ms | 384 KB |
1_43.txt | AC | 19 ms | 6896 KB |
1_44.txt | AC | 1 ms | 256 KB |
1_45.txt | AC | 11 ms | 2804 KB |
1_46.txt | AC | 6 ms | 1592 KB |
1_47.txt | AC | 1 ms | 256 KB |
1_48.txt | AC | 23 ms | 7152 KB |
1_49.txt | AC | 34 ms | 12092 KB |