QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#427337 | #7737. Extending Distance | zlxFTH | TL | 0ms | 0kb | C++14 | 4.5kb | 2024-06-01 12:49:26 | 2024-06-01 12:49:28 |
answer
#include <algorithm>
#include <cassert>
#include <cstdio>
#define debug(...) fprintf(stderr, __VA_ARGS__), \
fflush(stderr)
using LL = long long;
const int N = 500 + 5;
const int NN = 500 * 500 + 5;
const LL InfLL = 1e18;
int tot = 1, head[NN], cur[NN], dep[NN], pre[NN];
LL dis[NN], flow[NN];
struct Edge {
int nxt, v;
LL w, c;
} e[NN * 20];
int s, s1, t;
inline void addEdge(int u, int v, LL w, LL c) {
e[++tot] = {head[u], v, w, c};
head[u] = tot;
}
inline void add(int u, int v, LL w, LL c) {
if (v == s || u == t) return;
// debug("%d %d %lld %lld\n", u, v, w, c);
addEdge(u, v, w, c);
addEdge(v, u, 0, -c);
}
inline bool bfs() {
for (int i = 1; i <= t; ++i) dep[i] = 0;
static int q[NN];
int qh = 1, qt = 0;
q[++qt] = s;
dep[s] = 1;
while (qh <= qt) {
int u = q[qh++];
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].v;
LL w = e[i].w;
if (w && !dep[v]) {
dep[v] = dep[u] + 1;
q[++qt] = v;
}
}
}
return dep[t];
}
LL dfs(int u, LL in) {
if (u == t) return in;
LL out = 0;
for (int &i = cur[u]; i; i = e[i].nxt) {
int v = e[i].v;
LL w = e[i].w;
if (dep[v] == dep[u] + 1 && w) {
LL res = dfs(v, std::min(in - out, w));
e[i].w -= res;
e[i ^ 1].w += res;
out += res;
if (in == out) return in;
}
}
if (!out) dep[u] = 0;
return out;
}
inline bool spfa() {
static int vis[NN];
for (int i = 1; i <= t; ++i) {
dis[i] = InfLL;
vis[i] = 0;
flow[i] = 0;
}
static int q[NN];
int qh = 1, qt = 0;
q[++qt] = s, vis[s] = 1, dis[s] = 0, flow[s] = InfLL;
while (qh <= qt) {
int u = q[qh++];
vis[u] = 0;
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].v;
LL w = e[i].w, c = e[i].c;
if (dis[v] > dis[u] + c && w) {
dis[v] = dis[u] + c;
pre[v] = i;
flow[v] = std::min(flow[u], w);
if (!vis[v]) {
vis[v] = 1;
q[++qt] = v;
}
}
}
}
return flow[t];
}
inline LL ek() {
LL res = 0, mf = flow[t];
// debug("%lld\n", mf);
for (int u = t; u != s;) {
int i = pre[u];
res += mf * e[i].c;
e[i].w -= mf;
e[i ^ 1].w += mf;
u = e[i ^ 1].v;
}
return res;
}
int n, m, K;
LL a[N][N], b[N][N];
inline int id(int i, int j) {
assert(i <= n - 1 && j <= m - 1 && i > 0 && j > 0);
return (i - 1) * (m - 1) + j;
}
void solve() {
scanf("%d%d%d", &n, &m, &K);
for (int i = 1; i <= n; ++i)
for (int j = 1; j < m; ++j)
scanf("%lld", &a[i][j]);
for (int i = 1; i < n; ++i)
for (int j = 1; j <= m; ++j)
scanf("%lld", &b[i][j]);
s1 = (n - 1) * (m - 1) + 1, s = s1 + 1, t = s + 1;
add(s, s1, InfLL, 0);
for (int i = 1; i <= n; ++i) {
for (int j = 1; j < m; ++j) {
int u = (i == 1 ? s1 : id(i - 1, j));
int v = (i == n ? t : id(i, j));
add(u, v, a[i][j], 0);
add(v, u, a[i][j], 0);
}
}
for (int i = 1; i < n; ++i) {
for (int j = 1; j <= m; ++j) {
int u = (j == m ? s1 : id(i, j));
int v = (j == 1 ? t : id(i, j - 1));
add(u, v, b[i][j], 0);
add(v, u, b[i][j], 0);
}
}
LL mf = 0;
while (bfs()) {
for (int u = 1; u <= t; ++u) cur[u] = head[u];
mf += dfs(s, InfLL);
}
// debug("shortest path: %lld\n", mf);
e[2].w = 0;
add(s, s1, K, 0);
int bck = tot;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j < m; ++j) {
int u = (i == 1 ? s1 : id(i - 1, j));
int v = (i == n ? t : id(i, j));
add(u, v, K, 1);
add(v, u, K, 1);
}
}
for (int i = 1; i < n; ++i) {
for (int j = 1; j <= m; ++j) {
int u = (j == m ? s1 : id(i, j));
int v = (j == 1 ? t : id(i, j - 1));
add(u, v, K, 1);
add(v, u, K, 1);
}
}
LL ans = 0;
while (spfa()) ans += ek();
printf("%lld\n", ans);
for (int i = 1; i <= n; ++i) {
for (int j = 1; j < m; ++j) {
int u = (i == 1 ? s1 : id(i - 1, j));
int v = (i == n ? t : id(i, j));
printf("%lld%c", a[i][j] + e[bck += 2].w, " \n"[j == m - 1]);
}
}
for (int i = 1; i < n; ++i) {
for (int j = 1; j <= m; ++j) {
int u = (j == m ? s1 : id(i, j));
int v = (j == 1 ? t : id(i, j - 1));
printf("%lld%c", b[i][j] + e[bck += 2].w, " \n"[j == m]);
}
}
for (int i = 1; i <= t; ++i) head[i] = 0;
tot = 0;
}
int main() {
int t;
scanf("%d", &t);
while (t--) solve();
return 0;
}
詳細信息
Test #1:
score: 0
Time Limit Exceeded
input:
2 3 4 6 2 1 15 7 1 9 13 3 2 3 6 1 2 5 2 15 3 3 3 3 1 1 2 2 3 3 1 1 1 2 2 2