結果
| 問題 |
No.1541 ゅゅさんのテスト勉強
|
| コンテスト | |
| ユーザー |
emthrm
|
| 提出日時 | 2021-06-06 19:06:53 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 4,226 bytes |
| コンパイル時間 | 2,388 ms |
| コンパイル使用メモリ | 212,532 KB |
| 最終ジャッジ日時 | 2025-01-22 04:12:13 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 17 WA * 14 |
ソースコード
#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(i, k) cin >> a[i];
REP(i, k) cin >> b[i];
REP(i, k) psp.add_eq(i, a[i], 1, -b[i]);
}
cout << -psp.solve() << '\n';
return 0;
}
emthrm