結果

問題 No.1316 Maximum Minimum Spanning Tree
ユーザー 👑 hos.lyrichos.lyric
提出日時 2020-12-13 01:26:24
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 5,458 bytes
コンパイル時間 5,064 ms
コンパイル使用メモリ 113,940 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-09-04 11:30:25
合計ジャッジ時間 9,251 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 WA -
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 WA -
testcase_08 AC 2 ms
4,376 KB
testcase_09 WA -
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 6 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 3 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 WA -
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 24 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 3 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 4 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 4 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 4 ms
4,380 KB
testcase_26 AC 2 ms
4,384 KB
testcase_27 AC 4 ms
4,380 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 4 ms
4,376 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 4 ms
4,380 KB
testcase_32 AC 1 ms
4,380 KB
testcase_33 AC 4 ms
4,376 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 5 ms
4,380 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 5 ms
4,380 KB
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 AC 4 ms
4,376 KB
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 AC 2 ms
4,376 KB
testcase_47 AC 6 ms
4,376 KB
testcase_48 AC 2 ms
4,380 KB
testcase_49 AC 4 ms
4,380 KB
testcase_50 AC 2 ms
4,380 KB
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
testcase_54 AC 1 ms
4,376 KB
testcase_55 AC 36 ms
4,376 KB
testcase_56 WA -
testcase_57 AC 49 ms
4,384 KB
testcase_58 AC 43 ms
4,376 KB
testcase_59 AC 17 ms
4,380 KB
testcase_60 WA -
testcase_61 WA -
testcase_62 WA -
testcase_63 WA -
testcase_64 WA -
testcase_65 AC 38 ms
4,376 KB
testcase_66 WA -
testcase_67 AC 38 ms
4,376 KB
testcase_68 AC 47 ms
4,380 KB
testcase_69 AC 37 ms
4,376 KB
testcase_70 AC 53 ms
4,376 KB
testcase_71 AC 54 ms
4,376 KB
testcase_72 WA -
testcase_73 WA -
testcase_74 WA -
testcase_75 WA -
testcase_76 WA -
testcase_77 WA -
testcase_78 WA -
testcase_79 WA -
testcase_80 WA -
testcase_81 AC 2 ms
4,376 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)); }


int root(int[] uf, int u) {
  return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));
}
bool connect(int[] uf, int u, int v) {
  u = uf.root(u);
  v = uf.root(v);
  if (u == v) return false;
  if (uf[u] > uf[v]) swap(u, v);
  uf[u] += uf[v];
  uf[v] = u;
  return true;
}


struct MinCut {
  int n;
  long[][] adj;
  int[] opt;
  this(int n_) {
    n = n_;
    adj = new long[][](n, n);
  }
  void ae(int u, int v, long c) {
    adj[u][v] += c;
    adj[v][u] += c;
  }
  long solve() {
    long ret = long.max;
    auto p = iota(n).array;
    auto b = new long[n];
    opt = [];
    for (; --n; ) {
      foreach (u; 0 .. n + 1) b[p[u]] = 0;
      foreach (u; 0 .. n) {
        int vm = u + 1;
        foreach (v; u + 1 .. n + 1) {
          b[p[v]] += adj[p[u]][p[v]];
          if (b[p[vm]] < b[p[v]]) vm = v;
        }
        swap(p[u + 1], p[vm]);
      }
      if (chmin(ret, b[p[n]])) {
        opt = p[n .. $];
      }
      foreach (u; 0 .. n - 1) {
        adj[p[u]][p[n - 1]] += adj[p[u]][p[n]];
        adj[p[n - 1]][p[u]] += adj[p[n]][p[u]];
      }
    }
    return ret;
  }
};


enum INF = 4 * 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();
      }
      
      auto mc = new MinCut(N);
      foreach (i; 0 .. M) {
        mc.ae(A[i], B[i], D[i]);
      }
      const res = mc.solve;
      debug {
        writeln("res = ", res);
        writeln("opt = ", mc.opt);
      }
      
      long ans = -1;
      if (res < K) {
        ans = -1;
      } else {
        ans = 0;
        auto cs = C.dup;
        auto uf = new int[N];
        uf[] = -1;
        for (; ; ) {
          debug {
            writefln("ans = %s, cs = %s, uf = %s", ans, cs, uf);
          }
          long[] ccs;
          foreach (i; 0 .. M) {
            const a = uf.root(A[i]);
            const b = uf.root(B[i]);
            if (a != b) {
              ccs ~= cs[i];
            }
          }
          ccs = ccs.sort.uniq.array;
          if (ccs.empty) {
            break;
          }
          auto uf1 = new int[N];
          uf1[] = -1;
          auto adj = new long[][](N, N);
          foreach (i; 0 .. M) {
            const a = uf.root(A[i]);
            const b = uf.root(B[i]);
            if (a != b && cs[i] == ccs[0]) {
              uf1.connect(a, b);
              adj[a][b] += D[i];
              adj[b][a] += D[i];
            }
          }
          auto uss = new int[][N];
          foreach (u; 0 .. N) {
            uss[uf1.root(u)] ~= u;
          }
          auto col = new bool[N];
          foreach (r; 0 .. N) {
            const len = cast(int)(uss[r].length);
            if (len >= 2) {
              auto mc1 = new MinCut(len);
              foreach (x; 0 .. len) foreach (y; x + 1 .. len) {
                mc1.ae(x, y, adj[uss[r][x]][uss[r][y]]);
              }
              const res1 = mc1.solve;
              debug {
                writefln("  uss[r] = %s, res1 = %s, opt = %s", uss[r], res1, mc1.opt);
              }
              if (res1 < K) {
                foreach (x; mc1.opt) {
                  col[uss[r][x]] = true;
                }
              }
            }
          }
          bool upd = false;
          foreach (i; 0 .. M) {
            const a = uf.root(A[i]);
            const b = uf.root(B[i]);
            if (a != b && cs[i] == ccs[0] && col[a] != col[b]) {
              upd = true;
              ans -= D[i] * (ccs[1] - ccs[0]);
              cs[i] = ccs[1];
            }
          }
          if (!upd) {
            foreach (i; 0 .. M) {
              const a = uf.root(A[i]);
              const b = uf.root(B[i]);
              if (a != b && cs[i] == ccs[0]) {
                debug {
                  writeln("connect ", a, " ", b, " at ", ccs[0]);
                }
                if (uf.connect(a, b)) {
                  ans += K * ccs[0];
                }
              }
            }
          }
        }
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}
0