这题。。根本和网络流有关系吗?不是构造层次图跑一边SPFA就好了吗。
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std ;
const int maxn=110;
const int maxk=15;
const int oo=0x3f3f3f;
int n,k,a,b,c;
int map[maxn][maxn];
int dist[maxn][maxn][maxk],inq[maxn][maxn][maxk];
typedef struct NODE{
int x,y;
int k;
NODE (){
x=y=k=0;
}
NODE (int a,int b,int c){
x=a,y=b,k=c;
}
}N;
queue <N> q;
void init (){
freopen ("prog815.in","r",stdin);
freopen ("prog815.out","w",stdout);
cin >>n >>k >>a >>b >>c;
int i,j;
for (i=1;i<=n;i++){
for (j=1;j<=n;j++){
scanf ("%d",&map[i][j]);
}
}
memset (inq,0,sizeof(inq));
memset (dist,0x3f,sizeof(dist));
}
void change (N now,N next,int cost){
if (dist[now.x][now.y][now.k]+cost < dist[next.x][next.y][next.k]){
dist[next.x][next.y][next.k]=dist[now.x][now.y][now.k]+cost;
if (!inq[next.x][next.y][next.k]) {
q.push (next);
inq[next.x][next.y][next.k]=1;
}
}
}
void work (){
q.push (N(1,1,k));
inq[1][1][k]=1;
dist[1][1][k]=0;
N now,next;
while (!q.empty ()){
now=q.front ();
q.pop ();
inq[now.x][now.y][now.k]=0;
if(now.k!=k&&map[now.x][now.y]) {
change (now,N(now.x,now.y,k),a);
continue;
}
if(now.k!=k) change(now,N(now.x,now.y,k),a+c);
if(now.k!=0){
if(now.x<n) change(now,N(now.x+1,now.y,now.k-1),0);
if(now.y<n) change(now,N(now.x,now.y+1,now.k-1),0);
if(now.x>1) change(now,N(now.x-1,now.y,now.k-1),b);
if(now.y>1) change(now,N(now.x, now.y-1,now.k-1),b);
}
}
int ans=oo;
for (int i=0;i<=k;i++){
ans=min (ans,dist[n][n][i]);
}
cout <<ans;
}
int main (){
init ();
work ();
return 0;
}