QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#504859 | #9104. Zayin and Forest | Cheek_support# | TL | 0ms | 0kb | C++14 | 4.2kb | 2024-08-04 16:48:49 | 2024-08-04 16:48:49 |
answer
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define DEBUG_VAR(x) { cerr << "* "#x" = " << x << endl; }
#define DEBUG_ARR(arr, l, r) { \
cerr << "* "#arr"[" << l << ", " << r << "]:"; \
for(auto it = l, itr = r; it <= itr; it++) \
cerr << arr[it] << ", "; \
cerr << endl; \
}
#define DEBUG_FMT(...) { cerr << "* "; fprintf(stderr, __VA_ARGS__); }
#define DEBUG_HERE { DEBUG_FMT("Passing [%s] in LINE %d\n", __FUNCTION__, __LINE__); }
using namespace std;
using LL = long long;
using ULL = unsigned long long;
const int N = 2e5 + 5;
const int INF = 0x3f3f3f3f;
int n, m;
int a[N], b[N], c[N];
vector<int> e[N];
int one[N], idx;
int ver[N], nxt[N], w[N], cap[N];
void addEdge(int a, int b, int c, int d)
{
ver[idx] = b; cap[idx] = c; w[idx] = d; nxt[idx] = one[a]; one[a] = idx++;
ver[idx] = a; cap[idx] = 0; w[idx] = -d; nxt[idx] = one[b]; one[b] = idx++;
}
int S, T;
int flow, cost;
int pre[N];
bool vis_e[N];
int min_cost;
int min_pre[N];
void dfs(int x, int cur_cost)
{
// DEBUG_VAR(x);
if(cur_cost >= min_cost) return;
if(x == T) {
min_cost = cur_cost;
memcpy(min_pre, pre, sizeof(*pre)*(T + 1));
return;
}
for(int i = one[x]; ~i; i = nxt[i]) {
// DEBUG_VAR(i);
int y = ver[i], z = w[i];
if(vis_e[i ^ 1] || cap[i] == 0) continue;
vis_e[i] = true;
if(w[i] == 1) {
if(cap[i] % 2 == 0) {
z = 2;
} else {
z = 0;
}
} else if(w[i] == -1) {
if(cap[i] % 2 == 0) {
z = 0;
} else {
z = -2;
}
}
pre[y] = i;
dfs(y, cur_cost + z);
vis_e[i] = false;
}
}
bool EK()
{
// memset(dis, 0x3f, sizeof(*dis)*(T + 1));
memset(pre, -1, sizeof(*pre)*(T + 1));
// memset(vis_e, 0, sizeof(*vis_e)*(T + 1));
// queue<int> q;
// q.push(S); dis[S] = 0; vis[S] = true;
// while(q.size()) {
// int x = q.front(); q.pop();
// vis[x] = false;
// for(int i = one[x]; ~i; i = nxt[i]) {
// if(cap[i] == 0) continue;
// int y = ver[i], z = w[i];
// if(w[i] == 1) {
// if(cap[i] % 2 == 0) {
// z = 2;
// } else {
// z = 0;
// }
// } else if(w[i] == -1) {
// if(cap[i] % 2 == 0) {
// z = 0;
// } else {
// z = -2;
// }
// }
// if(dis[y] > dis[x] + z) {
// dis[y] = dis[x] + z;
// pre[y] = i;
// if(!vis[y]) q.push(y);
// }
// }
// }
min_cost = INF;
dfs(S, 0);
if(min_cost == INF) return false;
flow += 1;
cost += min_cost;
DEBUG_VAR(cost);
int cur = T;
while(cur != S) {
// DEBUG_VAR(cur);
int i = min_pre[cur];
cap[i]--;
cap[i ^ 1]++;
cur = ver[i ^ 1];
}
return true;
}
void solve()
{
cin >> n >> m;
int sum = 0;
for(int i = 1; i <= m; i++) {
cin >> a[i] >> b[i] >> c[i];
sum += b[i] + c[i];
int k; cin >> k;
while(k--) {
int x; cin >> x;
e[i].push_back(x);
}
}
memset(one, -1, sizeof one); idx = 0;
S = n + m + 1, T = n + m + 2;
for(int i = 1; i <= n; i++) {
addEdge(S, i, a[i], 0);
addEdge(S, i, b[i] * 2, 1);
addEdge(S, i, c[i], 2);
for(int x : e[i]) {
addEdge(i, m + x, 1, 0);
}
}
for(int i = 1; i <= n; i++) {
addEdge(m + i, T, 1, 0);
}
flow = 0, cost = 0;
while(EK());
if(flow < n) {
cout << "-1" << endl;
return;
}
assert(cost % 2 == 0);
sum -= cost/2;
cout << sum << endl;
}
int main()
{
ios::sync_with_stdio(false); cin.tie(nullptr);
int T; cin >> T;
while(T--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
1000000000 20000 2 384578735 526547442 1 64211261 592970906 1 512065247 448267721 1 44993150 127180320 1 880319036 927623947 1 170536687 572121854 1 896600029 804033011 1 666246328 754201635 1 654066651 179982083 2 240989825 984888006 2 372004567 858916479 2 76127818 98606736 1 181794163 902842353 1...