QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#505028 | #9102. Zayin and Elements | WorldFinalEscaped | WA | 274ms | 4076kb | C++14 | 3.7kb | 2024-08-04 18:27:18 | 2024-08-04 18:27:19 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1005;
const int M = 10005;
const int inf = 1e7;
struct Edge {
int to, nxt, cap, flow;
ll cost;
} edge[M];
int head[N], tol;
int pre[N];
ll dis[N];
bool vis[N];
int node, n, m;
void add(int u, int v, int cap, ll cost) {
edge[tol] = {v, head[u], cap, 0, cost};
head[u] = tol;
tol++;
}
void addedge(int u, int v, int cap, ll cost) {
// printf("???? u = %d, v = %d, cap = %lld, cost = %lld\n", u, v, cap, cost);
add(u, v, cap, cost);
add(v, u, 0, -cost);
}
bool spfa(int S, int T) {
queue<int> q;
for (int i = 1; i <= node; i++) {
dis[i] = 1e18;
vis[i] = false;
pre[i] = -1;
}
dis[S] = 0, vis[S] = true, q.push(S);
while (!q.empty()) {
int u = q.front(); q.pop();
vis[u] = false;
for (int i = head[u]; i != -1; i = edge[i].nxt) {
int v = edge[i].to;
if (edge[i].cap > edge[i].flow && dis[v] > dis[u] + edge[i].cost) {
dis[v] = dis[u] + edge[i].cost;
pre[v] = i;
if (!vis[v]) vis[v] = true, q.push(v);
}
}
}
if (pre[T] == -1) return false;
else return true;
}
int mcmf(int S, int T, ll &cost) {
int flow = 0; cost = 0;
while (spfa(S, T)) {
// printf("!!!\n");
int Min = inf;
for (int i = pre[T]; i != -1; i = pre[edge[i ^ 1].to]) {
if (Min > edge[i].cap - edge[i].flow)
Min = edge[i].cap - edge[i].flow;
}
// printf("Min = %d\n", Min);
flow += Min;
for (int i = pre[T]; i != -1; i = pre[edge[i ^ 1].to]) {
edge[i].flow += Min;
edge[i ^ 1].flow -= Min;
cost += edge[i].cost * Min;
}
}
return flow;
}
int a[55], b[55], c[55], id[55];
vector<int> to[55];
void clr() {
memset(head, -1, sizeof(head));
tol = 0;
node = 0;
}
mt19937 rng(114514);
void sc() {
scanf("%d", &n);
scanf("%d", &m);
for (int i = 1; i <= m; i++) {
scanf("%d%d%d", &a[i], &b[i], &c[i]);
int x; scanf("%d", &x);
while (x--) {
int t; scanf("%d", &t);
to[i].push_back(t);
}
id[i] = i;
}
int times = 300, qwq = 1e9, flows, all = 0;
while (times--) {
clr();
shuffle(id + 1, id + m + 1, rng);
int S = n + m + 1, T = n + m + 2;
for (int i = 1; i <= n; i++) {
addedge(S, i, 1, 0);
}
int cur = 0;
all = 0;
for (int _ = 1; _ <= m; _++) {
int i = id[_];
all += b[i] + c[i];
// printf("Now i = %d\n", i);
for (auto j: to[i])
addedge(j, n + i, 1, 0);
addedge(i + n, T, a[i], 0);
for (int j = 1; j <= 2 * b[i]; j++) {
if (j & 1) cur = rng() % b[i] + 1;
addedge(i + n, T, 1, cur * inf + (j & 1 ? 1 : 0));
}
addedge(i + n, T, c[i], 1ll * inf * inf);
}
node = n + m + 2;
ll costs = 0;
flows = mcmf(S, T, costs);
// printf("costs = %lld\n", costs);
qwq = min(qwq, (int) (costs % inf));
}
// cerr << "out!\n";
// printf("S = %d, T = %d\n", S, T);
// printf("flows = %lld, costs = %lld\n", flows, costs);
if (flows == n) printf("%d\n", all - qwq);
else puts("-1");
for (int i = 1; i <= m; i++) to[i].clear();
}
int main() {
int T; scanf("%d", &T);
while (T--) sc();
return 0;
}
/*
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
*/
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3792kb
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: 274ms
memory: 4076kb
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:
100 100 162 157 157
result:
wrong answer 1st lines differ - expected: '94', found: '100'