HDU 1676 Full Tank? 限制最短路(difficult)

Full Tank?

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 155    Accepted Submission(s): 71


Problem 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

有n个城市和m条路,一辆车要从s城市到e城市,而且这辆车能够乘的油的容量为cap,每走1单位距离就耗费1单位油。每个城市都有加油站,但是每个加油站的价格不同。让你判断这辆车能否到达城市e,如果能够达到,那么所要耗费的最少价格是多少。
每个城市油的价格不同,所以单纯的求最短路肯定行不通。用dp[i][j]来维护cost,代表到达到达第i个城市时,油箱的剩余油量为j时的最小花费。然后对于每个城市油量都是+1,然后更新与此城市相连的城市。
//250MS	4900K
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#define M 1007
using namespace std;
int n,m,cap,s,e,num;
int val[M],dp[M][M],head[M];
struct node
{
    int u,cost,res;//u代表节点,cost代表当前节点的花费,res代表此节点剩余的油
    bool operator < (const node a)const
    {
        return cost>a.cost;
    }
};
struct E
{
    int v,to,dist;
} edg[M*20];
vector<int>G[M];
void addedge(int u,int v,int dist)
{
    edg[num].v=v;
    edg[num].dist=dist;
    edg[num].to=head[u];
    head[u]=num++;
}
void init()
{
    num=0;
    memset(head,-1,sizeof(head));
    memset(val,0,sizeof(val));
    for(int i=0; i<n; i++)
        G[i].clear();
}
void bfs()
{
    for(int i=0; i<n; i++)
        for(int j=0; j<=cap; j++)
            dp[i][j]=0;
    priority_queue<node> q;
    q.push((node){s,0,0});
    while(!q.empty())
    {
        node x=q.top();
        q.pop();
        int u=x.u,cost=x.cost,res=x.res;
        if(u==e)
        {
            printf("%d\n",cost);
            return ;
        }
        if(res<cap&&(dp[u][res+1]==0||dp[u][res+1]>cost+val[u]))//如果多加1单位油,能够花费更少
        {
            dp[u][res+1]=cost+val[u];
            q.push((node){u,dp[u][res+1],res+1});
        }
        for(int i=head[u]; i!=-1; i=edg[i].to)//更新与此节点相邻的节点
        {
            int v=edg[i].v,dist=edg[i].dist;
            if(res<dist)continue;//当前油量不够到达下一个节点
            if(dp[v][res-dist]==0||dp[v][res-dist]>cost)//能够达到下一个节点,而且花费更少
            {
                dp[v][res-dist]=cost;
                q.push((node){v,cost,res-dist});
            }
        }
    }
    printf("impossible\n");
    return ;
}
int main()
{
    while( scanf("%d%d",&n,&m)!=EOF)
    {
        init();
        for(int i=0; i<n; i++)
            scanf("%d",&val[i]);
        int a,b,c;
        for(int i=1; i<=m; i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            addedge(a,b,c);
            addedge(b,a,c);
        }
        int q;
        scanf("%d",&q);
        while(q--)
        {
            scanf("%d%d%d",&cap,&s,&e);
            bfs();
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值