結果

問題 No.1316 Maximum Minimum Spanning Tree
ユーザー 👑 hos.lyrichos.lyric
提出日時 2020-12-25 05:14:09
言語 D
(dmd 2.106.1)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 5,124 bytes
コンパイル時間 970 ms
コンパイル使用メモリ 135,928 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-06-22 10:33:04
合計ジャッジ時間 78,269 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,948 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 41 ms
6,940 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 3 ms
6,940 KB
testcase_07 AC 1,970 ms
6,940 KB
testcase_08 TLE -
testcase_09 AC 1,384 ms
6,944 KB
testcase_10 AC 1,370 ms
6,944 KB
testcase_11 AC 404 ms
6,940 KB
testcase_12 AC 398 ms
6,940 KB
testcase_13 AC 230 ms
6,940 KB
testcase_14 AC 258 ms
6,944 KB
testcase_15 TLE -
testcase_16 TLE -
testcase_17 AC 1,314 ms
6,940 KB
testcase_18 AC 1,417 ms
6,940 KB
testcase_19 AC 169 ms
6,940 KB
testcase_20 AC 165 ms
6,944 KB
testcase_21 AC 97 ms
6,940 KB
testcase_22 AC 108 ms
6,940 KB
testcase_23 AC 214 ms
6,944 KB
testcase_24 AC 206 ms
6,940 KB
testcase_25 AC 121 ms
6,944 KB
testcase_26 AC 136 ms
6,944 KB
testcase_27 AC 164 ms
6,944 KB
testcase_28 AC 161 ms
6,940 KB
testcase_29 AC 102 ms
6,940 KB
testcase_30 AC 98 ms
6,944 KB
testcase_31 AC 200 ms
6,940 KB
testcase_32 AC 207 ms
6,940 KB
testcase_33 AC 117 ms
6,944 KB
testcase_34 AC 137 ms
6,948 KB
testcase_35 AC 169 ms
6,944 KB
testcase_36 AC 167 ms
6,940 KB
testcase_37 AC 106 ms
6,940 KB
testcase_38 AC 107 ms
6,944 KB
testcase_39 AC 203 ms
6,940 KB
testcase_40 AC 203 ms
6,944 KB
testcase_41 AC 129 ms
6,944 KB
testcase_42 AC 114 ms
6,944 KB
testcase_43 AC 1,683 ms
6,944 KB
testcase_44 AC 1,692 ms
6,940 KB
testcase_45 AC 1,173 ms
6,944 KB
testcase_46 AC 1,159 ms
6,944 KB
testcase_47 AC 1,991 ms
6,948 KB
testcase_48 AC 1,956 ms
6,940 KB
testcase_49 AC 1,154 ms
6,940 KB
testcase_50 AC 1,169 ms
6,940 KB
testcase_51 AC 1,806 ms
6,944 KB
testcase_52 AC 1,787 ms
6,940 KB
testcase_53 AC 1,203 ms
6,940 KB
testcase_54 AC 1,266 ms
6,944 KB
testcase_55 AC 1,362 ms
6,940 KB
testcase_56 AC 1,403 ms
6,940 KB
testcase_57 AC 1,354 ms
6,940 KB
testcase_58 AC 1,343 ms
6,940 KB
testcase_59 AC 1,379 ms
6,940 KB
testcase_60 AC 1,316 ms
6,944 KB
testcase_61 AC 1,424 ms
6,944 KB
testcase_62 AC 1,430 ms
6,940 KB
testcase_63 AC 1,351 ms
6,940 KB
testcase_64 AC 1,355 ms
6,940 KB
testcase_65 AC 1,443 ms
6,940 KB
testcase_66 AC 1,454 ms
6,944 KB
testcase_67 AC 1,385 ms
6,944 KB
testcase_68 AC 1,355 ms
6,944 KB
testcase_69 AC 1,444 ms
6,940 KB
testcase_70 AC 1,385 ms
6,940 KB
testcase_71 AC 1,461 ms
6,940 KB
testcase_72 AC 1,484 ms
6,944 KB
testcase_73 AC 1,430 ms
6,940 KB
testcase_74 AC 1,384 ms
6,944 KB
testcase_75 AC 225 ms
6,940 KB
testcase_76 AC 234 ms
6,940 KB
testcase_77 AC 228 ms
6,944 KB
testcase_78 TLE -
testcase_79 TLE -
testcase_80 TLE -
testcase_81 TLE -
権限があれば一括ダウンロードができます

ソースコード

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)); }


class MaxFlow(Capa, Capa wEPS = 0, Capa wINF = 10^^9) {
  int n, m;
  int[][] g;
  int[] zu;
  Capa[] capa;
  Capa tof;
  int[] lev, see, que;
  this(int n) {
    this.n = n; m = 0; g = new int[][n]; zu = []; capa = [];
    lev = new int[n]; see = new int[n]; que = new int[n];
  }
  void addEdge(int u, int v, Capa w0, Capa w1 = 0) {
    g[u] ~= m; zu ~= v; capa ~= w0; ++m;
    g[v] ~= m; zu ~= u; capa ~= w1; ++m;
  }
  Capa augment(int src, int ink, Capa flo) {
    if (src == ink) return flo;
    foreach (i; g[src][see[src] .. $]) {
      if (capa[i] > wEPS && lev[src] < lev[zu[i]]) {
        Capa f = augment(zu[i], ink, min(flo, capa[i]));
        if (f > wEPS) { capa[i] -= f; capa[i ^ 1] += f; return f; }
      }
      ++see[src];
    }
    return 0;
  }
  bool dinic(int src, int ink, Capa flo = wINF) {
    for (tof = 0; tof + wEPS < flo; ) {
      int qb, qe;
      lev[] = -1;
     dinicBFS:
      for (lev[src] = 0, que[qe++] = src; qb != qe; ) {
        const u = que[qb++];
        foreach (i; g[u]) {
          const v = zu[i];
          if (capa[i] > wEPS && lev[v] == -1) {
            lev[v] = lev[u] + 1; que[qe++] = v;
            if (v == ink) break dinicBFS;
          }
        }
      }
      if (lev[ink] == -1) return false;
      see[] = 0;
      for (; ; ) {
        Capa f = augment(src, ink, flo - tof);
        if (f <= wEPS) break;
        tof += f;
      }
    }
    return true;
  }
}


/*
  http://www.columbia.edu/~cs2035/courses/ieor6614.S16/mst-lp.pdf
  max_x { K min_y { \sum_i (c_i + x_i) y_i | MST-LP } - \sum_i d_i x_i | x_i >= 0 }
  
  https://www.jstage.jst.go.jp/article/kodaimath1978/11/1/11_1_5/_pdf
  min_y { max_x { \sum_i K (c_i + x_i) y_i - \sum_i d_i x_i | x_i >= 0 } | MST-LP }
  = min_y { max_x { \sum_i (K c_i y_i + (K y_i - d_i) x_i) | x_i >= 0 } | MST-LP }
  = min_y { \sum_i K c_i y_i + \sum_i INF [K y_i - d_i > 0] | MST-LP }
  = min_y { \sum_i K c_i y_i | MST-LP, K y_i <= d_i }
  
  MST-LP
    y_i >= 0
    \sum_i y_i = N - 1
    \sum_{i=uv, u,v\in S} y_i <= |S| - 1
  
  ("cut >= 1" instead: not nec integer!)
  
  min_S { |S| - \sum_{i=uv, u,v\in S} y_i } >= 1
  take i ==> take u and take v
  take i: cost -y_i
  take u: cost 1
  need to take some u
*/

enum INF = 10L^^18;

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      const M = readInt();
      const K = readLong();
      auto A = new int[M];
      auto B = new int[M];
      auto C = new long[M];
      auto D = new long[M];
      foreach (i; 0 .. M) {
        A[i] = readInt() - 1;
        B[i] = readInt() - 1;
        C[i] = readLong();
        D[i] = readLong();
      }
      
      alias Edge = Tuple!(long, "c", int, "i");
      auto edges = new Edge[M];
      foreach (i; 0 .. M) {
        edges[i] = Edge(C[i], i);
      }
      edges.sort;
      
      auto ys = new long[M];
      // take i0
      bool check(int i0) {
        auto mf = new MaxFlow!(long, 0, INF)(2 + M + N);
        long cost;
        foreach (i; 0 .. M) {
          cost -= ys[i];
          mf.addEdge(0, 2 + i, ys[i]);
          mf.addEdge(2 + i, 2 + M + A[i], INF);
          mf.addEdge(2 + i, 2 + M + B[i], INF);
        }
        foreach (u; 0 .. N) {
          mf.addEdge(2 + M + u, 1, K);
        }
        mf.addEdge(0, 2 + i0, INF);
        mf.dinic(0, 1);
        if (cost + mf.tof < K) {
          return false;
        }
        return true;
      }
      
      foreach (ref edge; edges) {
        const i = edge.i;
        long lo = -1, hi = D[i] + 1;
        for (; lo + 1 < hi; ) {
          const mid = (lo + hi) / 2;
          ys[i] = mid;
          (check(i) ? lo : hi) = mid;
        }
        ys[i] = lo;
      }
      debug {
        writeln("ys = ", ys);
      }
      
      if (ys.sum < K * (N - 1)) {
        writeln(-1);
      } else {
        long ans;
        foreach (i; 0 .. M) {
          ans += C[i] * ys[i];
        }
        writeln(ans);
      }
    }
  } catch (EOFException e) {
  }
}
0