汽车加油行驶问题
网络流24题中的,单好像不用费用流水过更快?(我没测过)
广搜搞一下,按剩余油量分层。
#include <queue>
#include <cstdio>
#include <cstring>
const int dx[4]= {0,0,1,-1},dy[4]= {1,-1,0,0};
int n,k,a,b,c,g[105][105],dis[105][105][15],ans=0x3f3f3f3f;
struct node {int x,y,rest,money;};
std::queue<node>q;
int main() {
scanf("%d%d%d%d%d",&n,&k,&a,&b,&c);
// printf("%d %d %d\n",a,b,c);
for(int i=1; i<=n; i++)for(int j=1; j<=n; j++)scanf("%d",&g[i][j]);
std::memset(dis,0x3f,sizeof dis);
dis[1][1][k]=0;node u;
u.money=0,u.rest=k,u.x=1,u.y=1;
q.push(u);
while(!q.empty()) {
u=q.front();
q.pop();
int x=u.x,y=u.y;
for(int i=0; i<4; i++) {
int nx=dx[i]+x,ny=dy[i]+y,cost=u.money;
if(nx<=0||nx>n||ny<=0||ny>n)continue;
if(dx[i]<0||dy[i]<0) cost+=b;
if(g[nx][ny]) {
cost+=a;
if(dis[nx][ny][k]<=cost)continue;
dis[nx][ny][k]=cost;
q.push({nx,ny,k,cost});
}
else {
if(u.rest==1){
if(nx==n&&ny==n) if(ans>cost){ans=cost;continue;}
cost+=c+a;
if(cost<dis[nx][ny][k])dis[nx][ny][k]=cost;
else continue;
q.push({nx,ny,k,cost});
}
else {
if(nx==n&&ny==n) if(ans>cost){ans=cost;continue;}
bool flag=0;
for(int z=u.rest-1;z<=k;z++)
if(dis[nx][ny][z]<=cost) {flag=1;break;}
if(flag) continue;
dis[nx][ny][u.rest-1]=cost;
q.push({nx,ny,u.rest-1,cost});
}
}
}
}
printf("%d",ans);
}
原文地址:https://www.cnblogs.com/sdfzhsz/p/9263899.html
时间: 2024-09-29 23:20:22