• POJ 3635


    题目链接:http://poj.org/problem?id=3635

    Description

    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(1 le n le 1e3)$ 个城市和 $m(1 le m le 1e4)$ 条道路,构成一张无向图。在每个城市里有一个加油站,不同城市的加油站价格可能不同。通过一条道路的油耗即为该边的边权。

    现在有不超过 $100$ 个询问,每个询问要计算油箱容量为 $c$ 的车子能不能从 $s$ 城到达 $e$ 城,若能则给出最少油钱花费。

    题解:

    其实这种最短路变形已经见过很多次了,一般都是在原来 $d[v]$ 的基础上,再添加一维,变成二维状态的最短路。

    $d[v][r]$ 表示到达 $v$ 节点、油箱还剩 $r$ 单位的油的状态下,最少花费的油钱。

    写这种题,用优先队列Dijkstra的想防止出错的关键点:把入队和修改 $d[v][r]$ 绑定起来。

    AC代码:

    #include<cstdio>
    #include<algorithm>
    #include<queue>
    #include<vector>
    #include<cstring>
    using namespace std;
    const int INF=0x3f3f3f3f;
    const int maxn=1e3+10;
    const int maxm=1e4+10;
    const int maxc=100+10;
    
    int n,m,q;
    int p[maxn];
    
    struct Edge{
        int u,v,w;
        Edge(int _u=0,int _v=0,int _w=0){u=_u,v=_v,w=_w;}
    };
    vector<Edge> E;
    vector<int> G[maxn];
    void init(int l,int r)
    {
        E.clear();
        for(int i=l;i<=r;i++) G[i].clear();
    }
    void addedge(int u,int v,int w)
    {
        E.push_back(Edge(u,v,w));
        G[u].push_back(E.size()-1);
    }
    
    struct Qnode{
        int v;
        int d,r;
        Qnode(){}
        Qnode(int _v,int _d,int _r) {
            v=_v, d=_d, r=_r;
        }
        bool operator<(const Qnode& oth)const {
            return d>oth.d;
        }
    };
    int d[maxn][maxc],vis[maxn][maxc];
    int dijkstra(int c,int s,int t)
    {
        memset(d,0x3f,sizeof(d));
        memset(vis,0,sizeof(vis));
    
        priority_queue<Qnode> Q;
        d[s][0]=0, Q.push(Qnode(s,d[s][0],0));
        while(!Q.empty())
        {
            int u=Q.top().v, r=Q.top().r; Q.pop();
            if(u==t) return d[u][r];
            if(vis[u][r]) continue;
            else vis[u][r]=1;
    
            if(r<c) {
                d[u][r+1]=d[u][r]+p[u], Q.push(Qnode(u,d[u][r+1],r+1));
            }
            for(int i=0;i<G[u].size();i++)
            {
                Edge &e=E[G[u][i]]; int v=e.v;
                if(r<e.w || vis[v][r-e.w]) continue;
                if(d[v][r-e.w]>d[u][r]) {
                    d[v][r-e.w]=d[u][r], Q.push(Qnode(v,d[v][r-e.w],r-e.w));
                }
            }
        }
        return INF;
    }
    
    int main()
    {
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++) scanf("%d",&p[i]);
        init(1,n);
        for(int i=1,u,v,w;i<=m;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            addedge(u,v,w);
            addedge(v,u,w);
        }
        scanf("%d",&q);
        for(int i=1,c,s,t;i<=q;i++)
        {
            scanf("%d%d%d",&c,&s,&t);
            int ans=dijkstra(c,s,t);
            if(ans<INF) printf("%d
    ",ans);
            else printf("impossible
    ");
        }
    }
  • 相关阅读:
    bzoj5137 [Usaco2017 Dec]Standing Out from the Herd
    bzoj2434 [Noi2011]阿狸的打字机
    【20181024T2】小C的序列【GCD性质+链表】
    【20181024T3】小C的宿舍【分治】
    【20181024T1】小C的数组【二分+dp】
    【20181023T2】行星通道计划【二维BIT】
    【20181023T1】战争【反向并查集】
    【20181020T1】蛋糕
    【20181019T2】硬币【矩阵快速幂优化DP】
    【20181019T3】比特战争【最小生成树思想】
  • 原文地址:https://www.cnblogs.com/dilthey/p/9991643.html
Copyright © 2020-2023  润新知