网络流24题15
https://loj.ac/problem/6223
bfs
#include <bits/stdc++.h> using namespace std; const int maxn = 109; int mapp[maxn][maxn]; int cost[maxn][maxn][11]; int dir[4][2] = {1,0,-1,0,0,1,0,-1}; struct node{ int x,y,canmove,c; }; int main() { int n,k,a,b,c; scanf("%d%d%d%d%d",&n,&k,&a,&b,&c); for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ scanf("%d",&mapp[i][j]); } } memset(cost,0x3f,sizeof(cost)); cost[1][1][k] = 0; queue<node> q; q.push((node){1,1,k,0}); while(!q.empty()){ node now = q.front(); q.pop(); for(int i=0;i<4;i++){ int nx = now.x+dir[i][0]; int ny = now.y+dir[i][1]; if(nx<1||nx>n||ny<1||ny>n) continue; int ncanmoce = now.canmove-1; int nc = now.c; if(ncanmoce<0) continue; if(nx<now.x || ny <now.y) nc+=b; if(mapp[nx][ny]==1){ nc+=a; ncanmoce = k; } if(nc<cost[nx][ny][ncanmoce]){ cost[nx][ny][ncanmoce] = nc; q.push((node){nx,ny,ncanmoce,nc}); } } if(now.c+c+a<cost[now.x][now.y][k]){ cost[now.x][now.y][k] = now.c+c+a; q.push((node){now.x,now.y,k,now.c+c+a}); } } int ans = 0x3f3f3f3f; for(int i=0;i<11;i++){ ans = min(ans,cost[n][n][i]); } printf("%d\n",ans); return 0; }