QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#593839 | #7737. Extending Distance | daduoli | WA | 2ms | 16132kb | C++23 | 3.9kb | 2024-09-27 16:21:44 | 2024-09-27 16:21:45 |
Judging History
answer
#include<bits/stdc++.h>
#define Yzl unsigned long long
#define mp make_pair
#define pi pair<int,int>
#define fi first
#define se second
#define pb emplace_back
typedef long long LL;
using namespace std;
const Yzl Lty=20120712;
const int MAXN=510,N=MAXN*MAXN,M=N*20;
const LL inf=1e18;
int n,m,k;
int a[MAXN][MAXN],b[MAXN][MAXN];
struct Flow {
int S,T;
struct EDGE {
int f,t;
LL w,c;
}que[M];
int h[N],cnt;
void add(int f,int t,LL w,LL c) {
que[++cnt]=(EDGE){h[f],t,w,c};
h[f]=cnt;
}
void adline(int f,int t,LL w,LL c) {
// cout<<f<<" "<<t<<' '<<w<<endl;
add(f,t,w,c);
add(t,f,0,-c);
}
LL dis[N],cur[N];
bool bfs() {
for(int i=1;i<=T;++i) {
dis[i]=-2;
cur[i]=h[i];
} dis[S]=0; queue<int> q; q.push(S);
while(!q.empty()) {
int u=q.front(); q.pop();
for(int i=h[u];i;i=que[i].f) {
int t=que[i].t;
if(!que[i].w||dis[t]!=-2) continue;
dis[t]=dis[u]+1; q.push(t);
if(t==T) return true;
}
} return false;
}
LL dfs(int u,LL flow) {
int res=0;
if(u==T) return flow;
for(int i=cur[u];i&&flow;i=que[cur[u]].f) {
int t=que[i].t; cur[u]=i;
if(dis[t]!=dis[u]+1||!que[i].w) continue;
int k=dfs(t,min(que[i].w,flow));
if(!k) dis[t]=-2;
flow-=k; res+=k;
que[i].w-=k; que[i^1].w+=k;
} return res;
}
void dinic() {
LL ans=0;
while(bfs()) ans+=dfs(S,inf);
// cout<<ans<<":";
}
void clear() {
for(int i=1;i<=n*m+2;++i) h[i]=0;
cnt=1;
}
int prep[N],pree[N];
LL fl[N];
void EK() {
for(int i=1;i<=T;++i) dis[i]=inf,fl[i]=0;
fl[S]=inf; dis[S]=0; queue<int> q; q.push(S);
while(!q.empty()) {
int u=q.front(); q.pop();
for(int i=h[u];i;i=que[i].f) {
int t=que[i].t;
if(!que[i].w||dis[u]+que[i].c>=dis[t]) continue;
q.push(t); dis[t]=dis[u]+que[i].c;
prep[t]=u; pree[t]=i; fl[t]=min(fl[u],que[i].w);
}
}
}
void del(LL x) {
for(int i=T;i!=S;i=prep[i]) {
que[pree[i]].w-=x;
que[pree[i]^1].w+=x;
}
}
void calc() {
LL lp=0,ans=0;
while(1) {
EK(); LL res=fl[T];
if(lp+fl[T]>k) {
res=k-lp;
lp=k;
} else lp+=fl[T];
ans+=res*dis[T];
del(res); if(lp==k) break;
} printf("%lld\n",ans);
for(int i=2;i<=cnt;++i) {
if(que[i].c<0) {
int f=que[i].f,t=que[i].t;
if(f>t) swap(f,t);
if((f-1)/(m-1)!=(t-1)/(m-1)) {
if(f==S) a[1][(f-1)%(m-1)+1]+=que[i].w;
else if(t==T) a[n][(f-1)%(m-1)+1]+=que[i].w;
else a[(f-1)/(m-1)+2][(f-1)%(m-1)+1]+=que[i].w;
} else {
b[(f-1)/(m-1)+1][(f-1)%(m-1)+2]+=que[i].w;
}
}
}
}
}D;
void vmain() {
scanf("%d%d%d",&n,&m,&k); D.S=(n-1)*(m-1)+1; D.T=D.S+1;
D.clear();
for(int i=1;i<=n;++i) {
for(int j=1;j<m;++j) {
int x; scanf("%d",&x); a[i][j]=x;
if(i==1) D.adline(D.S,(i-1)*(m-1)+j,x,0);
else if(i==n) D.adline((i-2)*(m-1)+j,D.T,x,0);
else {
D.adline((i-2)*(m-1)+j,(i-1)*(m-1)+j,x,0);
D.adline((i-1)*(m-1)+j,(i-2)*(m-1)+j,x,0);
}
}
}
for(int i=1;i<n;++i) {
for(int j=1;j<=m;++j) {
int x; scanf("%d",&x); b[i][j]=x;
if(j==1||j==m) continue;
D.adline((i-1)*(m-1)+j-1,(i-1)*(m-1)+j,x,0);
D.adline((i-1)*(m-1)+j,(i-1)*(m-1)+j-1,x,0);
}
} D.dinic();
for(int i=1;i<=n;++i) {
for(int j=1;j<m;++j) {
if(i==1) D.adline(D.S,(i-1)*(m-1)+j,inf,1);
else if(i==n) D.adline((i-2)*(m-1)+j,D.T,inf,1);
else {
D.adline((i-2)*(m-1)+j,(i-1)*(m-1)+j,inf,1);
D.adline((i-1)*(m-1)+j,(i-2)*(m-1)+j,inf,1);
}
}
}
for(int i=1;i<n;++i) {
for(int j=1;j<=m;++j) {
if(j==1||j==m) continue;
D.adline((i-1)*(m-1)+j-1,(i-1)*(m-1)+j,inf,1);
D.adline((i-1)*(m-1)+j,(i-1)*(m-1)+j-1,inf,1);
}
} D.calc();
for(int i=1;i<=n;++i) {
for(int j=1;j<m;++j) printf("%d ",a[i][j]);
puts("");
}
for(int i=1;i<n;++i) {
for(int j=1;j<=m;++j) printf("%d ",b[i][j]);
puts("");
}
}
int main () {
int T=1; scanf("%d",&T); while(T--) vmain();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 2ms
memory: 16132kb
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 4 1 2 3 3 3 1 1 1 2 2 2
result:
wrong answer The length of shortest path shoult extend exactly K. (test case 2)