QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#621600 | #7737. Extending Distance | rotcar07 | WA | 3ms | 10044kb | C++20 | 3.6kb | 2024-10-08 15:37:31 | 2024-10-08 15:37:31 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
constexpr int N=505,maxn=1805,maxm=1e4+5;
int n,m,k,tot=1,s,t,fir[maxn],nxt[maxm],firr[maxn],val[maxm],cst[maxm],to[maxm];
inline void addedge(int u,int v,int w,int c){
nxt[++tot]=firr[u],firr[u]=tot,val[tot]=w,to[tot]=v,cst[tot]=c;
}
inline void add(int u,int v,int w,int c){addedge(u,v,w,c),addedge(v,u,0,-c);}
typedef long long ll;
int dis[maxn];bool vis[maxn];
namespace dinic{
bool bfs(){
memset(dis,0,sizeof dis);
memcpy(fir,firr,sizeof fir);
dis[s]=1;
queue<int> q;
q.push(s);
while(!q.empty()){
int p=q.front();q.pop();
if(p==t) return 1;
for(int i=fir[p];i;i=nxt[i])if(!dis[to[i]]&&val[i]) dis[to[i]]=dis[p]+1,q.push(to[i]);
}
return 0;
}
int dinic(int p,int fl){
if(p==t) return fl;
int res=0;
for(int &i=fir[p];i;i=nxt[i])if(val[i]&&dis[to[i]]==dis[p]+1){
int z=dinic(to[i],min(fl,val[i]));
val[i]-=z,val[i^1]+=z,res+=z,fl-=z;
}
if(!res) dis[p]=0;
return res;
}
}
namespace MCMF{
bool bfs(){
memset(dis,0x3f,sizeof dis);
memcpy(fir,firr,sizeof fir);
dis[s]=0;
queue<int> q;int z=dis[1];
q.push(s);vis[s]=1;
while(!q.empty()){
int p=q.front();q.pop();vis[p]=0;
for(int i=fir[p];i;i=nxt[i])if(dis[to[i]]>dis[p]+cst[i]&&val[i]){
dis[to[i]]=dis[p]+cst[i];
if(!vis[to[i]])q.push(to[i]),vis[to[i]]=1;
}
}
return dis[t]<z;
}
int cost=0;
int dinic(int p,int fl){
if(p==t) return fl;
vis[p]=1;
int res=0;
for(int &i=fir[p];i;i=nxt[i])if(!vis[to[i]]&&val[i]&&dis[to[i]]==dis[p]+cst[i]){
int z=dinic(to[i],min(fl,val[i]));cost+=cst[i]*1ll*z;
val[i]-=z,val[i^1]+=z,res+=z,fl-=z;
}
if(!res) dis[p]=1e9;vis[p]=0;
return res;
}
}
constexpr int inf=2e9;
int rs[N][N],cs[N][N];
int ori[N][N],oci[N][N];
inline void solve(){
cin>>n>>m>>k;n++,m--;
s=0,t=n*m+1;
for(int i=0;i<=n*m+2;i++) firr[i]=0;tot=1;
auto f=[&](int x,int y){return (x-1)*m+y;};
int S=t+1;add(s,S,inf,0);add(S,s,0,0);
for(int i=1;i<=m;i++) add(S,f(1,i),inf,0),add(f(n,i),t,inf,0);
for(int i=1;i<n;i++)
for(int j=1;j<=m;j++){
int x;cin>>x;oci[i][j]=x;
add(f(i,j),f(i+1,j),x,0);
add(f(i+1,j),f(i,j),x,0);
}
for(int i=2;i<n;i++)
for(int j=0;j<=m;j++){
int x;cin>>x;
ori[i][j]=x;
if(j==0||j==m) continue;
add(f(i,j),f(i,j+1),x,0);
add(f(i,j+1),f(i,j),x,0);
}
int cur=0;
while(dinic::bfs()) cur+=dinic::dinic(s,inf);
val[2]=k;
for(int i=2;i<n;i++)
for(int j=1;j<m;j++) add(f(i,j),f(i,j+1),inf,1),add(f(i,j+1),f(i,j),inf,1),rs[i][j]=tot;
for(int i=1;i<n;i++)
for(int j=1;j<=m;j++) add(f(i,j),f(i+1,j),inf,1), add(f(i+1,j),f(i,j),inf,1),cs[i][j]=tot;
cur=0;
while(MCMF::bfs()) cur+=MCMF::dinic(s,1e9);
assert(cur==k);
cout<<MCMF::cost<<'\n';MCMF::cost=0;
for(int i=1;i<n;i++,cout<<'\n')
for(int j=1;j<=m;j++) cout<<max(val[cs[i][j]],val[cs[i][j]-2])+oci[i][j]<<' ';
for(int i=2;i<n;i++,cout<<'\n')
for(int j=0;j<=m;j++){
if(!j||j==m) cout<<ori[i][j]<<' ';
else cout<<ori[i][j]+max(val[rs[i][j]],val[rs[i][j]-2])<<' ';
}
}
main(){
std::ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int t;cin>>t;
while(t--) solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 9756kb
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 7 1 12 13 3 6 3 6 3 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: 10004kb
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 38 53 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 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 63 73 78 77 90 99 49 10 55 4 63 47 ...
result:
ok Correct. (125 test cases)
Test #3:
score: 0
Accepted
time: 3ms
memory: 10044kb
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 17 15 16 102 16 14 14 106 12 20 12 106 12 17 15 106 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 18 19 13 66 10 19 18 69 19 19 12 66 19 19 12 66 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 1...
result:
ok Correct. (80 test cases)
Test #4:
score: -100
Wrong Answer
time: 2ms
memory: 9844kb
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 264106783 163628126 318085983 226850181 101764170 179381345 486537031 346100002 805792579 50...
result:
wrong answer The length of shortest path shoult extend exactly K. (test case 4)