結果

問題 No.654 Air E869120
ユーザー Kana NagataKana Nagata
提出日時 2019-06-12 23:16:31
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 19 ms / 2,000 ms
コード長 3,350 bytes
コンパイル時間 2,850 ms
コンパイル使用メモリ 198,972 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-20 05:19:40
合計ジャッジ時間 4,295 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 19 ms
5,376 KB
testcase_11 AC 11 ms
5,376 KB
testcase_12 AC 14 ms
5,376 KB
testcase_13 AC 13 ms
5,376 KB
testcase_14 AC 12 ms
5,376 KB
testcase_15 AC 13 ms
5,376 KB
testcase_16 AC 11 ms
5,376 KB
testcase_17 AC 14 ms
5,376 KB
testcase_18 AC 12 ms
5,376 KB
testcase_19 AC 13 ms
5,376 KB
testcase_20 AC 6 ms
5,376 KB
testcase_21 AC 7 ms
5,376 KB
testcase_22 AC 5 ms
5,376 KB
testcase_23 AC 5 ms
5,376 KB
testcase_24 AC 8 ms
5,376 KB
testcase_25 AC 4 ms
5,376 KB
testcase_26 AC 4 ms
5,376 KB
testcase_27 AC 4 ms
5,376 KB
testcase_28 AC 5 ms
5,376 KB
testcase_29 AC 4 ms
5,376 KB
testcase_30 AC 4 ms
5,376 KB
testcase_31 AC 4 ms
5,376 KB
testcase_32 AC 4 ms
5,376 KB
testcase_33 AC 5 ms
5,376 KB
testcase_34 AC 5 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
testcase_36 AC 1 ms
5,376 KB
testcase_37 AC 1 ms
5,376 KB
testcase_38 AC 3 ms
5,376 KB
testcase_39 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
template<typename T>
class fixed_point : T {
  public:
    explicit constexpr fixed_point (
        T&& t
      ) noexcept
      : T(forward<T>(t))
      {
      }
    template<typename... Args>
    constexpr decltype(auto)
    operator()(
        Args&&... args
      ) const
      {
        return T::operator()(*this, forward<Args>(args)...);
      }
};
template<typename T>
static inline constexpr decltype(auto) fix (T&& t) noexcept {
  return fixed_point<T>{forward<T>(t)};
}
template <typename T>
class dinic {
  public:
    static constexpr T inf = numeric_limits<T>::max();
  private:
    struct edge {
      int to; T cap;
      weak_ptr<edge> rev;
    };
    const int n, source, sink;
    vector<bool> ckd;
    vector<int> dst;
    vector<vector<shared_ptr<edge>>> grh;
    void bfs () {
      queue<int> que;
      que.emplace(source); dst[source] = 0;
      while (!que.empty()) {
        int crr = que.front(); que.pop();
        for (auto const& e : grh[crr]) {
          if (e->cap == 0) continue;
          int nxt = e->to;
          if (dst[nxt] != -1) continue;
          que.push(nxt); dst[nxt] = dst[crr] + 1;
        }
      }
    }
    T dfs () {
      return fix ([&](auto dfs, int crr, T f = inf) -> T {
        if (crr == sink) return f;
        ckd[crr] = true;
        for (auto& e : grh[crr]) {
          int nxt = e->to;
          if (ckd[nxt] || e->cap == 0 || dst[crr] >= dst[nxt]) continue;
          T d = dfs(nxt, min(f, e->cap));
          if (d > 0) {
            e->cap -= d;
            e->rev.lock()->cap += d;
            return d;
          }
        }
        dst[crr] = -1;
        return 0;
      })(source);
    }
  public:
    dinic (int n, int source, int sink) :
      n(n), source(source), sink(sink), grh(n)
      {}
    void insert(int u, int v, T c) {
      auto e = make_shared<edge>(edge{v, c});
      auto r = make_shared<edge>(edge{u, 0});
      e->rev = r;
      r->rev = e;
      grh[u].push_back(e);
      grh[v].push_back(r);
    }
    T cal() {
      T ret = 0;
      while (true) {
        dst.assign(n, -1);
        bfs();
        if (dst[sink] == -1) return ret;
        ckd.assign(n, false);
        while (true) {
          T f = dfs();
          if (f == 0) break;
          ret += f;
        }
      }
    }
};
int main() {
  cin.tie(0); cin.sync_with_stdio(false);
  int n, m, d;
  cin >> n >> m >> d;
  vector<pair<int, int>> vtx(n);
  vtx.emplace_back(0, -d);
  vtx.emplace_back(n - 1, 1e9);
  vector<tuple<int, int, int, int, int>> query;
  while (m--) {
    int u, v, p, q, w;
    cin >> u >> v >> p >> q >> w;
    u--; v--;
    query.emplace_back(u, v, p, q, w);
    vtx.emplace_back(u, p - d);
    vtx.emplace_back(v, q);
  }
  sort(vtx.begin(), vtx.end());
  int N = unique(vtx.begin(), vtx.end()) - vtx.begin();
  vtx.resize(N);
  dinic<long long> dnc(N, 0, N - 1);
  for (int i = 0; i < N - 1; i++) {
    if (vtx[i].first == vtx[i + 1].first) dnc.insert(i, i + 1, dinic<long long>::inf);
  }
  for (auto const& qry : query) {
    int u, v, p, q, w; tie(u, v, p, q, w) = qry;
    int s = find(vtx.begin(), vtx.end(), pair<int, int>{u, p - d}) - vtx.begin();
    int t = find(vtx.begin(), vtx.end(), pair<int, int>{v, q}) - vtx.begin();
    dnc.insert(s, t, w);
  }
  auto ret = dnc.cal();
  cout << ret << endl;
  return 0;
}
0