QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#642058 | #7737. Extending Distance | 369Pai | WA | 15ms | 16160kb | C++23 | 4.3kb | 2024-10-15 09:24:29 | 2024-10-15 09:24:29 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll , int> pr;
namespace FLOW
{
const int N = 250050 , M = N * 4;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
int s , t , tot = 1 , now[N] , head[N]; ll minc , dis[N];
bool vis[N]; struct Edge{int to , nxt; ll f , w;}e[M * 2];
void Add(int u , int v , ll f , ll w){e[++tot] = {v , head[u] , f , w} , head[u] = tot;}
void AddEdge(int u , int v , ll f , ll w = INF)
{
Add(u , v , f , w == INF ? 1 : w);
Add(v , u , 0 , w == INF ? 1 : -w);
}
bool BFS()
{
queue<int>q;
memset(dis , 0x3f , sizeof(ll) * (t + 1));
memset(vis , 0 , sizeof(bool) * (t + 1));
memcpy(now , head , sizeof(int) * (t + 1));
dis[s] = 0 , q.push(s);
while(!q.empty())
{
int u = q.front(); q.pop(); vis[u] = 0;
for(int i = head[u] ; i ; i = e[i].nxt)
{
ll v = e[i].to , f = e[i].f , w = e[i].w;
if(f && dis[u] + w < dis[v])
{
dis[v] = dis[u] + w;
if(!vis[v])q.push(v) , vis[v] = 1;
}
}
}
return dis[t] != INF;
}
ll Dinic(int u , ll flow)
{
if(u == t)return flow;
ll rest = flow;
vis[u] = 1;
for(int i = now[u] ; i && rest ; now[u] = i , i = e[i].nxt)
{
ll v = e[i].to , f = e[i].f , w = e[i].w;
if(f && !vis[v] && dis[u] + w == dis[v])
{
ll k = Dinic(v , min(f , rest));
if(!k)dis[v] = -INF;
e[i].f -= k , e[i ^ 1].f += k;
rest -= k , minc += k * w;
}
}
vis[u] = 0;
return flow - rest;
}
void Clear()
{
memset(head , 0 , sizeof(int) * (t + 1));
memset(vis , 0 , sizeof(bool) * (t + 1));
memset(e , 0 , sizeof(Edge) * (tot + 1));
tot = 1;
}
}
using FLOW::AddEdge , FLOW::Dinic , FLOW::BFS , FLOW::INF , FLOW::s , FLOW::t , FLOW::e , FLOW::tot , FLOW::minc;
const int N = 505;
int n , m , a[N][N] , b[N][N] , ia[N][N] , ib[N][N];
ll dis[N * N] , k; bool vis[N * N];
vector<pair<int , int> >g[N * N];
priority_queue<pr , vector<pr> , greater<pr> >pq;
ll Dijkstra()
{
int tot = n * m;
memset(dis , 0x3f , sizeof(ll) * (tot + 1));
memset(vis , 0 , sizeof(bool) * (tot + 1));
for(int i = 1 ; i <= n ; i++)
{
int u = (i - 1) * m + 1;
dis[u] = 0 , pq.push({0 , u});
}
while(!pq.empty())
{
auto [d , u] = pq.top(); pq.pop();
if(vis[u])continue ;
vis[u] = 1;
for(auto [v , w] : g[u])
{
if(dis[u] + w < dis[v])
{
dis[v] = dis[u] + w;
pq.push({dis[v] , v});
}
}
}
ll ans = INF;
for(int i = 1 ; i <= n ; i++)
{
int u = (i - 1) * m + m;
ans = min(ans , dis[u]);
}
return ans;
}
int Solve()
{
cin >> n >> m >> k;
auto id = [&](int i , int j){return (i - 1) * m + j;};
auto adde = [&](int u , int v , int w)
{
g[u].push_back({v , w});
g[v].push_back({u , w});
};
s = (n - 1) * (m - 1) + 1 , t = s + 1;
auto id2 = [&](int i , int j)
{
if(i <= 0)return s;
if(i >= n)return t;
return (i - 1) * (m - 1) + j;
};
auto adde2 = [&](int u , int v , int f)
{
AddEdge(u , v , f , 0);
AddEdge(v , u , f , 0);
AddEdge(u , v , INF , 1);
AddEdge(v , u , INF , 1);
// cerr << u << ' ' << v << " " << f << "\n";
};
for(int i = 1 ; i <= n ; i++)
for(int j = 1 ; j < m ; j++)
{
cin >> a[i][j];
adde(id(i , j) , id(i , j + 1) , a[i][j]);
adde2(id2(i - 1 , j) , id2(i , j) , a[i][j]);
ia[i][j] = tot - 3;
}
for(int i = 1 ; i < n ; i++)
for(int j = 1 ; j <= m ; j++)
{
cin >> b[i][j];
adde(id(i , j) , id(i + 1 , j) , b[i][j]);
if(j > 1 && j < m)
{
adde2(id2(i , j - 1) , id2(i , j) , b[i][j]);
ib[i][j] = tot - 3;
}
}
k += Dijkstra();
// cerr << k << "\n";
ll flow = 0; minc = 0;
while(BFS())
{
flow += Dinic(s , k - flow);
// cerr << flow << ',' << minc << "\n";
if(flow == k)break ;
}
cout << minc << "\n";
for(int i = 1 ; i <= n ; i++)
for(int j = 1 ; j < m ; j++)
{
int id = ia[i][j];
cout << a[i][j] + e[id + 1].f + e[id + 3].f << " \n"[j == m - 1];
}
for(int i = 1 ; i < n ; i++)
for(int j = 1 ; j <= m ; j++)
{
int id = ib[i][j];
cout << b[i][j] + (id ? e[id + 1].f + e[id + 3].f : 0) << " \n"[j == m];
}
FLOW::Clear();
for(int i = 1 ; i <= n * m ; i++)g[i].clear();
return 0;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0) , cout.tie(0);
int T; cin >> T;
while(T--)Solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 13896kb
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: 0ms
memory: 13880kb
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 3 18 44 48 20 91 19 76 95 36 16 61 88 50 49 68 18 33 84 14 54 69 42 47 90 28 2 73 59 15 20 90 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 45...
result:
ok Correct. (125 test cases)
Test #3:
score: 0
Accepted
time: 0ms
memory: 14156kb
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 15 16 20 99 28 21 13 88 12 23 15 100 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 16 19 18 63 23 31 17 45 20 18 22 56 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 ...
result:
ok Correct. (80 test cases)
Test #4:
score: 0
Accepted
time: 0ms
memory: 14164kb
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: 4ms
memory: 13868kb
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: 0
Accepted
time: 0ms
memory: 13868kb
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...
output:
17 27500 8466 7754 5254 45204 36821 35457 23725 45494 26962 24728 31454 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 17295 49299...
result:
ok Correct. (40 test cases)
Test #7:
score: 0
Accepted
time: 2ms
memory: 13884kb
input:
31 8 8 84 82373 175391 615535 844446 885043 54908 235817 174599 782716 140021 505505 551220 980613 844864 490309 720051 436451 436322 357173 349094 303200 428983 865478 890817 50236 172208 96832 261006 265321 413840 490656 677420 172238 872751 182871 957260 978182 971447 603592 37811 282590 470570 1...
output:
84 82373 175391 615535 844446 885043 54908 235817 174599 782716 140021 505505 551220 980613 844864 490309 720051 436451 436322 357173 349094 303200 428983 865478 890817 50236 172208 96832 261090 265321 413840 490656 677420 172238 872751 182871 957260 978182 971447 603592 37811 282590 470570 134862 3...
result:
ok Correct. (31 test cases)
Test #8:
score: 0
Accepted
time: 6ms
memory: 13760kb
input:
24 9 9 80 178 146 100 118 196 180 100 110 153 125 200 139 174 169 163 196 173 167 120 182 172 142 188 132 160 150 148 171 162 125 180 152 159 152 161 177 106 129 152 114 179 132 146 126 107 148 141 151 165 123 151 153 112 151 148 182 105 188 136 199 173 192 117 118 116 190 103 198 125 150 105 118 15...
output:
227 178 146 100 118 196 180 100 167 153 125 200 139 174 169 163 196 173 167 120 182 172 142 188 132 160 150 148 171 162 125 180 152 159 152 161 177 106 129 152 149 214 132 146 126 107 148 141 171 165 123 151 153 112 151 148 182 105 188 136 199 173 192 117 118 116 190 103 198 125 150 105 198 157 130 ...
result:
ok Correct. (24 test cases)
Test #9:
score: 0
Accepted
time: 6ms
memory: 13952kb
input:
20 10 10 91 90000001 90000000 90000001 90000000 90000001 90000000 90000000 90000001 90000000 90000001 90000001 90000001 90000001 90000000 90000001 90000001 90000001 90000001 90000001 90000001 90000001 90000000 90000000 90000000 90000001 90000000 90000000 90000000 90000001 90000001 90000001 90000000 ...
output:
886 90000001 90000000 90000001 90000000 90000001 90000000 90000000 90000001 90000090 90000087 90000001 90000001 90000001 90000000 90000001 90000001 90000001 90000001 90000001 90000001 90000001 90000000 90000000 90000000 90000001 90000000 90000090 90000087 90000001 90000001 90000001 90000000 90000001...
result:
ok Correct. (20 test cases)
Test #10:
score: 0
Accepted
time: 15ms
memory: 16160kb
input:
10 14 14 68 20 23 20 22 23 26 23 22 28 30 25 20 29 22 30 26 20 22 21 26 23 23 22 22 22 21 24 29 30 30 24 20 25 23 30 27 27 26 24 30 25 25 24 26 26 23 22 22 30 24 20 23 27 24 29 24 22 24 21 20 20 28 24 21 28 20 24 25 29 20 30 30 30 30 24 27 23 28 29 25 25 26 21 27 23 25 25 27 23 27 23 23 22 27 27 29 ...
output:
607 20 23 20 23 23 26 23 22 28 30 25 20 85 34 30 26 20 22 21 26 23 26 31 27 22 60 41 29 30 30 24 20 25 23 30 27 27 26 36 31 25 25 24 26 26 23 22 30 30 24 22 60 45 24 29 34 27 24 21 20 32 30 24 21 37 22 24 34 29 20 33 30 30 30 24 27 23 42 29 25 25 26 21 27 23 25 30 27 23 27 60 38 22 27 27 29 30 25 24...
result:
ok Correct. (10 test cases)
Test #11:
score: 0
Accepted
time: 11ms
memory: 16004kb
input:
10 10 20 79 1001 1003 1000 1003 1001 1003 1002 1003 1001 1003 1003 1000 1001 1001 1001 1002 1002 1003 1002 1001 1003 1001 1001 1001 1000 1003 1002 1000 1002 1001 1001 1003 1000 1001 1003 1001 1001 1000 1001 1003 1000 1002 1003 1003 1003 1001 1002 1002 1000 1002 1003 1001 1003 1000 1002 1000 1002 100...
output:
732 1001 1003 1000 1003 1001 1003 1002 1003 1001 1003 1003 1000 1001 1001 1001 1002 1002 1003 1070 1069 1003 1001 1001 1001 1000 1003 1002 1000 1002 1001 1001 1003 1000 1001 1003 1001 1001 1010 1003 1003 1000 1002 1003 1003 1003 1001 1002 1002 1000 1002 1003 1001 1003 1000 1002 1000 1070 1070 1003 1...
result:
ok Correct. (10 test cases)
Test #12:
score: 0
Accepted
time: 6ms
memory: 13912kb
input:
10 20 10 21 777601561 917773453 313011120 861769383 651010533 771534309 418153755 749795307 939533489 769621271 705696594 863041783 330858635 136276987 569175453 21935211 559486868 264609946 30013079 725693020 492377730 630078388 365743281 693415122 589281054 690370363 47310510 125777481 136448711 5...
output:
21 777601561 917773453 313011120 861769383 651010533 771534309 418153755 749795307 939533489 769621271 705696594 863041783 330858635 136276987 569175453 21935211 559486868 264609946 30013079 725693020 492377730 630078388 365743281 693415122 589281054 690370363 47310510 125777481 136448711 508925654 ...
result:
ok Correct. (10 test cases)
Test #13:
score: -100
Wrong Answer
time: 3ms
memory: 12164kb
input:
24 6 2 37 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 9 18 13 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...
output:
222 38 38 38 38 38 38 1 1 1 1 1 1 1 1 1 1 117 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 14 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 13 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 14 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 13 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 14 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 13 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 14 2 1 1 1 1...
result:
wrong answer The output T doesn't match number of operations. (test case 14)