poj3635 Full Tank? (优先队列bfs)

Full Tank?

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.

Input

The first line of input gives 1 ≤ n ≤ 1000 and 0 ≤ m ≤ 10000, the number of cities and roads. Then follows a line with n integers 1 ≤ pi ≤ 100, where pi is the fuel price in the ith city. Then follow m lines with three integers 0 ≤ 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 ≤ 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.

Output

For each query, output the price of the cheapest trip from s to e using a car with the given capacity, or “impossible” if there is no way of getting from s to e with the given car.

Sample Input

5 5
10 10 20 12 13
0 1 9
0 2 8
1 2 1
1 3 11
2 3 7
2
10 0 3
20 1 4

Sample Output

170
impossible

题意:

有n个城市(0-(n-1))和m条路。
在每个城市里边都有一个加油站,不同的加油站的单位油价不一样。
给出多个询问,在每个询问要求计算出一架油箱容量为C的车子,从起点城市S开到终点城市E至少要花多少油钱

分析:

d(a,b)表示到达a点时剩余b油的最小花费
跑的时候更新d数组就行了

code:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<algorithm>
#include<sstream>
const int inf=0x3f3f3f3f;
const int inn=-(1<<30);
using namespace std;
const int N=1e3+5,M=2e4+5,C=105;
int head[N],nt[M],to[M],w[M],cnt;//链式前向星
int p[N];//价格
int d[N][C];
bool mark[N][C];
int n,m;
struct Node{
    int d,u,c;
    bool operator <(const Node& a)const{
        return d>a.d;
    }
    Node(int dd,int uu,int cc){
        d=dd,u=uu,c=cc;
    }
};
priority_queue<Node>q;//d小的先出列
void init(){
    cnt=0;
    memset(head,-1,sizeof head);
}
void add(int x,int y,int z){
    cnt++;nt[cnt]=head[x];head[x]=cnt;to[cnt]=y;w[cnt]=z;
}
int bfs(int c,int st,int ed){
    while(!q.empty())q.pop();
    memset(mark,0,sizeof mark);
    memset(d,inf,sizeof d);
    d[st][0]=0;//不加这一句也行,但是慢了十几ms
    q.push(Node(0,st,0));
    while(!q.empty()){
        Node x=q.top();
        q.pop();
        if(x.u==ed)return x.d;//到终点直接return
        if(d[x.u][x.c]<x.d)continue;
        if(mark[x.u][x.c])continue;
        mark[x.u][x.c]=1;
        if(x.c<c){//如果可以原地加油
            if(d[x.u][x.c+1]>x.d+p[x.u]){
                d[x.u][x.c+1]=x.d+p[x.u];
                q.push(Node(d[x.u][x.c+1],x.u,x.c+1));
            }
        }
        for(int i=head[x.u];i!=-1;i=nt[i]){
            int v=to[i];
            if(x.c>=w[i]){//如果能去
                if(d[v][x.c-w[i]]>x.d){//判断是否更优
                    d[v][x.c-w[i]]=x.d;
                    q.push(Node(d[v][x.c-w[i]],v,x.c-w[i]));
                }
            }
        }
    }
    return -1;
}
signed main(){
    init();
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)scanf("%d",p+i);
    for(int i=0;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--){
        int c,st,ed;
        scanf("%d%d%d",&c,&st,&ed);
        int t=bfs(c,st,ed);
        if(t==-1){
            puts("impossible");
        }else{
            printf("%d\n",t);
        }
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值