QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#185045 | #3588. Hamilton - The Musical | isaunoya | RE | 0ms | 3460kb | C++14 | 3.1kb | 2023-09-21 16:19:25 | 2023-09-21 16:19:26 |
Judging History
answer
#include <stdio.h>
#include <iostream>
#include <vector>
#define rep(i,s,t) for (register int i = (s), i##_ = (t) + 1; i < i##_; ++ i)
#define pre(i,s,t) for (register int i = (s), i##_ = (t) - 1; i > i##_; -- i)
const int N = 505;
using ll = long long;
int n, m, s, t, t1, t2, t3, t4, tme;
int head[N], mlc = 1;
struct ep {
int to, next;
ll flow, cost;
} e[N << 1];
static inline void adde(int u, int v, ll fl, int cs, bool __same = false) {
e[++ mlc] = { v, head[u], fl, cs };
head[u] = mlc;
e[++ mlc] = { u, head[v], (__same ? fl : 0), -cs };
head[v] = mlc;
}
int vis[N], fa[N], fae[N];
void dfs(int u) {
vis[u] = 1;
for (int i = head[u], v; i; i = e[i].next) {
if (!vis[v = e[i].to] and e[i].flow)
fa[v] = u, fae[v] = i, dfs(v);
}
}
int pi[N];
int phi(int x) {
if (vis[x] == tme) return pi[x];
return vis[x] = tme, pi[x] = phi(fa[x]) - e[fae[x]].cost;
}
int cyc[N];
ll cost;
#define swap(x, y) { int t = x; x = y; y = t; }
static inline void pushflow(int id) {
int pen = e[id ^ 1].to, lca = e[id].to;
++ tme;
while (pen) vis[pen] = tme, pen = fa[pen];
while (vis[lca] != tme) vis[lca] = tme, lca = fa[lca];
int e2 = 0, f = e[id].flow, path = 2, clen = 0;
for (int i = e[id ^ 1].to; i != lca; i = fa[i]) {
cyc[++ clen] = fae[i];
if (e[fae[i]].flow < f) f = e[fae[e2 = i] ^ (path = 0)].flow;
}
for (int i = e[id].to; i != lca; i = fa[i]) {
cyc[++ clen] = fae[i] ^ 1;
if (e[fae[i] ^ 1].flow < f) f = e[fae[e2 = i] ^ (path = 1)].flow;
}
cyc[++ clen] = id;
rep(i,1,clen) {
e[cyc[i]].flow -= f, e[cyc[i] ^ 1].flow += f;
cost += 1ll * f * e[cyc[i]].cost;
}
if (path == 2) return;
int lste = id ^ path, lst = e[lste].to, now = e[lste ^ 1].to;
while (lst != e2) {
vis[now] --;
lste ^= 1;
swap(lste, fae[now]);
swap(lst, fa[now]);
swap(lst, now);
}
}
static inline ll simplex(int s, int t) {
int lhead = head[s], rhead = head[t];
adde(t, s, 1e13, -0x3f3f3f3);
dfs(t);
vis[t] = tme = 2, fa[t] = cost = 0;
for (int i = 2, pre = mlc; i != pre; i = (i == mlc ? 2 : i + 1)) {
if (e[i].flow and e[i].cost < phi(e[i ^ 1].to) - phi(e[i].to))
pushflow(pre = i);
}
head[s] = lhead, head[t] = rhead;
mlc -= 2;
return e[mlc + 2].flow;
}
using namespace std;
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
int N = (n + 1) / 2;
int S = N * 2, T = N * 2 + 1;
s = S, t = T;
std::vector<vector<int>> a(n, vector<int>(n, 0));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> a[i][j];
}
}
for (int i = 0; i < n; i += 2) {
adde(S, i / 2, 1, 0);
adde(i / 2 + N, T, 1, 0);
}
for (int i = 0; i < n; i += 2) {
for (int j = 0; j < n; j += 2) {
int l = j - 1;
int r = j + 1;
int v = 0;
if (l >= 0) {
v += a[i][l];
}
if (r < n) {
v += a[i][r];
}
adde(i / 2, j / 2 + N, 1, v);
}
}
ll flow = simplex(s, t);
cout << cost + 1ll * flow * 0x3f3f3f3 << "\n";
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3460kb
input:
4 0 3 2 13 3 0 8 9 2 8 0 5 13 9 5 0
output:
16
result:
ok single line: '16'
Test #2:
score: -100
Runtime Error
input:
455 0 71154840 37432199 724743679 761809953 949789082 725973225 28455138 924138757 574256423 297516452 223475432 693485895 699629134 731875885 697643903 595490098 206757530 965177624 178416136 103223719 474785234 322984824 510200285 656185874 993023494 973542087 511171280 465648799 836547414 8145240...