結果

問題 No.654 Air E869120
ユーザー te-shte-sh
提出日時 2018-04-17 15:45:57
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 18 ms / 2,000 ms
コード長 3,627 bytes
コンパイル時間 1,088 ms
コンパイル使用メモリ 117,992 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-06-13 00:35:14
合計ジャッジ時間 2,147 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;

void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}}
void readC(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}}

void main()
{
  int n, m, d; readV(n, m, d);
  int[] u, v, p, q, w; readC(m, u, v, p, q, w);
  u[] -= 1; v[] -= 1;

  auto dep = new int[][](n), arr = new int[][](n);
  foreach (i; 0..m) {
    dep[u[i]] ~= p[i];
    arr[v[i]] ~= q[i];
  }

  auto st = 0, tt = 10^^9;
  dep[0] ~= st;
  arr[n-1] ~= tt;

  foreach (i; 0..n) {
    dep[i] = dep[i].sort().uniq.array;
    arr[i] = arr[i].sort().uniq.array;
  }

  auto deph = new int[int][](n), arrh = new int[int][](n), c = 0;
  foreach (i; 0..n)
    foreach (pi; dep[i]) deph[i][pi] = c++;
  foreach (i; 0..n)
    foreach (qi; arr[i]) arrh[i][qi] = c++;

  auto g = GraphW!(int, long, 10L^^18)(c);

  foreach (i; 0..m)
    g.addEdge(deph[u[i]][p[i]], arrh[v[i]][q[i]], w[i]);

  foreach (i; 0..n)
    if (!dep[i].empty)
      foreach (j; 0..dep[i].length-1)
        g.addEdge(deph[i][dep[i][j]], deph[i][dep[i][j+1]], g.inf);

  foreach (i; 0..n-1)
    foreach (qi; arr[i]) {
      auto np = dep[i].assumeSorted.upperBound(qi+d-1);
      if (!np.empty)
        g.addEdge(arrh[i][qi], deph[i][np.front], g.inf);
    }

  foreach (i; 0..arr[n-1].length-1)
    g.addEdge(arrh[n-1][arr[n-1][i]], arrh[n-1][arr[n-1][i+1]], g.inf);

  writeln(g.dinic(deph[0][st], arrh[n-1][tt]));
}

struct GraphW(N = int, W = int, W i = 10^^9)
{
  alias Node = N, Wt = W, inf = i;
  struct Edge { Node src, dst; Wt wt; alias cap = wt; }
  Node n;
  Edge[][] g;
  alias g this;
  this(Node n) { this.n = n; g = new Edge[][](n); }
  void addEdge(Node u, Node v, Wt w) { g[u] ~= Edge(u, v, w); }
  void addEdgeB(Node u, Node v, Wt w) { g[u] ~= Edge(u, v, w); g[v] ~= Edge(v, u, w); }
}

template Dinic(Graph)
{
  import std.algorithm, std.container, std.traits;
  alias Node = TemplateArgsOf!Graph[0], Wt = TemplateArgsOf!Graph[1];

  struct EdgeR { Node src, dst; Wt cap, flow; Node rev; }

  Wt dinic(ref Graph g, Node s, Node t)
  {
    auto n = g.n, adj = withRev(g, n), level = new int[](n);

    auto levelize()
    {
      level[] = -1; level[s] = 0;

      auto q = DList!Node(s);
      while (!q.empty) {
        auto u = q.front; q.removeFront();
        if (u == t) break;
        foreach (ref e; adj[u])
          if (e.cap > e.flow && level[e.dst] < 0) {
            q.insertBack(e.dst);
            level[e.dst] = level[u] + 1;
          }
      }

      return level[t];
    }

    Wt augment(Node u, Wt cur)
    {
      if (u == t) return cur;

      foreach (ref e; adj[u]) {
        auto r = &adj[e.dst][e.rev];
        if (e.cap > e.flow && level[u] < level[e.dst]) {
          auto f = augment(e.dst, min(cur, e.cap - e.flow));
          if (f > 0) {
            e.flow += f;
            r.flow -= f;
            return f;
          }
        }
      }

      return 0;
    }

    Wt flow = 0, f = 0;

    while (levelize >= 0)
      while ((f = augment(s, g.inf)) > 0)
        flow += f;

    return flow;
  }

  EdgeR[][] withRev(ref Graph g, Node n)
  {
    auto r = new EdgeR[][](n);

    foreach (gi; g)
      foreach (e; gi) {
        r[e.src] ~= EdgeR(e.src, e.dst, e.cap, 0, cast(Node)(r[e.dst].length));
        r[e.dst] ~= EdgeR(e.dst, e.src, 0, 0, cast(Node)(r[e.src].length) - 1);
      }

    return r;
  }
}
auto dinic(G, N)(G g, N s, N t) { return Dinic!G.dinic(g, s, t); }
0