zoukankan      html  css  js  c++  java
  • 装满的油箱(最短路变式)

    有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;
    } 
  • 相关阅读:
    nginx限制某个IP同一时间段的访问次数
    在64位机器上使用yum install特定指定安装32位的库
    pl sql developer连接oracle数据库提示:ORA12541: TNS: no listener
    [转]Eclipse中几种加入包方式的区别
    Oracle安装出现Can't connect to X11 window
    [转]ORACLE10GR2,dbca时ora27125错误解决方法
    [转]教你如何进入google国际版 不跳转g.cn 及 opendns
    安装oracle遇到的故障
    [转]宝文!Apple Push Notification Service (APNS)原理与实现方案
    [转]关于安卓与ios的推送系统,我说说自己的看法。
  • 原文地址:https://www.cnblogs.com/AK-ls/p/11423585.html
Copyright © 2011-2022 走看看