2013-05-07 11:01:03Morris

[UVA][dijkstra] 11367 - Full Tank?


  F: 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?

epsfbox{p11367.eps}

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$ le$n$ le$1000 and 0$ le$m$ le$10000 , the number of cities and roads. Then follows a line with n integers 1$ le$pi$ le$100 , where pi is the fuel price in the i th city. Then follow m lines with three integers 0$ le$u , v < n and 1$ le$d$ le$100 , telling that there is a road between u and v with length d . Then comes a line with the number 1$ le$q$ le$100 , giving the number of queries, and q lines with three integers 1$ le$c$ le$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


題目意思:
每個城市本身都有加油站,但每個加油站的油價不同,你可選加 x 升的油以方便抵達下一個城市。
s -> e 的最小花費是多少錢。

解法:
因為沒有規定一個要加滿,我們將狀態歸類成 dp[city][抵達時的油量] = 最小花費

因此轉換有兩個操作 (1) 多一升 (2) 花掉 distance 的油量抵達下一個城市。
用一個 priority_queue 實作 dijkstra。
 

#include <stdio.h>
#include <queue>
#include <vector>
using namespace std;
struct Node {
    int nd, f, mncost;
    bool operator<(const Node &A) const {
        return mncost > A.mncost;
    }
    Node(int a, int b, int c):
        nd(a), f(b), mncost(c) {}
};
struct Edge {
    int to, v;
    Edge(int a, int b):
        to(a), v(b) {}
};
vector<Edge> g[1005];
int price[1005];
int dp[1005][101];
void sol(int s, int e, int c, int n) {
    int i, j, k;
    Node tn(0,0,0);
    for(i = 0; i < n; i++)
        for(j = 0; j <= c; j++)
            dp[i][j] = 0xfffffff;
    priority_queue<Node, vector<Node> > pQ;
    dp[s][0] = 0;
    pQ.push(Node(s, 0, dp[s][0]));
    while(!pQ.empty()) {
        tn = pQ.top(), pQ.pop();
        if(tn.nd == e) {
            printf("%d\n", tn.mncost);
            return ;
        }
        // add one unit of fuel;
        if(tn.f != c) {
            if(dp[tn.nd][tn.f+1] > dp[tn.nd][tn.f] + price[tn.nd]) {
                dp[tn.nd][tn.f+1] = dp[tn.nd][tn.f] + price[tn.nd];
                pQ.push(Node(tn.nd, tn.f+1, dp[tn.nd][tn.f+1]));
            }
        }
        // goto next city
        for(vector<Edge>::iterator it = g[tn.nd].begin();
            it != g[tn.nd].end(); it++) {
            if(tn.f >= it->v) {
                if(dp[it->to][tn.f-(it->v)] > dp[tn.nd][tn.f]) {
                    dp[it->to][tn.f-(it->v)] = dp[tn.nd][tn.f];
                    pQ.push(Node(it->to, tn.f-(it->v), dp[tn.nd][tn.f]));
                }
            }
        }
    }
    puts("impossible");
}
int main() {
    int n, m, q;
    int x, y, d;
    int i, j, k;
    while(scanf("%d %d", &n, &m) == 2) {
        for(i = 0; i < n; i++) {
            scanf("%d", &price[i]);
            g[i].clear();
        }
        while(m--) {
            scanf("%d %d %d", &x, &y, &d);
            g[x].push_back(Edge(y, d));
            g[y].push_back(Edge(x, d));
        }
        scanf("%d", &q);
        while(q--) {
            scanf("%d %d %d", &d, &x, &y);
            sol(x, y, d, n);
        }
    }
    return 0;
}