Submission #3430658


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;
template<class T> void assign(V<T>& v, int n, const T& a = T()) { v.assign(n, a); }
template<class T, class... U> void assign(V<T>& v, int n, const U&... u) { v.resize(n); for (auto&& i : v) assign(i, u...); }

constexpr int inf = 1e9;
template<class T> struct edge { int to; T cap; int rev; edge(int t, T c, int r) : to(t), cap(c), rev(r) {} };

void add_edge(int from, int to, auto cap, auto& g) {
  g[from].emplace_back(to, cap, g[to].size());
  g[to].emplace_back(from, 0, g[from].size() - 1);
}

template<class T> T dinic(int s, int t, VV< edge<T> > g) {
  int n = g.size();
  V<> dist, i;

  auto bfs = [&]() {
    dist.assign(n, -1);
    queue<int> q;
    dist[s] = 0; q.push(s);
    while (!q.empty()) {
      int v = q.front(); q.pop();
      for (auto&& e : g[v]) if (dist[e.to] == -1 and e.cap > 0) {
        dist[e.to] = dist[v] + 1; q.push(e.to);
      }
    }
  };

  auto dfs = [&](int v, T f, auto dfs) {
    if (v == s) return f;
    for (; i[v] < g[v].size(); i[v]++) {
      edge<T>& e = g[v][i[v]];
      if (dist[e.to] < dist[v] and g[e.to][e.rev].cap > 0) {
        T d = dfs(e.to, min(f, g[e.to][e.rev].cap), dfs);
        if (d > 0) {
          g[e.to][e.rev].cap -= d;
          e.cap += d;
          return d;
        }
      }
    }
    return (T) 0;
  };

  T res = 0;
  while (true) {
    bfs();
    if (dist[t] == -1) return res;
    i.assign(n, 0);
    while (true) {
      T f = dfs(t, inf, dfs);
      if (f == 0) break;
      res += f; 
    }
  }
}

int main() {
  cin.tie(NULL); ios::sync_with_stdio(false);
  int h, w; cin >> h >> w;
  V<string> a(h); for (int i = 0; i < h; i++) cin >> a[i];
  int s, t;
  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;
    if (a[i][j] == 'T') t = 2 * (i * w + j);
  }
  VV< edge<int> > g(2 * h * w);
  for (int i = 0; i < h; i++) {
    for (int jl = 0; jl < w; jl++) for (int jr = 0; jr < w; jr++) {
      if (jl != jr and a[i][jl] != '.' and a[i][jr] != '.') add_edge(2 * (i * w + jl) + 1, 2 * (i * w + jr), inf, g);
    }
  }
  for (int j = 0; j < w; j++) {
    for (int il = 0; il < h; il++) for (int ir = 0; ir < h; ir++) {
      if (il != ir and a[il][j] != '.' and a[ir][j] != '.') add_edge(2 * (il * w + j) + 1, 2 * (ir * w + j), inf, g);
    }
  }
  for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) add_edge(2 * (i * w + j), 2 * (i * w + j) + 1, 1, g);
  int res = dinic<int>(s, t, g);
  cout << (res >= inf ? -1 : res) << '\n';
}

Submission Info

Submission Time
Task F - Lotus Leaves
User risujiroh
Language C++14 (GCC 5.4.1)
Score 800
Code Size 2727 Byte
Status AC
Exec Time 162 ms
Memory 109056 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 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 7 ms 2560 KB
1_05.txt AC 36 ms 29184 KB
1_06.txt AC 150 ms 109056 KB
1_07.txt AC 7 ms 2688 KB
1_08.txt AC 40 ms 28032 KB
1_09.txt AC 162 ms 109056 KB
1_10.txt AC 11 ms 4608 KB
1_11.txt AC 13 ms 6528 KB
1_12.txt AC 7 ms 2304 KB
1_13.txt AC 21 ms 12800 KB
1_14.txt AC 27 ms 14592 KB
1_15.txt AC 30 ms 19840 KB
1_16.txt AC 9 ms 4096 KB
1_17.txt AC 13 ms 7424 KB
1_18.txt AC 9 ms 3456 KB
1_19.txt AC 21 ms 10752 KB
1_20.txt AC 27 ms 19840 KB
1_21.txt AC 7 ms 2560 KB
1_22.txt AC 27 ms 18304 KB
1_23.txt AC 23 ms 12928 KB
1_24.txt AC 6 ms 2432 KB
1_25.txt AC 29 ms 21888 KB
1_26.txt AC 25 ms 18304 KB
1_27.txt AC 14 ms 6272 KB
1_28.txt AC 11 ms 4608 KB
1_29.txt AC 17 ms 8960 KB
1_30.txt AC 15 ms 9728 KB
1_31.txt AC 34 ms 22656 KB
1_32.txt AC 40 ms 21888 KB
1_33.txt AC 7 ms 2560 KB
1_34.txt AC 24 ms 14464 KB
1_35.txt AC 27 ms 14208 KB
1_36.txt AC 27 ms 18432 KB
1_37.txt AC 35 ms 21504 KB
1_38.txt AC 21 ms 14592 KB
1_39.txt AC 40 ms 22272 KB
1_40.txt AC 8 ms 3072 KB
1_41.txt AC 36 ms 20864 KB
1_42.txt AC 6 ms 2176 KB
1_43.txt AC 15 ms 7168 KB
1_44.txt AC 5 ms 1920 KB
1_45.txt AC 12 ms 4864 KB
1_46.txt AC 8 ms 3200 KB
1_47.txt AC 6 ms 2304 KB
1_48.txt AC 17 ms 8576 KB
1_49.txt AC 22 ms 12928 KB