poj 3635 Full Tank? 图上dp

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

题意:

已知每个点的加油站的油价单价(即点权),每条路的长度(边权)。

有q个询问,每个询问包括起点s、终点e和油箱容量。

问从起点走到终点的最小花费。如果不可达输出impossible,否则输出最小的旅途费用。

算法:

最直接的想法是 每到一个点都加上要走到下一个点所需要的油量。但是走的路不同,到底怎么处理加多少的问题呢?

因此想到分解状态,即拆点。每到一个点都+1单位的油量,然后把这个状态加入队列。另外如果现在油箱内的油足够达到下一点,

则更新状态,把新状态加入优先队列。

dp[i][j]表示到第i个城市剩余油量为j的花费。

简单的说,就是优先队列优化的最短路算法多了一维。

为了把所有可能的状态考虑到,

每到一个点只有两种操作:1、加一单位的油 2、更新能走到的下一个点。

代码:

#include
#include
#include
#include
#include
#define N 1005
#define M 10005
using namespace std;
int n,m,p[N],cnt,head[N],dp[N][110],que,ans;
int cap,st,ed;
bool vis[N][110];

struct Node{
    int id,oil,cost;
    bool operator <(const Node &b) const
    {
        return cost>b.cost;
    }
};

struct Edge{
    int to,nxt,val;
}edge[M*2];

void add(int x,int y,int z)
{
    cnt++;
    edge[cnt].nxt=head[x];
    edge[cnt].to=y;
    edge[cnt].val=z;
    head[x]=cnt;
}

bool bfs()
{
    memset(dp,0x3f,sizeof dp); memset(vis,0,sizeof vis);
    priority_queue q;
    Node now,cur,next;
    int id,cost,oil;
    now.id=st;
    now.cost=0;
    now.oil=0;
    q.push(now);
    while(!q.empty())
    {
        cur=q.top();
        q.pop();
        id=cur.id, cost=cur.cost, oil=cur.oil;
        if(id==ed)
        {
            ans=cost;
            return true;
        }
        if(vis[id][oil]) continue;
        vis[id][oil]=1;
        if(oilnext.id=id;
            next.oil=oil+1;
            next.cost=cost+p[id];
            if(!vis[id][next.oil] && dp[id][next.oil]>next.cost)
            {
                dp[id][next.oil]=next.cost;
                q.push(next);
            }
        }
        for(int i=head[id];i;i=edge[i].nxt)
        {
            int v=edge[i].to;
            int w=edge[i].val;
            if(oil>=w && dp[v][oil-w]>cost)
            {
                dp[v][oil-w]=cost;
                next.id=v;
                next.cost=cost;
                next.oil=oil-w;
                q.push(next);
            }
        }
    }
    return false;
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0;i"%d",&p[i]);
    for(int i=1;i<=m;i++)
    {
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        add(x,y,z); add(y,x,z);
    }
    scanf("%d",&que);
    for(int i=1;i<=que;i++)
    {
        scanf("%d%d%d",&cap,&st,&ed);
        if(bfs())
        {
            printf("%d\n",ans);
        }
        else printf("impossible\n");
    }
    return 0;
}

你可能感兴趣的:(——图论——)