QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#505015 | #9102. Zayin and Elements | Cheek_support | WA | 38ms | 10032kb | C++20 | 3.7kb | 2024-08-04 18:15:25 | 2024-08-04 18:15:25 |
Judging History
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;
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[N];
double min_cost;
int min_pre[N];
int cnt_t = 0;
void dfs(int x, double cur_cost)
{
cnt_t++;
// assert(cnt_t <= 1000000);
// if(cnt_t > 100000) return;
// DEBUG_VAR(x);
if(cur_cost >= min_cost) return;
if(x == T) {
min_cost = cur_cost;
for(int i = 1; i <= T; i++) {
min_pre[i] = pre[i];
}
return;
}
vis[x] = true;
for(int i = one[x]; ~i; i = nxt[i]) {
// DEBUG_VAR(i);
int y = ver[i];
double z = w[i];
if(vis[y] || !cap[i]) continue;
if(w[i] == 1) {
if(cap[i] % 2 == 0) {
z = 1.9999999;
} else {
z = 0;
}
} else if(w[i] == -1) {
if(cap[i] % 2 == 0) {
z = 0;
} else {
z = -2.000001;
}
}
pre[y] = i;
dfs(y, cur_cost + z);
pre[y] = -1;
}
vis[x] = false;
}
bool EK()
{
fill_n(pre + 1, T, -1);
fill_n(min_pre + 1, T, -1);
min_cost = INF;
cnt_t = 0;
dfs(S, 0);
DEBUG_VAR(min_cost);
if(min_cost > INF - 1) return false;
flow += 1;
cost += round(min_cost);
assert(flow <= n);
// DEBUG_VAR(cost);
int cur = T;
int cnt_c = 0;
while(cur != S) {
int i = min_pre[cur];
cap[i]--;
cap[i ^ 1]++;
cur = ver[i ^ 1];
cnt_c++;
assert(cnt_c <= 1000);
}
return true;
}
void solve()
{
cin >> n >> m;
for(int i = 1; i <= m; i++) {
e[i].clear();
}
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);
}
}
S = n + m + 1, T = n + m + 2;
fill_n(one, T + 1, -1); idx = 0;
for(int i = 1; i <= m; i++) {
addEdge(S, i, a[i], 0);
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;
for(int i = 1; i <= m; i++) {
addEdge(S, i, b[i] * 2, 1);
}
for(int i = 1; i <= m; i++) {
addEdge(S, i, c[i], 2);
}
while(EK());
// DEBUG_HERE;
if(flow < n) {
cout << "-1" << endl;
return;
}
sum -= cost/2;
cout << sum << endl;
}
int main()
{
// freopen("1.in", "r", stdin);
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: 100
Accepted
time: 2ms
memory: 10032kb
input:
2 5 2 2 3 1 2 3 1 1 1 1 3 4 5 2 5 2 2 3 1 1 3 1 0 1 2 1 2
output:
5 -1
result:
ok 2 lines
Test #2:
score: -100
Wrong Answer
time: 38ms
memory: 8004kb
input:
5 9 10 0 0 10 2 3 8 0 0 10 2 4 6 0 8 2 2 2 4 0 0 10 3 1 3 8 0 4 6 2 2 3 0 8 2 3 2 6 7 0 9 1 2 1 7 0 2 8 3 1 4 9 0 7 3 1 5 0 0 10 3 5 6 9 3 10 0 9 1 1 2 0 5 5 1 1 0 1 9 1 1 0 7 3 1 1 0 7 3 0 0 0 10 0 0 6 4 1 3 0 9 1 0 0 7 3 0 0 9 1 1 2 90 20 0 6 4 12 1 4 5 22 32 64 66 67 73 87 88 89 0 1 9 12 3 8 21 3...
output:
93 97 151 150 150
result:
wrong answer 1st lines differ - expected: '94', found: '93'