結果

問題 No.654 Air E869120
ユーザー ngtkanangtkana
提出日時 2019-08-28 00:25:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 7 ms / 2,000 ms
コード長 5,486 bytes
コンパイル時間 2,979 ms
コンパイル使用メモリ 230,616 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-17 16:04:51
合計ジャッジ時間 3,630 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#define loop(n) for (int ngtkana_is_genius = 0; ngtkana_is_genius < int(n); ngtkana_is_genius++)
#define rep(i, begin, end) for(int i = int(begin); i < int(end); i++)
#define lint long long
auto cmn = [](auto& a, auto b){if (a > b) {a = b; return true;} return false;};
auto cmx = [](auto& a, auto b){if (a < b) {a = b; return true;} return false;};
void debug_impl() { std::cerr << std::endl; }
template <typename Head, typename... Tail>
void debug_impl(Head head, Tail... tail){
  std::cerr << " " << head;
  debug_impl(tail...);
}
#define debug(...) std::cerr << std::boolalpha << "[" << #__VA_ARGS__ << "]:";\
  debug_impl(__VA_ARGS__);\
  std::cerr << std::noboolalpha;

template <typename Value>
struct dinic_edge
{
  int to; Value cap; int rev;
  dinic_edge(int to, Value cap, int rev): to(to), cap(cap), rev(rev){}
};

template <typename Value>
std::ostream& operator<< (std::ostream& os, const dinic_edge<Value>& e)
{
  return os
    << "dinic_edge{"
    << "to:" << e.to << ","
    << "cap:" << e.cap << ","
    << "rev:" << e.rev
    << "}";
}

template <typename Value>
class dinic {

  const int n, source, sink;
  std::vector<bool> ckd;
  std::vector<int> level;
  std::vector<std::vector<dinic_edge<Value>>> graph;
  static constexpr Value inf = std::numeric_limits<Value>::max();

  void bfs ()
  {
    std::queue<int> que;
    que.emplace(source); level.at(source) = 0;
    while (!que.empty())
    {
      auto crr = que.front(); que.pop();
      for (auto const& e : graph.at(crr))
      {
        if (e.cap == 0) continue;
        int nxt = e.to;
        if (level.at(nxt) != -1) continue;
        que.push(nxt);
        level.at(nxt) = level.at(crr) + 1;
      }
    }
  }

  auto dfs (int crr, Value f = inf) -> Value
  {
      if (crr == sink) return f;
      ckd.at(crr) = true;
      for (auto& e : graph.at(crr))
      {
        auto nxt = e.to;
        if (ckd.at(nxt) || e.cap == 0 || level.at(crr) >= level.at(nxt)) continue;
        auto d = dfs(nxt, std::min(f, e.cap));
        if (d > 0)
        {
          e.cap -= d;
          graph.at(nxt).at(e.rev).cap += d;
          return d;
        }
      }
      level.at(crr) = -1;
      return 0;
  }

  public:
    dinic (int n, int source, int sink) :
      n(n), source(source), sink(sink), graph(n)
      {}
    void insert(int u, int v, Value c)
    {
      int k = graph.at(u).size();
      int l = graph.at(v).size();
      graph.at(u).emplace_back(v, c, l);
      graph.at(v).emplace_back(u, 0, k);
    }

    Value build()
    {
      Value ret = 0;
      while (true)
      {
        level.assign(n, -1);
        bfs();
        if (level.at(sink) == -1) return ret;
        ckd.assign(n, false);
        while (true)
        {
          Value f = dfs(source);
          if (f == 0) break;
          ret += f;
        }
      }
    }
};

template <typename T, typename U>
std::ostream& operator<< (std::ostream& os, const std::pair<T, U>& pair){
  return os << "(" << pair.first << "," << pair.second << ")";
}

template <typename T, typename U>
std::istream& operator>> (std::iostream& is, std::pair<T, U>& pair) {
  return is >> pair.first >> pair.second;
}

template <typename T>
std::istream& operator>> (std::istream& is, std::vector<T>& v)
{
  for (auto & x : v) is >> x;
  return is;
}

template <typename T>
std::ostream& operator<< (std::ostream& os, const std::vector<T>& v)
{
  auto n = v.size();
  os << "{";
  for (size_t i = 0; i < n; i++)
    os << (i > 0 ? "," : "") << v[i];
  return os << "}";
}

template <typename T>
auto make_vector_impl(size_t sz, T t) {return std::vector<T>(sz, t);}

template <size_t N, typename T, typename U, std::enable_if_t<N == 1, std::nullptr_t> = nullptr>
auto make_vector(size_t sz, U u) {return make_vector_impl(sz, T(u));}

template <size_t N, typename T, std::enable_if_t<N == 1, std::nullptr_t> = nullptr>
auto make_vector(size_t sz) {return std::vector<T>(sz);}

template <size_t N, typename T, typename... Args, std::enable_if_t<N != 1, std::nullptr_t> = nullptr>
auto make_vector(size_t a, Args... args) {return make_vector_impl(a, make_vector<N - 1, T>(args...));}

template <typename T, typename Size_t>
auto& at(T& t, Size_t i) {return t.at(i);}

template <typename T, typename Size_t, typename... Args>
auto& at(T& t, Size_t i, Args... args) {return at(t.at(i), args...);}

int main()
{
  std::cin.tie(0); std::cin.sync_with_stdio(false);
  int n, m, d;
  std::cin >> n >> m >> d;
  std::vector<lint> u(m), v(m), p(m), q(m), w(m);
  rep(i, 0, m) {
    std::cin >> u.at(i) >> v.at(i) >> p.at(i) >> q.at(i) >> w.at(i);
    u.at(i)--, v.at(i)--, q.at(i) += d;
  }
  constexpr long long linf = 1LL << 60;
  auto s = 2 * m, t = 2 * m + 1;
  auto dnc = dinic<lint>(2 * m + 2, s, t);
  rep (i, 0, m) {
    if (u.at(i) == 0) dnc.insert(s, i, linf);
    if (v.at(i) == n - 1) dnc.insert(m + i, t, linf);
    dnc.insert(i, m + i, w.at(i));
  }
  auto events = make_vector<2, std::pair<lint, int>>(n, 0);
  rep(i, 0, m) {
    events.at(u.at(i)).emplace_back(p.at(i), i);
    events.at(v.at(i)).emplace_back(q.at(i), m + i);
  }
  rep(i, 0, n) {
    auto& v = events.at(i);
    std::sort(v.begin(), v.end(), [](auto p, auto q){
      if (p.first < q.first) return true;
      if (p.first > q.first) return false;
      return p.second > q.second;
    });
    int k = v.size();
    rep(j, 0, k - 1) {
      dnc.insert(v.at(j).second, v.at(j + 1).second, linf);
    }
  }
  std::cout << dnc.build() << std::endl;
  return 0;
}
0