装满的油箱(最短路变式)

有N个城市(编号0、1…N-1)和M条道路,构成一张无向图。

在每个城市里边都有一个加油站,不同的加油站的单位油价不一样。

现在你需要回答不超过100个问题,在每个问题中,请计算出一架油箱容量为C的车子,从起点城市S开到终点城市E至少要花多少油钱?

输入格式

第一行包含两个整数N和M。

第二行包含N个整数,代表N个城市的单位油价,第i个数即为第i个城市的油价pipi。

接下来M行,每行包括三个整数u,v,d,表示城市u与城市v之间存在道路,且车子从u到v需要消耗的油量为d。

接下来一行包含一个整数q,代表问题数量。

接下来q行,每行包含三个整数C、S、E,分别表示车子油箱容量、起点城市S、终点城市E。

输出格式

对于每个问题,输出一个整数,表示所需的最少油钱。

如果无法从起点城市开到终点城市,则输出”impossible”。

每个结果占一行。


emmmmm, 算是最短路的一个变式吧, 这道题并不问你到达终点的最短路, 而是问你到达终点时花费最少的油钱, 很容易和我们最短路的算法Dijkstra联系在一起, 将花费的价钱加入优先队列中, 当第一次弹出终点时, 此时的花费即为最少花费。我们可以想到, 用一个二维数组来记录,到哪个点, 剩下多少油的最小花费为dis[x][c],x是城市的编号, c是剩下的油量。如果c < C, 我们可以拓展一个新的状态dis[x][c + 1] = dis[x][c] + p[x], 并把它加到队列中, 而我们从一个城市到达另一个城市时, 又有一个新的状态dis[next][c - v] = dis[x][c], 这样我们跑一遍Dijkstra就行了。

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int INF = 0x3f3f3f3f;
const int MAXN = 2e5 + 100;
const int MAXM = 1e3 + 10;

template < typename T > inline void read(T &x) {
    x = 0; T ff = 1, ch = getchar();
    while(!isdigit(ch)) {
        if(ch == '-') ff = -1;
        ch = getchar();
    } 
    while(isdigit(ch)) {
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar(); 
    }
    x *= ff;
}

template < typename T > inline void write(T x) {
    if(x < 0) putchar('-'), x = -x;
    if(x > 9) write(x / 10);
    putchar(x % 10 + '0');
}

int n, m, T, c, s, t;
int p[MAXM], dis[MAXM][MAXM], vis[MAXM][MAXM];
int lin[MAXN], tot = 0;
struct edge {
    int y, v, next; 
}e[MAXN];

struct node {
    int d, x, c;
    bool operator < (const node &a) const {
        return a.d < d;
    }
};

inline void add(int xx, int yy, int vv) {
    e[++tot].y = yy;
    e[tot].v = vv;
    e[tot].next = lin[xx];
    lin[xx] = tot; 
}                

int BFS() {
    priority_queue < node > q;
    memset(vis, false, sizeof(vis));
    memset(dis, 0x3f, sizeof(dis));
    q.push(node{0, s, 0});
    dis[s][0] = 0;
    while(!q.empty()) {
        node x = q.top(); q.pop();
        int xx = x.x, xc = x.c;
        if(xx == t) return x.d;
        if(vis[xx][xc]) continue;
        vis[xx][xc] = true;
        if(xc < c) {
            if(dis[xx][xc + 1] > x.d + p[xx]) {
                dis[xx][xc + 1] = x.d + p[xx];
                q.push(node{dis[xx][xc + 1], xx, xc + 1}); 
            }
        }
        for(int i = lin[xx], y; i; i = e[i].next) {
            if(xc >= e[i].v) {
                if(dis[y = e[i].y][xc - e[i].v] > x.d) {
                    dis[y][xc - e[i].v] = x.d;
                    q.push(node{dis[y][xc - e[i].v], y, xc - e[i].v});
                }
            }
        }
    }
    return -1;
}

int main() {
    read(n); read(m);
    for(int i = 0; i < n; ++i) {
        read(p[i]);
    }
    for(int i = 1; i <= m; ++i) {
        int u, v, w;
        read(u); read(v); read(w);
        add(u, v, w);
        add(v, u, w);
    }
    read(T);
    while(T--) {
        read(c); read(s); read(t);
        int val = BFS();
        if(val == -1) puts("impossible");
        else write(val), puts("");
    }
    return 0;
} 

 

转载于:https://www.cnblogs.com/AK-ls/p/11423585.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值