#include<bits/stdc++.h>
#define reg register
using namespace std;
inline int read() {
int x=0,f=1;char s=getchar();
while (s>'9'||s<'0') {
if (s=='-') f=-f;
s=getchar();
}
while (s>='0'&&s<='9') {
x=x*10+s-'0';
s=getchar();
}
return x*f;
}
const int N = 210;
const int INF = 1e9;
const int xx[4]={1,-1,0,0};
const int yy[4]={0,0,-1,1};
int n,K,a,b,c,dist[N][N][21],ans;
int mmap[N][N];
bool d[N][N][21];
struct node{
int x,y,k,dist;
bool operator < (const node &a) const{
return a.dist<dist;
}
};
priority_queue< node > q;
bool judge(int x,int y) {
return x>=1&&x<=n&&y>=1&&y<=n;
}
void dijkstra() {
node st={1,1,K,0};
dist[1][1][K]=0;
q.push(st);
while (!q.empty()) {
node root=q.top();q.pop();
if (d[root.x][root.y][root.k]) continue;
d[root.x][root.y][root.k]=true;
int x=root.x,y=root.y;
if (mmap[x][y]) {
if (dist[x][y][K]>dist[x][y][root.k]+a) {
dist[x][y][K]=dist[x][y][root.k]+a;
node h={x,y,K,dist[x][y][K]};
q.push(h);
continue;
}
}
else {
if (dist[x][y][K]>dist[x][y][root.k]+a+c) {
dist[x][y][K]=dist[x][y][root.k]+a+c;
node h={x,y,K,dist[x][y][K]};
q.push(h);
}
}
if (root.k==0) continue;
for (int i=0;i<4;++i) {
int lx=root.x+xx[i],ly=root.y+yy[i];
if (!judge(lx,ly)) continue;
int w=0;
if (xx[i]==-1||yy[i]==-1) w=b;
if (dist[lx][ly][root.k-1]>dist[root.x][root.y][root.k]+w) {
dist[lx][ly][root.k-1]=dist[root.x][root.y][root.k]+w;
node h={lx,ly,root.k-1,dist[lx][ly][root.k-1]};
q.push(h);
}
}
}
}
int main(){
n=read();K=read();a=read();b=read();c=read();
for (reg int i=1;i<=n;++i)
for (reg int j=1;j<=n;++j) mmap[i][j]=read();
for (reg int i=1;i<=n;++i)
for (reg int j=1;j<=n;++j)
for (reg int l=0;l<=K;++l) dist[i][j][l]=INF;
dijkstra();
ans=INF;
for (reg int i=0;i<=K;++i) ans=min(ans,dist[n][n][i]);
printf("%d\n",ans);
return 0;
}
虽然知道了2的样例并且很不要脸的过了,但是还是觉得疑惑。
input:
7 5 4 5 100
0 0 0 0 0 0 0
0 0 0 0 0 0 0
1 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 1 1 1 1 0 0
output:
17
但是这份代码数出来是16。。。心累