QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#323196 | #7737. Extending Distance | complexor | RE | 6ms | 3964kb | C++23 | 5.3kb | 2024-02-08 20:22:09 | 2024-02-08 20:22:09 |
Judging History
answer
#include <bits/stdc++.h>
#define clr(f, n) memset(f, 0, (n) << 2)
#define cpy(f, g, n) memcpy(f, g, (n) << 2)
typedef long long LL;
typedef unsigned long long ULL;
typedef __int128 LLL;
typedef std::pair<int, int> pii;
typedef std::pair<LL, LL> pll;
#define MP std::make_pair
#define fi first
#define se second
int read()
{
int s = 0, f = 1;
char c = getchar();
while (!(c >= '0' && c <= '9'))
f ^= (c == '-'), c = getchar();
while (c >= '0' && c <= '9')
s = s * 10 + (c ^ 48), c = getchar();
return f ? s : -s;
}
const int MAXN = 505, MAXM = 10005;
const LL INF = 0x3f3f3f3f3f3f3f3fll;
int n, m, r[MAXN][MAXN], d[MAXN][MAXN], K, N;
int S, T;
LL dist[MAXN][MAXN];
int nxt[MAXM], to[MAXM], cst[MAXM], pre[MAXN], tot = -1;
int L[MAXN][MAXN], R[MAXN][MAXN], U[MAXN][MAXN], D[MAXN][MAXN];
int head[MAXN], cur[MAXN];
LL cap[MAXN];
void add_edge(int u, int v, LL w, int c)
{ to[++tot] = v, nxt[tot] = head[u], cap[tot] = w, cst[tot] = c, head[u] = tot; }
void add_net(int u, int v, LL w, int c)
{ add_edge(u, v, w, c), add_edge(v, u, 0, -c); }
LL dis[MAXN];
bool vis[MAXN];
pll dfs(int x, LL lim)
{
if (vis[x]) return MP(0, 0);
if (x == T) return MP(lim, 0);
vis[x] = true;
LL F = 0, C = 0;
for (int &e = cur[x]; ~e; e = nxt[e])
if (cap[e] && dis[to[e]] == dis[x] + cst[e])
{
pll tmp = dfs(to[e], std::min(lim, cap[e]));
F += tmp.fi, C += tmp.fi * cst[e] + tmp.se;
lim -= tmp.fi, cap[e] -= tmp.fi, cap[e ^ 1] += tmp.fi;
if (!lim) return MP(F, C);
}
return MP(F, C);
}
bool modify()
{
if (vis[T]) return true;
LL mn = INF;
for (int i = 1; i <= N; i++) if (vis[i])
for (int e = head[i]; ~e; e = nxt[e])
if (cap[e] && !vis[to[e]])
mn = std::min(dis[i] + cst[e] - dis[to[e]], mn);
if (mn >= INF) return false;
for (int i = 1; i <= N; i++)
if (vis[i]) dis[i] -= mn;
return true;
}
pll MCMF()
{
LL F = 0, C = 0;
memset(dis + 1, 0, N << 3);
dis[S] = -INF;
do
{
memset(vis + 1, false, N);
memcpy(cur + 1, head + 1, N << 2);
pll res = dfs(S, INF);
F += res.fi, C += res.se;
} while (modify()) ;
return MP(F, C);
}
struct Node
{
int x, y; LL d;
Node(){}
Node(int _x, int _y, LL _d)
: x(_x), y(_y), d(_d) {}
friend bool operator<(Node a, Node b)
{ return a.d > b.d; }
} ;
void Dijkstra()
{
static bool vis[MAXN][MAXN];
static std::priority_queue<Node> q;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
dist[i][j] = INF, vis[i][j] = false;
for (int i = 1; i <= n; i++)
q.emplace(i, 1, dist[i][1] = 0);
while (!q.empty())
{
Node x = q.top(); q.pop();
if (vis[x.x][x.y]) continue;
vis[x.x][x.y] = true;
if (x.x < n && dist[x.x + 1][x.y] > x.d + d[x.x][x.y])
q.emplace(x.x + 1, x.y, dist[x.x + 1][x.y] = x.d + d[x.x][x.y]);
if (x.x > 1 && dist[x.x - 1][x.y] > x.d + d[x.x - 1][x.y])
q.emplace(x.x - 1, x.y, dist[x.x - 1][x.y] = x.d + d[x.x - 1][x.y]);
if (x.y < m && dist[x.x][x.y + 1] > x.d + r[x.x][x.y])
q.emplace(x.x, x.y + 1, dist[x.x][x.y + 1] = x.d + r[x.x][x.y]);
if (x.y > 1 && dist[x.x][x.y - 1] > x.d + r[x.x][x.y - 1])
q.emplace(x.x, x.y - 1, dist[x.x][x.y - 1] = x.d + r[x.x][x.y - 1]);
}
}
int main()
{
for (int Tcnt = read(); Tcnt--; )
{
n = read(), m = read(), K = read();
for (int i = 1; i <= n; i++)
for (int j = 1; j < m; j++)
r[i][j] = read();
for (int i = 1; i < n; i++)
for (int j = 1; j <= m; j++)
d[i][j] = read();
Dijkstra();
LL lim = INF;
for (int i = 1; i <= n; i++)
lim = std::min(dist[i][m], lim);
lim += K;
// printf("%d\n", lim);
N = (n - 1) * (m - 1);
S = ++N, T = ++N;
int s = ++N, t = ++N;
memset(head + 1, -1, N << 2), tot = -1;
add_net(S, s, lim, 0), add_net(t, T, lim, 0);
#define id(i, j) (((i) - 1) * (m - 1) + (j))
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
L[i][j] = R[i][j] = U[i][j] = D[i][j] = -1;
for (int i = 1; i < m; i++)
{
add_net(s, id(1, i), r[1][i], 0);
add_net(s, id(1, i), INF, 1), R[1][i] = tot;
}
for (int i = 1; i < m; i++)
{
add_net(id(n - 1, i), t, r[n][i], 0);
add_net(id(n - 1, i), t, INF, 1), R[n][i] = tot;
}
for (int i = 1; i < n - 1; i++)
for (int j = 1; j < m; j++)
{
add_net(id(i, j), id(i + 1, j), r[i + 1][j], 0);
add_net(id(i, j), id(i + 1, j), INF, 1), R[i + 1][j] = tot;
add_net(id(i + 1, j), id(i, j), r[i + 1][j], 0);
add_net(id(i + 1, j), id(i, j), INF, 1), L[i + 1][j + 1] = tot;
}
for (int i = 1; i < n; i++)
for (int j = 1; j < m - 1; j++)
{
add_net(id(i, j), id(i, j + 1), d[i][j + 1], 0);
add_net(id(i, j), id(i, j + 1), INF, 1), D[i][j + 1] = tot;
add_net(id(i, j + 1), id(i, j), d[i][j + 1], 0);
add_net(id(i, j + 1), id(i, j), INF, 1), U[i + 1][j + 1] = tot;
}
printf("%lld\n", MCMF().se);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
{
if (~L[i][j] && cap[L[i][j]]) r[i][j - 1] += cap[L[i][j]];
if (~R[i][j] && cap[R[i][j]]) r[i][j] += cap[R[i][j]];
if (~D[i][j] && cap[D[i][j]]) d[i][j] += cap[D[i][j]];
if (~U[i][j] && cap[U[i][j]]) d[i - 1][j] += cap[U[i][j]];
}
for (int i = 1; i <= n; i++)
for (int j = 1; j < m; j++)
printf("%d%c", r[i][j], " \n"[j + 1 == m]);
for (int i = 1; i < n; i++)
for (int j = 1; j <= m; j++)
printf("%d%c", d[i][j], " \n"[j == m]);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3900kb
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
output:
9 2 1 15 8 1 9 13 3 5 3 6 6 2 5 2 15 3 4 1 4 2 3 3 3 1 1 1 2 2 2
result:
ok Correct. (2 test cases)
Test #2:
score: 0
Accepted
time: 3ms
memory: 3920kb
input:
125 4 4 48 33 9 39 38 74 3 18 44 9 20 91 19 76 95 17 16 61 88 50 49 68 18 33 84 4 4 14 54 69 42 47 90 28 2 73 59 1 20 90 43 22 74 19 27 67 46 43 42 21 78 80 4 4 93 12 67 38 13 85 39 74 68 77 71 80 64 92 97 53 46 66 6 30 20 66 70 71 24 4 4 34 43 86 55 49 34 73 78 77 90 99 49 5 55 4 63 47 80 24 15 3 8...
output:
87 33 38 39 38 74 22 37 44 29 20 91 19 76 95 17 16 61 88 50 49 68 18 33 84 14 54 69 42 47 90 28 2 73 59 1 20 104 43 22 74 19 27 67 46 43 42 21 78 80 166 12 79 119 59 85 66 74 68 77 71 80 64 92 97 53 46 66 6 30 20 66 70 71 24 34 43 86 55 49 34 73 78 77 90 99 49 9 55 4 63 47 80 24 45 3 85 12 6 31 45 4...
result:
ok Correct. (125 test cases)
Test #3:
score: 0
Accepted
time: 5ms
memory: 3888kb
input:
80 5 5 97 10 18 14 13 17 15 16 11 15 10 14 15 12 17 12 15 12 11 15 15 19 19 13 19 19 19 17 10 10 19 12 13 18 11 20 12 17 14 13 12 5 5 65 13 15 13 20 18 19 13 20 10 19 18 17 19 19 11 14 12 18 11 10 18 14 18 19 18 20 11 17 11 17 16 13 19 18 13 16 14 17 11 18 5 5 3 15 10 10 18 17 17 17 14 13 15 15 19 1...
output:
473 10 18 14 108 26 15 20 89 35 16 20 79 48 17 17 68 57 20 18 55 19 19 13 19 19 19 17 10 10 19 12 13 18 11 20 12 17 14 13 12 271 13 15 13 75 27 19 14 56 34 19 18 45 42 24 11 39 36 18 16 46 18 14 18 19 18 20 11 17 11 17 16 13 19 18 13 16 14 17 11 18 3 15 10 10 21 17 17 17 14 13 15 15 19 10 18 16 17 1...
result:
ok Correct. (80 test cases)
Test #4:
score: 0
Accepted
time: 6ms
memory: 3964kb
input:
55 6 6 98 943830625 154853276 396311720 585129723 216006508 789713291 522861691 174874210 616414184 931597164 831871942 149821142 520941619 814161584 200419736 646044877 574761262 188910317 673355715 723256093 264106685 163628126 318085983 226850181 101764170 179381345 486537031 346100002 805792579 ...
output:
98 943830625 154853276 396311720 585129723 216006508 789713291 522861691 174874210 616414184 931597164 831871942 149821142 520941619 814161584 200419736 646044877 574761262 188910317 673355715 723256093 264106685 163628126 318085983 226850279 101764170 179381345 486537031 346100002 805792579 5081942...
result:
ok Correct. (55 test cases)
Test #5:
score: 0
Accepted
time: 5ms
memory: 3948kb
input:
55 6 6 96 16739843 17336211 10384494 17185421 10646458 18552039 13790956 13642043 10307995 14193711 19297204 12810329 18681558 18724838 16636750 11505737 19658923 19307194 12241535 15070027 16123862 17524159 19471242 14316479 10954501 10604286 17691735 17352365 14092770 19909253 11761060 19372581 16...
output:
96 16739843 17336211 10384494 17185421 10646458 18552039 13790956 13642043 10307995 14193807 19297204 12810329 18681558 18724838 16636750 11505737 19658923 19307194 12241535 15070027 16123862 17524159 19471242 14316479 10954501 10604286 17691735 17352365 14092770 19909253 11761060 19372581 16863139 ...
result:
ok Correct. (55 test cases)
Test #6:
score: -100
Runtime Error
input:
40 7 7 17 27500 8466 7754 5254 45204 36821 35457 23725 45494 26962 24728 31437 46232 38720 23756 17265 21004 25959 29727 6060 23244 45236 39610 23968 17068 14954 9745 28949 20957 30885 8272 49710 28660 17038 12058 48058 10306 5065 45011 25264 25765 17423 21072 22743 17503 11324 10214 6879 22253 1729...