QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#504892 | #9102. Zayin and Elements | Cheek_support# | WA | 40ms | 12544kb | C++20 | 3.4kb | 2024-08-04 17:03:19 | 2024-08-04 17:03:20 |
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];
int min_cost;
int min_pre[N];
void dfs(int x, int cur_cost)
{
// DEBUG_VAR(cur_cost);
if(cur_cost >= min_cost) return;
if(x == T) {
min_cost = cur_cost;
memcpy(min_pre, pre, sizeof(*pre)*(T + 1));
return;
}
vis[x] = true;
for(int i = one[x]; ~i; i = nxt[i]) {
// DEBUG_VAR(i);
int y = ver[i], z = w[i];
if(vis[y] || !cap[i]) continue;
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);
pre[y] = -1;
}
vis[x] = false;
}
bool EK()
{
memset(pre, -1, sizeof(*pre)*(T + 1));
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) {
int i = min_pre[cur];
cap[i]--;
cap[i ^ 1]++;
cur = ver[i ^ 1];
}
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);
}
}
memset(one, -1, sizeof one); idx = 0;
S = n + m + 1, T = n + m + 2;
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;
while(EK());
for(int i = 1; i <= m; i++) {
addEdge(S, i, b[i] * 2, 1);
}
while(EK());
for(int i = 1; i <= m; i++) {
addEdge(S, i, c[i], 2);
}
while(EK());
// DEBUG_HERE;
if(flow < n) {
cout << "-1" << endl;
return;
}
assert(cost % 2 == 0);
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;
}
详细
Test #1:
score: 100
Accepted
time: 3ms
memory: 12544kb
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: 40ms
memory: 10580kb
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'