Full Tank?

链接:https://ac.nowcoder.com/acm/problem/51026
After going through the receipts from your car trip through Europe this summer,
you realised that the gas prices varied between the cities you visited.
Maybe you could have saved some money if you were a bit more clever about where you filled your fuel?

To help other tourists (and save money yourself next time),
you want to write a program for finding the cheapest way to travel between cities,
filling your tank on the way.
We assume that all cars use one unit of fuel per unit of distance, and
start with an empty gas tank.

The first line of input gives 10001≤n≤1000 and 0≤m≤10000,
the number of cities and roads. Then follows a line with n integers 1≤p ≤100,
where pi is the fuel price in the ith city.
Then follow m lines with three integers0≤u,v<n and 1≤d≤100, 
telling that there is a road between u and v with length d. 
Then comes a line with the number 1 \leq q \leq 1001≤q≤100, 
giving the number of queries, 
and q lines with three integers 1≤c≤100, s and e, 
where c is the fuel capacity of the vehicle,
 s is the starting city, and e is the goal.

在处理好输入输出之后,关键从集合的角度考虑最小花费。决策类问题带有动态规划的思想,用第一个维度表示从起点到达的城市,第二个维度表示当前油箱剩余的油量,f[i,j]表示从起点到i城剩余油量为j的最小花费。
然后使用一个小根堆来维护这个状态,这样可以保证第一次得到终点时的状态是最优解
这道题难在无法判断在某个城市应该加多少油,因此我们一点一点的加。每次加一单位的油并假如优先队列,直到到达终点即可。

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
#define x first
#define y second
#define N 1010
#define M 10010

int n,m;
int S,T,c;
int pi[M];
int e[M*2],ne[M*2],h[N],w[2*M],idx=0;
int f[N][N];  //动态规划用
bool st[N][110];

struct Cost{
    int city,fl,s; //到达的城市,剩余的油量,花费
    bool operator<(const Cost W)const{
        return W.s<s;
    }
};

void add(int a,int b,int c){
    e[idx]=b,w[idx]=c;
    ne[idx]=h[a],h[a]=idx++;
}

int bfs(int S,int T,int c){
    memset(st,0,sizeof st);
    memset(f,0x3f,sizeof f);
    
    priority_queue<Cost> heap;
    heap.push({S,0,0});
    f[S][0]=0;
    
    while(heap.size()){
        auto u=heap.top();heap.pop();
        int city = u.city , fuel = u.fl , spent = u.s ;
        
        if(st[city][fuel]) continue;
        st[city][fuel]=true;
        
        if(city==T){
            return spent;
        }
        
        for(int i=h[city];~i;i=ne[i]){
            int j=e[i];
            if(fuel>=w[i]&&f[city][fuel]<f[j][fuel-w[i]]){
                f[j][fuel-w[i]]=f[city][fuel];
                heap.push({j,fuel-w[i],f[j][fuel-w[i]]});
            }
        }
        
        if(fuel<c){
            if(f[city][fuel]+pi[city]<f[city][fuel+1]){
                f[city][fuel+1]=f[city][fuel]+pi[city];
                heap.push({city,fuel+1,f[city][fuel+1]});
            }
        }
        
    }
    return -1;
}

int main(){
    memset(h,-1,sizeof h);
    
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
        scanf("%d",&pi[i]);
    
    for(int i=1;i<=m;i++){
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        add(a,b,c),add(b,a,c);
    }
    
    int q;
    scanf("%d",&q);
    while(q--){
        scanf("%d%d%d",&c,&S,&T);
        int res=bfs(S,T,c);
        if(res==-1) puts("impossible");
        else printf("%d\n",res);
    }
    return 0;
}

借鉴自一篇出色的bloghttps://blog.csdn.net/qq_41822647/article/details/89682221?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1.control

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值