結果

問題 No.1678 Coin Trade (Multiple)
ユーザー 👑 hos.lyrichos.lyric
提出日時 2021-09-10 21:54:38
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 2,349 ms / 5,000 ms
コード長 4,296 bytes
コンパイル時間 984 ms
コンパイル使用メモリ 145,256 KB
実行使用メモリ 25,816 KB
最終ジャッジ日時 2024-06-22 12:26:56
合計ジャッジ時間 36,050 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 231 ms
14,760 KB
testcase_04 AC 1,160 ms
20,480 KB
testcase_05 AC 466 ms
25,524 KB
testcase_06 AC 359 ms
22,484 KB
testcase_07 AC 1,066 ms
13,408 KB
testcase_08 AC 692 ms
17,248 KB
testcase_09 AC 509 ms
24,792 KB
testcase_10 AC 274 ms
7,480 KB
testcase_11 AC 809 ms
19,828 KB
testcase_12 AC 244 ms
8,272 KB
testcase_13 AC 1,743 ms
21,492 KB
testcase_14 AC 594 ms
11,396 KB
testcase_15 AC 620 ms
17,968 KB
testcase_16 AC 180 ms
19,016 KB
testcase_17 AC 538 ms
23,832 KB
testcase_18 AC 2 ms
6,944 KB
testcase_19 AC 1 ms
6,944 KB
testcase_20 AC 1 ms
6,940 KB
testcase_21 AC 2 ms
6,944 KB
testcase_22 AC 2 ms
6,944 KB
testcase_23 AC 1 ms
6,944 KB
testcase_24 AC 2 ms
6,940 KB
testcase_25 AC 1 ms
6,940 KB
testcase_26 AC 1 ms
6,944 KB
testcase_27 AC 1 ms
6,940 KB
testcase_28 AC 1 ms
6,944 KB
testcase_29 AC 1 ms
6,940 KB
testcase_30 AC 1 ms
6,944 KB
testcase_31 AC 1 ms
6,940 KB
testcase_32 AC 2 ms
6,940 KB
testcase_33 AC 2 ms
6,944 KB
testcase_34 AC 2 ms
6,940 KB
testcase_35 AC 2 ms
6,940 KB
testcase_36 AC 1 ms
6,940 KB
testcase_37 AC 1 ms
6,944 KB
testcase_38 AC 1 ms
6,940 KB
testcase_39 AC 1 ms
6,944 KB
testcase_40 AC 2 ms
6,944 KB
testcase_41 AC 1 ms
6,940 KB
testcase_42 AC 2 ms
6,940 KB
testcase_43 AC 2 ms
6,944 KB
testcase_44 AC 1 ms
6,940 KB
testcase_45 AC 1 ms
6,944 KB
testcase_46 AC 2 ms
6,944 KB
testcase_47 AC 1 ms
6,940 KB
testcase_48 AC 2,211 ms
25,816 KB
testcase_49 AC 2,225 ms
24,748 KB
testcase_50 AC 2,349 ms
24,936 KB
testcase_51 AC 2,324 ms
23,860 KB
testcase_52 AC 2,204 ms
22,236 KB
testcase_53 AC 2,182 ms
24,112 KB
testcase_54 AC 2,251 ms
22,856 KB
testcase_55 AC 2,232 ms
23,776 KB
testcase_56 AC 2,265 ms
24,984 KB
testcase_57 AC 2,254 ms
24,660 KB
testcase_58 AC 1,354 ms
14,876 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }


// Dijkstra
class MinCostFlow(Capa, Cost, Capa CAPA_EPS = 0) {
  int n, m;
  int[][] g;
  int[] as, bs;
  Capa[] capa;
  Cost[] cost, pot;
  Capa tof;
  Cost toc;
  this(int n) {
    this.n = n; m = 0; g = new int[][n]; as = []; bs = []; capa = []; cost = [];
  }
  int addEdge(int u, int v, Capa w, Cost c) {
    debug {
      writeln("addEdge ", u, " ", v, " ", w, " ", c);
    }
    const i = m;
    g[u] ~= m; as ~= u; bs ~= v; capa ~= w; cost ~= c; ++m;
    g[v] ~= m; as ~= v; bs ~= u; capa ~= 0; cost ~= -c; ++m;
    return i;
  }
  bool solve(int source, int sink, Capa flow) {
    pot = new Cost[n];
    pot[] = 0;
    /*
    for (; ; ) {
      bool cont;
      foreach (u; 0 .. n) foreach (i; g[u]) if (capa[i] > CAPA_EPS) {
        const v = bs[i];
        if (pot[v] > pot[u] + cost[i]) {
          pot[v] = pot[u] + cost[i];
          cont = true;
        }
      }
      if (!cont) break;
    }
    */
    auto dist = new Cost[n];
    auto prei = new int[n];
    auto vis = new bool[n];
    for (tof = 0, toc = 0; tof + CAPA_EPS < flow; ) {
      alias Entry = Tuple!(Cost, "c", int, "u");
      BinaryHeap!(Array!Entry, "a > b") que;
      dist[] = -1;
      prei[] = -1;
      vis[] = false;
      dist[source] = 0;
      prei[source] = -2;
      que.insert(Entry(0, source));
      for (; !que.empty(); ) {
        const c = que.front.c;
        const u = que.front.u;
        que.removeFront;
        if (vis[u]) continue;
        vis[u] = true;
        foreach (i; g[u]) if (capa[i] > CAPA_EPS) {
          const v = bs[i];
          if (vis[v]) continue;
          const cc = c + cost[i] + pot[u] - pot[v];
          if (prei[v] == -1 || dist[v] > cc) {
            dist[v] = cc;
            prei[v] = i;
            que.insert(Entry(cc, v));
          }
        }
      }
      if (!vis[sink]) return false;
      Capa f = flow - tof;
      for (int v = sink; v != source; ) {
        const i = prei[v];
        if (f > capa[i]) f = capa[i];
        v = as[i];
      }
      for (int v = sink; v != source; ) {
        const i = prei[v];
        capa[i] -= f; capa[i ^ 1] += f;
        v = as[i];
      }
      tof += f;
      toc += f * (dist[sink] - pot[source] + pot[sink]);
      pot[] += dist[];
    }
    return true;
  }
}


enum BIG = 10L^^10;

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      const K = readInt();
      auto A = new long[N];
      auto M = new int[N];
      auto B = new int[][N];
      foreach (u; 0 .. N) {
        A[u] = readLong();
        M[u] = readInt();
        B[u] = new int[M[u]];
        foreach (m; 0 .. M[u]) {
          B[u][m] = readInt() - 1;
        }
      }
      
      auto mcf = new MinCostFlow!(int, long)(N);
      foreach (u; 0 .. N - 1) {
        mcf.addEdge(u, u + 1, K, BIG);
      }
      foreach (u; 0 .. N) {
        foreach (m; 0 .. M[u]) {
          const v = B[u][m];
          mcf.addEdge(v, u, 1, BIG * (u - v) + A[v] - A[u]);
        }
      }
      const res = mcf.solve(0, N - 1, K);
      assert(res);
      const ans = -(mcf.toc - K * BIG * (N - 1));
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}
0