結果

問題 No.1541 ゅゅさんのテスト勉強
ユーザー 👑 emthrmemthrm
提出日時 2021-06-06 19:12:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 7 ms / 2,000 ms
コード長 4,234 bytes
コンパイル時間 2,518 ms
コンパイル使用メモリ 216,516 KB
実行使用メモリ 5,340 KB
最終ジャッジ日時 2023-08-15 02:17:09
合計ジャッジ時間 3,982 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,384 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 7 ms
5,192 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,384 KB
testcase_14 AC 2 ms
4,384 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 3 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,384 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 7 ms
5,272 KB
testcase_30 AC 7 ms
5,136 KB
testcase_31 AC 7 ms
5,156 KB
testcase_32 AC 7 ms
5,124 KB
testcase_33 AC 7 ms
5,252 KB
testcase_34 AC 7 ms
5,340 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 dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, 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;
    Edge(int dst, T cap, int rev) : dst(dst), cap(cap), rev(rev) {}
  };

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

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

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

  T maximum_flow(int s, int t, T limit) {
    T res = 0;
    while (true) {
      std::fill(level.begin(), level.end(), -1);
      std::queue<int> que;
      level[s] = 0;
      que.emplace(s);
      while (!que.empty()) {
        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) return res;
      std::fill(itr.begin(), itr.end(), 0);
      T f;
      while ((f = dfs(s, t, limit)) > 0) res += f;
    }
  }

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

  T dfs(int ver, int t, 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) {
        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 {
  ProjectSelectionProblem(int n) : n(n) {}

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

  void add(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, bool group, T cost) {
    assert(cost >= 0);
    add(n, group, cost);
    if (group) {
      for (int vi : v) add_neq(n, vi, inf);
    } else {
      for (int vi : v) add_neq(vi, n, inf);
    }
    ++n;
  }

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

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

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

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

private:
  const T inf = std::numeric_limits<T>::max();
  int n;
  T res = 0;
  std::vector<int> us, vs;
  std::vector<T> costs;
};

int main() {
  int n, m; cin >> n >> m;
  ProjectSelectionProblem<Dinic, ll> psp(n);
  REP(i, n) {
    int k, c; cin >> k >> c;
    psp.add(i, 1, c - m);
    vector<int> a(k), b(k);
    REP(j, k) cin >> a[j], --a[j];
    REP(j, k) cin >> b[j];
    REP(j, k) psp.add_eq(i, a[j], 1, -b[j]);
  }
  cout << -psp.solve() << '\n';
  return 0;
}
0