1 2 3
1
4 1
3 4
1 4
out:3
ans:4
给组hack,可以有人帮忙调一下吗,感觉找不到问题。
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e5+5,inf=1e18;
int idx=1,e[N],w[N],ne[N],h[N],dep[N],s,t,q,p,r,now[N],dis[42][42][42];
inline int read(){
int a=1,b=0;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-') a=-a;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
b=(b<<1)+(b<<3)+(ch^48);
ch=getchar();
}
return a*b;
}
inline int lock(int a,int b,int c){
return a*41*41+b*41+c;
}
inline void add(int a,int b,int c){
// cout<<a/(41*41)<<" "<<a/41%41<<" "<<a%41<<endl;
// cout<<b/(41*41)<<" "<<b/41%41<<" "<<b%41<<endl;
// cout<<c<<endl;
// puts("");
// cout<<a<<" "<<b<<" "<<c<<endl;
e[++idx]=b;
w[idx]=c;
ne[idx]=h[a];
h[a]=idx;
e[++idx]=a;
w[idx]=0;
ne[idx]=h[b];
h[b]=idx;
}
// inline bool bfs(){
// }
inline int bfs(){
queue<int>q;
for(int i=0;i<=lock(41,41,41);i++){
dep[i]=inf;
}
q.push(s);
dep[s]=0;
now[s]=h[s];
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=h[u];i;i=ne[i]){
int v=e[i];
if(w[i]&&dep[v]==inf){
now[v]=h[v];
dep[v]=dep[u]+1;
q.push(v);
if(v==t) return 1;
}
}
}
return 0;
}
inline int dfs(int u,int flow){
if(u==t) return flow;
int res=0,k;
for(int i=now[u];i;i=ne[i]){
int v=e[i];
now[u]=i;
if(w[i]&&dep[v]==dep[u]+1){
k=dfs(v,min(flow,w[i]));
if(!k) dep[v]=inf;
w[i]-=k;
w[i^1]-=k;
res+=k;
flow-=k;
}
}
return res;
}
signed main(){
p=read(),q=read(),r=read();//r层p行q列
s=0,t=lock(41,41,41);
int d=read();
for(int i=1;i<=r;i++){
for(int j=1;j<=p;j++){
for(int k=1;k<=q;k++){
dis[i][j][k]=read();//i层j行k列
}
}
}
for(int i=1;i<=p;i++){
for(int j=1;j<=q;j++){
add(0,lock(1,i,j),dis[1][i][j]);
add(lock(r,i,j),t,1e18);
// e[0][0][0].push_back({{1,i,j},dis[1][j][k]});
// e[r][i][j].push_back({{r+1,i,j},1e18});
}
}
for(int i=1;i<r;i++){
for(int j=1;j<=p;j++){
for(int k=1;k<=q;k++){
add(lock(i,j,k),lock(i+1,j,k),dis[i+1][j][k]);
}
}
}
for(int i=d+1;i<=r;i++){
for(int j=1;j<=p;j++){
for(int k=1;k<=q;k++){
if(j!=1) add(lock(i,j,k),lock(i-d,j-1,k),dis[i-d][j-1][k]);
if(j!=p) add(lock(i,j,k),lock(i-d,j+1,k),dis[i-d][j+1][k]);
if(k!=1) add(lock(i,j,k),lock(i-d,j,k-1),dis[i-d][j][k-1]);
if(k!=q) add(lock(i,j,k),lock(i-d,j,k+1),dis[i-d][j][k+1]);
}
}
}
int ans=0;
while(bfs()){
ans+=dfs(s,inf);
}
cout<<ans<<endl;
return 0;
}