QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#665371 | #7737. Extending Distance | tarjen | TL | 8ms | 5888kb | C++20 | 5.3kb | 2024-10-22 11:48:33 | 2024-10-22 11:48:33 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=510,M=2510*10;
int n,m,k;
int c[510][510],r[510][510];
struct SSP {
int cnt = 1, hd[N], nxt[M << 1], to[M << 1], limit[M << 1], cst[M << 1];
void init(int nn){
for(int i=0;i<=nn;i++)hd[i]=0;
cnt=1;
}
// w limit c cost
void add(int u, int v, int f, int c) {// limit cost
nxt[++cnt] = hd[u], hd[u] = cnt, to[cnt] = v, limit[cnt] = f, cst[cnt] = c;
nxt[++cnt] = hd[v], hd[v] = cnt, to[cnt] = u, limit[cnt] = 0, cst[cnt] = -c;
}
void just_add(int u,int v,int f){
nxt[++cnt]=hd[u],hd[u]=cnt,to[cnt]=v,limit[cnt]=f;
}
int fr[N], fl[N], in[N], dis[N];
void calans(int x,int y,int w,int i){
auto in = [&](int id){
return id>=1&&id<=(n-1)*(m-1);
};
if(x==0&&in(y)){
r[1][y]+=w;
}
else if(in(x)&&y==(n-1)*(m-1)+1){
r[n][(x-1)%(m-1)+1]+=w;
}
else if(in(x)&&in(y)){
if(x+m-1==y){
r[(x-1)/(m-1)+2][(x-1)%(m-1)+1]+=w;
}
else if(x-(m-1)==y){
r[(y-1)/(m-1)+2][(y-1)%(m-1)+1]+=w;
}
else if(x+1==y){
c[(x-1)/(m-1)+1][(x-1)%(m-1)+1+1]+=w;
}
else if(x-1==y){
c[(y-1)/(m-1)+1][(y-1)%(m-1)+1+1]+=w;
}
}
};
pair<int, int> min_cost(int s, int t) {
int flow = 0;
ll cost = 0;
while (true) { // SPFA
queue<int> q;
memset(dis, 0x3f, sizeof(dis));
memset(in, 0, sizeof(in));
fl[s] = 1e9, dis[s] = 0, q.push(s);
while (!q.empty()) {
int cur = q.front();
q.pop(), in[cur] = 0;
for (int i = hd[cur]; i; i = nxt[i]) {
int it = to[i], d = dis[cur] + cst[i];
if (limit[i] && d < dis[it]) {
fl[it] = min(limit[i], fl[cur]), fr[it] = i, dis[it] = d;
if (!in[it]) in[it] = 1, q.push(it);
}
}
}
if (dis[t] > 1e9) return {flow, cost};//??>0?????
flow += fl[t], cost += (ll)dis[t] * fl[t];
for (int u = t; u != s; u = to[fr[u] ^ 1]) {
calans(to[fr[u]^1],u,fl[t]*cst[fr[u]],fr[u]);
limit[fr[u]] -= fl[t], limit[fr[u] ^ 1] += fl[t];
}
}
}
} sol2;
class Maxflow{
private:
int nedge=1,p[2*M],nex[2*M],head[N],c[2*M],cur[2*M];
int dist[2*N];
int S,T;
void Addedge(int a,int b,int v){
p[++nedge]=b;nex[nedge]=head[a];head[a]=nedge;
c[nedge]=v;
}
bool bfs(){
queue<int>q;
for(int i=S;i<=T;i++)dist[i]=-1;
dist[S]=0;q.push(S);
while(!q.empty()){
int now=q.front();q.pop();
for(int k=head[now];k;k=nex[k])if(dist[p[k]]==-1&&c[k]>0){
dist[p[k]]=dist[now]+1;
q.push(p[k]);
}
}
return dist[T]>-1;
}
int dfs(int x,int low){
if(x==T)return low;
if(low==0)return 0;
int used=0;
for(int &k=cur[x];k;k=nex[k])if(dist[p[k]]==dist[x]+1&&c[k]>0){
int a=dfs(p[k],min(c[k],low-used));
c[k]-=a;c[k^1]+=a;used+=a;
if(low==used)break;
}
if(used==0)dist[x]=-1;
return used;
}
public:
void init(int s,int t){
for(int i=S;i<=T;i++)head[i]=0;
S=s,T=t;
nedge=1;
}
void addedge(int a,int b,int v){
Addedge(a,b,v);
Addedge(b,a,0);
}
int dinic(SSP &sol2){
int flow=0;
while(bfs()){
for(int i=S;i<=T;i++)cur[i]=head[i];
flow+=dfs(S,1e9);
}
for(int i=S;i<=T;i++){
for(int k=head[i];k;k=nex[k])if(k%2==0){
sol2.just_add(i,p[k],c[k]);
sol2.just_add(p[k],i,c[k^1]);
}
}
return flow;
}
}sol1;
void solve()
{
cin>>n>>m>>k;
for(int i=1;i<=n;i++){
for(int j=1;j<m;j++)cin>>r[i][j];
}
for(int i=1;i<n;i++){
for(int j=1;j<=m;j++)cin>>c[i][j];
}
auto id = [&](int x,int y){
return (x-1)*(m-1)+y;
};
int s=0,t=(n-1)*(m-1)+1;
sol1.init(s,t);
auto addedge1 =[&](int x,int y,int w){
sol1.addedge(x,y,w);
sol1.addedge(y,x,w);
};
auto addedge2 =[&](int x,int y,int w=(int)1e9){
sol2.add(x,y,w,1);
sol2.add(y,x,w,1);
};
for(int j=1;j<m;j++){
addedge1(s,id(1,j),r[1][j]);
addedge1(id(n-1,j),t,r[n][j]);
}
for(int i=1;i+1<n;i++){
for(int j=1;j<m;j++){
addedge1(id(i,j),id(i+1,j),r[i+1][j]);
}
}
for(int i=1;i<n;i++){
for(int j=1;j+1<m;j++){
addedge1(id(i,j),id(i,j+1),c[i][j+1]);
}
}
sol2.init(t+1);
sol1.dinic(sol2);
for(int j=1;j<m;j++){
addedge2(s,id(1,j));
addedge2(id(n-1,j),t);
}
for(int i=1;i+1<n;i++){
for(int j=1;j<m;j++){
addedge2(id(i,j),id(i+1,j));
}
}
for(int i=1;i<n;i++){
for(int j=1;j+1<m;j++){
addedge2(id(i,j),id(i,j+1));
}
}
int t2=t+1;
sol2.add(t,t2,k,0);
auto [flow,cost]=sol2.min_cost(s,t2);
cout<<cost<<"\n";
for(int i=1;i<=n;i++){
for(int j=1;j<m;j++)cout<<r[i][j]<<" \n"[j==m-1];
}
for(int i=1;i<n;i++){
for(int j=1;j<=m;j++)cout<<c[i][j]<<" \n"[j==m];
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.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: 5708kb
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 4 1 15 7 1 12 13 3 6 3 6 1 2 5 2 15 3 4 2 3 2 3 3 3 1 1 1 2 2 2
result:
ok Correct. (2 test cases)
Test #2:
score: 0
Accepted
time: 2ms
memory: 5688kb
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 41 39 38 74 19 18 73 19 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 2 33 90 43 22 74 19 27 67 46 43 42 21 78 80 166 60 85 65 60 85 65 74 68 77 71 80 64 92 97 53 46 66 6 30 20 66 70 71 24 34 45 86 55 49 37 73 78 77 90 99 49 34 55 4 63 47 80 24 15 3 85 12 6 31 45 45...
result:
ok Correct. (125 test cases)
Test #3:
score: 0
Accepted
time: 0ms
memory: 5888kb
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 12 20 14 104 17 15 16 102 17 13 14 106 14 18 12 106 14 15 15 106 19 19 13 19 19 19 17 10 10 19 12 13 18 11 20 12 17 14 13 12 271 20 15 13 68 18 19 13 66 16 19 18 63 25 19 11 61 25 19 11 61 18 14 18 19 18 20 11 17 11 17 16 13 19 18 13 16 14 17 11 18 3 16 10 12 18 17 17 17 14 13 15 15 19 10 18 16 ...
result:
ok Correct. (80 test cases)
Test #4:
score: 0
Accepted
time: 2ms
memory: 3692kb
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 226850181 101764268 179381345 486537031 346100002 805792579 5081942...
result:
ok Correct. (55 test cases)
Test #5:
score: 0
Accepted
time: 2ms
memory: 3920kb
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 13642139 10307995 14193711 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: 2ms
memory: 5700kb
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 7771 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 17295 49299...
result:
ok Correct. (40 test cases)
Test #7:
score: 0
Accepted
time: 2ms
memory: 5608kb
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 261006 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: 3ms
memory: 5704kb
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 210 188 111 134 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 169 132 179 132 161 131 107 148 158 169 165 123 151 153 112 151 148 182 105 188 136 199 173 192 117 118 116 190 103 198 151 192 117 118 157 130 ...
result:
ok Correct. (24 test cases)
Test #9:
score: 0
Accepted
time: 2ms
memory: 5720kb
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 90000001 90000001 90000001 90000000 90000002 90000087 90000001 90000001 90000001 90000001 90000000 90000001 90000001 90000001 90000087 90000001 90000001 90000001 90000001 90000000 90000001 90000002 90000000 90000087 90000000 90000001 90000001 90000002 90000000 90000001...
result:
ok Correct. (20 test cases)
Test #10:
score: 0
Accepted
time: 8ms
memory: 5644kb
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 24 28 23 26 25 23 35 30 27 26 58 22 33 31 26 22 21 28 26 32 22 26 28 51 24 29 30 30 24 20 25 23 30 27 28 26 52 30 25 28 30 26 26 25 27 23 32 25 20 51 27 24 32 30 26 26 25 25 21 30 25 21 56 20 24 25 29 20 30 30 30 30 24 27 23 56 29 25 25 26 22 27 23 27 31 29 26 27 51 23 22 27 27 29 30 25 24...
result:
ok Correct. (10 test cases)
Test #11:
score: 0
Accepted
time: 4ms
memory: 5640kb
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 1001 1003 1001 1002 1003 1004 1004 1002 1001 1002 1001 1001 1003 1000 1001 1003 1001 1001 1069 1001 1003 1000 1003 1003 1004 1003 1001 1002 1002 1000 1002 1003 1001 1003 1000 1002 1000 1070 1002 1003 1...
result:
ok Correct. (10 test cases)
Test #12:
score: 0
Accepted
time: 2ms
memory: 5668kb
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: 0
Accepted
time: 1ms
memory: 5652kb
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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 14 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 14 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 14 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 14 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 14 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 14 1 1 1 1 1...
result:
ok Correct. (24 test cases)
Test #14:
score: -100
Time Limit Exceeded
input:
31 9 3 33 1 5 4 5 3 4 4 2 5 3 4 3 2 5 5 5 4 3 5 1 4 2 1 2 1 5 3 5 3 2 2 3 2 3 5 3 4 4 3 1 4 2 2 3 4 3 2 5 2 1 3 5 12 6 72 2 2 1 1 5 1 2 5 1 4 5 5 1 2 3 4 1 4 4 4 1 2 2 2 5 5 5 3 5 3 2 1 1 3 5 4 1 4 2 1 4 2 1 5 3 3 3 2 4 5 5 5 2 3 3 4 3 4 5 1 3 5 5 5 1 1 5 5 3 2 5 3 5 3 1 4 3 3 5 4 5 1 3 5 2 3 3 5 4 ...