QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#185040 | #3588. Hamilton - The Musical | isaunoya | TL | 1ms | 3476kb | C++14 | 2.8kb | 2023-09-21 16:15:26 | 2023-09-21 16:15:27 |
Judging History
answer
#include <bits/stdc++.h>
using i64 = int64_t;
using u32 = uint32_t;
using u64 = uint64_t;
namespace Flow {
int S, T;
void Set(int _S, int _T) { S = _S, T = _T; }
struct Edge_t {
int v, nxt, w, cost;
Edge_t(int V = 0, int Nxt = 0, int W = 0, int Cost = 0) {
v = V, nxt = Nxt, w = W, cost = Cost;
}
};
std::vector<Edge_t> e(2);
std::vector<int> head;
void add(int u, int v, int w, int cost) {
if (u >= (int)head.size()) {
head.resize(u + 127);
}
e.emplace_back(v, head[u], w, cost);
head[u] = (int)e.size() - 1;
}
void addedge(int u, int v, int w, int cost) {
add(u, v, w, cost);
add(v, u, 0, -cost);
}
constexpr int inf = 1e9;
std::queue<int> q;
std::vector<int> dep;
std::vector<int> cur;
std::vector<bool> inq;
bool bfs() {
dep.assign(head.size(), inf);
inq.assign(head.size(), false);
cur = head;
q.emplace(S), dep[S] = 0;
while (!q.empty()) {
int u = q.front(); q.pop(); inq[u] = false;
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].v;
if (e[i].w && dep[v] > dep[u] + e[i].cost) {
dep[v] = dep[u] + e[i].cost;
if (!inq[v]) {
inq[v] = true;
q.emplace(v);
}
}
}
}
return dep[T] < inf;
}
int dfs(int u, int flow) {
if (!flow) {
return 0;
} else if (u == T) {
return flow;
} else {
inq[u] = true;
int f = 0;
for (int &i = cur[u], rf; i; i = e[i].nxt) {
int v = e[i].v;
if (!inq[v] && dep[v] == dep[u] + e[i].cost && (rf = dfs(v, std::min(flow, e[i].w)))) {
flow -= rf, f += rf;
e[i].w -= rf, e[i ^ 1].w += rf;
if (flow == 0) {
return f;
}
}
}
return f;
}
}
std::pair<int, int> dinic() {
int ans = 0, res = 0;
while (bfs()) {
int tmp = dfs(S, inf);
ans += tmp;
res += dep[T] * tmp;
}
return std::make_pair(ans, res);
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
using namespace std;
cin >> n;
int N = (n + 1) / 2;
int S = N * 2, T = N * 2 + 1;
Flow::Set(S, 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) {
Flow::addedge(S, i / 2, 1, 0);
Flow::addedge(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];
}
Flow::addedge(i / 2, j / 2 + N, 1, v);
}
}
auto tmp = Flow::dinic();
// std::cout << tmp.first << " " << tmp.second << std::endl;
cout << tmp.second << "\n";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3476kb
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
Time Limit Exceeded
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...