POJ3635 Full Tank? #最短路径 动态规划#

Full Tank?

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10618   Accepted: 3563

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 ≤ uv < 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

Source

Nordic 2007

Solution

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;

const int maxn = 1e3 + 10;
const int maxm = 1e4 + 10;
const int maxc = 1e2 + 10;
const int inf = 0x3f3f3f3f;
bool vis[maxn][maxc];
int n, m, t, tot, price[maxn], head[maxn];
int dp[maxn][maxc]; // 走到点 i 时剩余汽油 j 时的最小花费
struct edge { int v, w, nxt; } e[maxm << 1];
struct node
{
    int id, sum, rem; // 当前点,历史花费,剩余汽油
    node(int id, int sum, int rem)
    {
        this->id = id;
        this->sum = sum;
        this->rem = rem;
    }
    bool operator <(const node& x) const { return sum > x.sum; }
};
priority_queue<node> q; // 优先队列保存状态

void addEdge(int u, int v, int w) // 链式前向星存图
{
    e[tot].v = v;
    e[tot].w = w;
    e[tot].nxt = head[u];
    head[u] = tot++;
}

int bfs(int src, int des, int cap)
{
    memset(dp, 0x3f, sizeof(dp));
    memset(vis, false, sizeof(vis));
    while (!q.empty()) q.pop(); // 清空上次可能存在残留的优先队列
    node s = node(src, 0, 0);
    q.push(s);
    dp[src][0] = 0;
    while (!q.empty())
    {
        node now = q.top();
        q.pop();
        if (vis[now.id][now.rem]) continue; // 如果访问过当前状态
        vis[now.id][now.rem] = true;
        if (now.id == des) return now.sum; // 到达终点
        if (now.rem < cap && !vis[now.id][now.rem + 1] && dp[now.id][now.rem + 1] > dp[now.id][now.rem] + price[now.id]) // 如果容量足够且没有访问过状态且能够优化
        {
            dp[now.id][now.rem + 1] = dp[now.id][now.rem] + price[now.id];
            q.push(node(now.id, dp[now.id][now.rem + 1], now.rem + 1));
        }
        for (int i = head[now.id]; ~i; i = e[i].nxt)
        {
            node nxt = node(e[i].v, now.sum, now.rem - e[i].w);
            if (nxt.rem >= 0 && !vis[nxt.id][nxt.rem] && dp[nxt.id][nxt.rem] > now.sum) // 如果余量足够且没有访问过状态且能够优化
            {
                q.push(nxt);
                dp[nxt.id][nxt.rem] = now.sum;
            }
        }
    }
    return -1; // 无解
}

int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("input.txt", "r", stdin);
#endif
    std::ios::sync_with_stdio(false);
    memset(head, -1, sizeof(head));
    cin >> n >> m;
    for (int i = 0; i < n; i++) cin >> price[i];
    while (m--)
    {
        int u, v, w;
        cin >> u >> v >> w;
        addEdge(u, v, w);
        addEdge(v, u, w);
    }
    cin >> t;
    while (t--)
    {
        int cap, src, des;
        cin >> cap >> src >> des;
        int res = bfs(src, des, cap);
        if (res == -1) cout << "impossible" << endl;
        else cout << res << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_35850147/article/details/105930927