[POJ3635] Full Tank?

Posted hihocoder

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[POJ3635] Full Tank?相关的知识,希望对你有一定的参考价值。

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

Source

Nordic 2007

题解

先简要说一下题意:(N)个点,(M)条边,每一条边都要消耗一定的油量,每一个点可以加不定量的油,且有一定的油费,现在给定油桶的容量,要从编号为(s)的点到编号为(t)的点,求最小费用

很明显的最短路,我用的是(Dijkstra),油一升一升的加即可

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
using namespace std;

const int N=2500,M=25000,C=150;
int n,m,c,s,t,p[N];
struct edge
{
    int v,w,nx;
}e[M];
int ne,h[N];
struct node
{
    int id,w,c;
    bool operator< (node tmp) const
    {
        return w>tmp.w;
    }
};
priority_queue<node> Q;
int v[N][C];

void Build(int u,int v,int w)
{
    ++ne,e[ne]=(edge){v,w,h[u]},h[u]=ne;
}

void Read()
{
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;++i) scanf("%d",&p[i]);
    int u,v,w;
    for(int i=1;i<=m;++i)
    {
        scanf("%d%d%d",&u,&v,&w);
        Build(u,v,w),
        Build(v,u,w);
    }
}

void Dijkstra()
{
    for(int i=0;i<n;++i)
        for(int j=0;j<=c;++j) v[i][j]=0;
    while(!Q.empty()) Q.pop();//注意还原
    Q.push((node){s,0,0});
    node tp;
    while(!Q.empty())
    {
        tp=Q.top();
        Q.pop();
        if(v[tp.id][tp.c]) continue;
        v[tp.id][tp.c]=1;
        if(tp.id==t)
        {
            printf("%d
",tp.w);
            return;
        }
        if(tp.c<c) Q.push((node){tp.id,tp.w+p[tp.id],tp.c+1});
        for(int i=h[tp.id];i;i=e[i].nx)
            if(tp.c>=e[i].w)
                Q.push((node){e[i].v,tp.w,tp.c-e[i].w});
    }
    puts("impossible");
}

int main()
{
    int T;
    Read();
    scanf("%d",&T);
    for(int i=1;i<=T;++i)
    {
        scanf("%d%d%d",&c,&s,&t),
        Dijkstra();
    }
    return 0;
}

以上是关于[POJ3635] Full Tank?的主要内容,如果未能解决你的问题,请参考以下文章

[POJ3635] Full Tank?

POJ3635 Full Tank?

poj3635Full Tank?[分层图最短路]

UVa 11367 Full Tank? (DP + Dijkstra)

poj3635 优先队列+打标记+广搜

图论补完计划poj 3635 (最短路变形)