网络流24题之T15 汽车加油行驶问题

这题。。根本和网络流有关系吗?不是构造层次图跑一边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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值