結果

問題 No.177 制作進行の宮森あおいです!
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-01-19 16:53:36
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 3,644 bytes
コンパイル時間 657 ms
コンパイル使用メモリ 106,436 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-03 22:42:05
合計ジャッジ時間 1,621 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

import std.conv, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.container, std.math, std.numeric, std.range, 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; }

void chmin(T)(ref T t, in T f) { if (t > f) t = f; }
void chmax(T)(ref T t, in T f) { if (t < f) t = f; }

int binarySearch(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = cast(int)(as.length); for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; }
int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a < val)); }
int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a <= val)); }


class MaxFlow(CAPA) {
  static immutable CAPA wEPS = 0;
  static immutable CAPA wINF = 1001001001;
  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 = null; capa = null;
    lev = new int[n]; see = new int[n]; que = new int[n];
  }
  void ae(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[] q;
      lev[] = -1;
    dinicBFS:
      for (lev[src] = 0, q ~= src; !q.empty; ) {
        int u = q.front; q.popFront;
        foreach (i; g[u]) {
          int v = zu[i];
          if (capa[i] > wEPS && lev[v] == -1) {
            lev[v] = lev[u] + 1; q ~= 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;
  }
}

int W;
int N;
int[] J;
int M;
int[] C;
int[] Q;
int[][] X;

void main() {
  try {
    for (; ; ) {
      W = readInt();
      N = readInt();
      J = new int[N];
      foreach (i; 0 .. N) {
        J[i] = readInt();
      }
      M = readInt();
      C = new int[M];
      foreach (j; 0 .. M) {
        C[j] = readInt();
      }
      Q = new int[M];
      X = new int[][M];
      foreach (j; 0 .. M) {
        Q[j] = readInt();
        X[j] = new int[Q[j]];
        foreach (k; 0 .. Q[j]) {
          X[j][k] = readInt() - 1;
        }
      }
      
      auto mf = new MaxFlow!int(2 + N + M);
      foreach (i; 0 .. N) {
        mf.ae(0, 2 + i, J[i]);
      }
      foreach (j; 0 .. M) {
        mf.ae(2 + N + j, 1, C[j]);
      }
      foreach (j; 0 .. M) {
        auto del = new bool[N];
        foreach (i; X[j]) {
          del[i] = true;
        }
        foreach (i; 0 .. N) {
          if (!del[i]) {
            mf.ae(2 + i, 2 + N + j, W);
          }
        }
      }
      const ans = mf.dinic(0, 1, W);
      writeln(ans ? "SHIROBAKO" : "BANSAKUTSUKITA");
    }
  } catch (EOFException e) {
  }
}
0