結果

問題 No.119 旅行のツアーの問題
ユーザー te-shte-sh
提出日時 2017-04-24 17:08:51
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 2,070 bytes
コンパイル時間 847 ms
コンパイル使用メモリ 94,640 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-03 12:58:25
合計ジャッジ時間 2,222 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,384 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 2 ms
4,384 KB
testcase_21 AC 3 ms
4,376 KB
testcase_22 AC 3 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;

void main()
{
  auto n = readln.chomp.to!size_t;
  auto bi = new int[](n), ci = new int[](n);

  foreach (i; 0..n) {
    auto rd = readln.split.to!(int[]);
    bi[i] = rd[0];
    ci[i] = rd[1];
  }

  auto gi = new edge[][](n * 2 + 2), s = n * 2, t = n * 2 + 1;
  foreach (i; 0..n) {
    gi[s] ~= edge(i, bi[i]);
    gi[i] ~= edge(i + n, int.max);
    gi[i + n] ~= edge(t, ci[i]);
  }

  auto m = readln.chomp.to!size_t;
  foreach (_; 0..m) {
    auto rd = readln.split.to!(size_t[]), i = rd[0], j = rd[1];
    gi[i] ~= edge(j + n, int.max);
  }

  writeln(bi.sum + ci.sum - gi.dinic(s, t));
}

alias Edge!int edge;

struct Edge(T)
{
  size_t v;
  T w;
}

T dinic(T)(const Edge!T[][] gi, size_t s, size_t t)
{
  import std.container;

  class EdgeR
  {
    size_t v;
    T w;
    size_t rev;

    this(size_t v, T w, size_t rev)
    {
      this.v = v;
      this.w = w;
      this.rev = rev;
    }
  }

  auto n = gi.length;

  auto hi = new EdgeR[][](n);
  foreach (i, g; gi)
    foreach (e; g) {
      hi[i] ~= new EdgeR(e.v, e.w, hi[e.v].length);
      hi[e.v] ~= new EdgeR(i, 0, hi[i].length - 1);
    }

  auto level = new int[](n);
  auto itr = new size_t[](n);

  int[] bfs(size_t s)
  {
    level[] = -1;

    auto qi = new DList!size_t();
    level[s] = 0; qi.insertBack(s);

    while (!qi.empty) {
      auto v = qi.front; qi.removeFront;
      foreach (e; hi[v])
        if (e.w > 0 && level[e.v] < 0) {
          level[e.v] = level[v] + 1;
          qi.insertBack(e.v);
        }
    }

    return level;
  }

  T dfs(size_t v, size_t t, T f)
  {
    if (v == t) return f;
    foreach (i; itr[v]..hi[v].length) {
      auto e = hi[v][i];
      if (e.w > 0 && level[v] < level[e.v]) {
        auto d = dfs(e.v, t, min(f, e.w));
        if (d > 0) {
          e.w -= d;
          hi[e.v][e.rev].w += d;
          return d;
        }
      }
    }
    return 0;
  }

  T ret, f;

  while (bfs(s)[t] >= 0) {
    itr[] = 0;
    while ((f = dfs(s, t, T.max)) > 0) ret += f;
  }

  return ret;
}
0