結果

問題 No.1984 [Cherry 4th Tune *] Dilemma
ユーザー 👑 emthrmemthrm
提出日時 2022-06-17 22:42:17
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 6,439 bytes
コンパイル時間 2,721 ms
コンパイル使用メモリ 216,188 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-26 01:45:59
合計ジャッジ時間 7,682 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 2 ms
6,940 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 2 ms
6,940 KB
testcase_19 AC 2 ms
6,940 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 2 ms
6,940 KB
testcase_22 AC 2 ms
6,940 KB
testcase_23 AC 3 ms
6,944 KB
testcase_24 AC 2 ms
6,944 KB
testcase_25 AC 2 ms
6,940 KB
testcase_26 AC 2 ms
6,940 KB
testcase_27 AC 2 ms
6,944 KB
testcase_28 AC 2 ms
6,940 KB
testcase_29 AC 2 ms
6,944 KB
testcase_30 AC 3 ms
6,940 KB
testcase_31 AC 3 ms
6,940 KB
testcase_32 AC 2 ms
6,944 KB
testcase_33 AC 2 ms
6,944 KB
testcase_34 AC 2 ms
6,940 KB
testcase_35 AC 2 ms
6,944 KB
testcase_36 AC 2 ms
6,940 KB
testcase_37 AC 2 ms
6,944 KB
testcase_38 AC 2 ms
6,948 KB
testcase_39 AC 2 ms
6,940 KB
testcase_40 AC 2 ms
6,940 KB
testcase_41 AC 2 ms
6,940 KB
testcase_42 AC 2 ms
6,940 KB
testcase_43 AC 2 ms
6,940 KB
testcase_44 AC 2 ms
6,944 KB
testcase_45 AC 2 ms
6,940 KB
testcase_46 AC 2 ms
6,944 KB
testcase_47 AC 2 ms
6,940 KB
testcase_48 AC 2 ms
6,940 KB
testcase_49 AC 2 ms
6,940 KB
testcase_50 AC 2 ms
6,944 KB
testcase_51 AC 2 ms
6,940 KB
testcase_52 AC 2 ms
6,940 KB
testcase_53 AC 1 ms
6,940 KB
testcase_54 AC 2 ms
6,944 KB
testcase_55 AC 2 ms
6,940 KB
testcase_56 AC 2 ms
6,940 KB
testcase_57 AC 2 ms
6,944 KB
testcase_58 AC 2 ms
6,940 KB
testcase_59 AC 2 ms
6,940 KB
testcase_60 AC 2 ms
6,944 KB
testcase_61 AC 2 ms
6,940 KB
testcase_62 AC 2 ms
6,944 KB
testcase_63 AC 2 ms
6,940 KB
testcase_64 AC 2 ms
6,944 KB
testcase_65 AC 2 ms
6,944 KB
testcase_66 AC 2 ms
6,940 KB
testcase_67 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1};
constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1};
constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U>
inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U>
inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

template <typename T>
struct Dinic {
  struct Edge {
    int dst, rev;
    T cap;
    explicit Edge(const int dst, const T cap, const int rev)
        : dst(dst), cap(cap), rev(rev) {}
  };

  std::vector<std::vector<Edge>> graph;

  explicit Dinic(const int n) : graph(n), level(n), itr(n) {}

  void add_edge(const int src, const int dst, const T cap) {
    graph[src].emplace_back(dst, cap, graph[dst].size());
    graph[dst].emplace_back(src, 0, graph[src].size() - 1);
  }

  T maximum_flow(const int s, const int t,
                 T limit = std::numeric_limits<T>::max()) {
    T res = 0;
    while (limit > 0) {
      std::fill(level.begin(), level.end(), -1);
      level[s] = 0;
      std::queue<int> que;
      que.emplace(s);
      while (!que.empty()) {
        const int ver = que.front();
        que.pop();
        for (const Edge& e : graph[ver]) {
          if (level[e.dst] == -1 && e.cap > 0) {
            level[e.dst] = level[ver] + 1;
            que.emplace(e.dst);
          }
        }
      }
      if (level[t] == -1) break;
      std::fill(itr.begin(), itr.end(), 0);
      while (limit > 0) {
        const T f = dfs(s, t, limit);
        if (f == 0) break;
        limit -= f;
        res += f;
      }
    }
    return res;
  }

 private:
  std::vector<int> level, itr;

  T dfs(const int ver, const int t, const T flow) {
    if (ver == t) return flow;
    for (; itr[ver] < graph[ver].size(); ++itr[ver]) {
      Edge& e = graph[ver][itr[ver]];
      if (level[ver] < level[e.dst] && e.cap > 0) {
        const T tmp = dfs(e.dst, t, std::min(flow, e.cap));
        if (tmp > 0) {
          e.cap -= tmp;
          graph[e.dst][e.rev].cap += tmp;
          return tmp;
        }
      }
    }
    return 0;
  }
};

template <template <typename> class C, typename T>
struct ProjectSelectionProblem {
  explicit ProjectSelectionProblem(const int n)
      : inf(std::numeric_limits<T>::max()), n(n), res(0) {}

  void add_neq(const int u, const int v, const T cost) {
    assert(cost >= 0);
    us.emplace_back(u);
    vs.emplace_back(v);
    costs.emplace_back(cost);
  }

  void add(const int v, bool group, T cost) {
    if (cost < 0) {
      cost = -cost;
      res += cost;
      group = !group;
    }
    if (group) {
      add_neq(-2, v, cost);  // -2 represents S.
    } else {
      add_neq(v, -1, cost);  // -1 represents T.
    }
  }

  void add_or(const std::vector<int>& v, const bool group, const T cost) {
    assert(cost >= 0);
    add(n, group, cost);
    if (group) {
      for (const int e : v) add_neq(n, e, inf);
    } else {
      for (const int e : v) add_neq(e, n, inf);
    }
    ++n;
  }

  void add_or(const int u, const int v, const bool group, const T cost) {
    add_or({u, v}, group, cost);
  }

  void add_eq(const std::vector<int>& v, const bool group, T cost) {
    assert(cost <= 0);
    cost = -cost;
    res += cost;
    add_or(v, !group, cost);
  }

  void add_eq(const int u, const int v, const bool group, const T cost) {
    add_eq({u, v}, group, cost);
  }

  T solve() {
    C<T> mf(n + 2);
    const int neq_size = costs.size();
    for (int i = 0; i < neq_size; ++i) {
      mf.add_edge(us[i] < 0 ? us[i] + n + 2 : us[i],
                  vs[i] < 0 ? vs[i] + n + 2 : vs[i], costs[i]);
    }
    return mf.maximum_flow(n, n + 1, inf) - res;
  }

 private:
  const T inf;
  int n;
  T res;
  std::vector<int> us, vs;
  std::vector<T> costs;
};

int main() {
  int n, m, k, p; cin >> n >> m >> k >> p;
  // ProjectSelectionProblem<Dinic, ll> psp(n + m + k);
  // REP(i, n) {
  //   int e; cin >> e;
  //   psp.add(i, true, -e);
  // }
  // ll action_exp = 0;
  // REP(i, m) {
  //   int f; cin >> f;
  //   psp.add(n + i, true, f);
  //   action_exp += f;
  // }
  // REP(i, k) {
  //   int v; cin >> v;
  //   psp.add(n + m + i, true, v);
  // }
  // REP(i, n) {
  //   int l; cin >> l;
  //   while (l--) {
  //     int a; cin >> a; --a;
  //     psp.add_neq(n + m + a, i, LINF);
  //   }
  // }
  // while (p--) {
  //   int i, j; cin >> i >> j; --i; --j;
  //   psp.add_neq(n + j, i, LINF);
  // }
  // cout << action_exp - psp.solve() << '\n';

  Dinic<ll> dinic(n + m + k + 2);
  const int s = n + m + k, t = n + m + k + 1;
  ll c = 0;
  REP(i, n) {
    int e; cin >> e;
    c += e;
    dinic.add_edge(i, t, e);
  }
  REP(i, m) {
    int f; cin >> f;
    c += f;
    dinic.add_edge(s, n + i, f);
  }
  REP(i, k) {
    int v; cin >> v;
    dinic.add_edge(s, n + m + i, v);
  }
  REP(i, n) {
    int l; cin >> l;
    while (l--) {
      int a; cin >> a; --a;
      dinic.add_edge(n + m + a, i, LINF);
    }
  }
  while (p--) {
    int i, j; cin >> i >> j; --i; --j;
    dinic.add_edge(n + j, i, LINF);
  }
  cout << c - dinic.maximum_flow(s, t, LINF) << '\n';
  vector<int> can_reach(n + m + k, false);
  queue<int> que;
  for (const auto& e : dinic.graph[s]) {
    if (e.cap > 0) {
      can_reach[e.dst] = true;
      que.emplace(e.dst);
    }
  }
  while (!que.empty()) {
    const int ver = que.front(); que.pop();
    for (const auto& e : dinic.graph[ver]) {
      if (e.cap > 0 && !can_reach[e.dst]) {
        can_reach[e.dst] = true;
        que.emplace(e.dst);
      }
    }
  }
  vector<pair<string, int>> ans;
  REP(i, m) {
    if (can_reach[n + i]) ans.emplace_back("Action", i);
  }
  REP(i, k) {
    if (!can_reach[n + m + i]) ans.emplace_back("Preparation", i);
  }
  REP(i, n) {
    if (!can_reach[i]) ans.emplace_back("Goal", i);
  }
  cout << ans.size() << '\n';
  for (const auto [s, i] : ans) cout << s << ' ' << i + 1 << '\n';
  return 0;
}
0