poj3635Full Tank?[分层图最短路]

FullTank? 最短路径问题
本文介绍了一个基于最短路径算法的编程问题——FullTank?。该问题涉及在不同城市间旅行时寻找最经济的加油策略。文章通过示例详细解析了如何使用类似于迪杰斯特拉算法的方法来解决此问题,并提供了完整的代码实现。
Full Tank?
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7248 Accepted: 2338

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


题意:有了油量限制,每个点加油价格不一样,对于每个询问求最少花费

一看到d[i][j]这样的状态表示,马上想到分层图最短路,有点类似DP的思想
按油量分层,d[i][j]表示到节点i还有j个油的最小花费(不是最短路)
两种决策,加一个油或者直接走
感觉用Dijkstra写比较好
注意状态判重,多判一次也无所谓了
 
小优化:离线处理询问,相同起点同时处理
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
typedef long long ll;
const int N=1e3+5,M=1e4+5,V=105;
int read(){
    char c=getchar();int x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}
struct edge{
    int v,ne,w;
}e[M<<1];
int h[N],cnt=0;
void ins(int u,int v,int w){
    cnt++;
    e[cnt].v=v;e[cnt].w=w;e[cnt].ne=h[u];h[u]=cnt;
    cnt++;
    e[cnt].v=u;e[cnt].w=w;e[cnt].ne=h[v];h[v]=cnt;
}
int n,m,Q,u,v,w,p[N],c,st,ed;

struct hn{
    int u,d,f;
    bool operator <(const hn &rhs)const{return d>rhs.d;}
    hn(int a=0,int b=0,int c=0):u(a),d(b),f(c){}
};
int d[N][V];
bool done[N][V];
void bfs(int c,int st,int ed){
    memset(d,127,sizeof(d));
    memset(done,0,sizeof(done));
    priority_queue<hn> q;
    q.push(hn(st,0,0));
    d[st][0]=0;
    
    while(!q.empty()){
        hn x=q.top();q.pop();
        int u=x.u,f=x.f,cost=x.d;//printf("bfs %d %d\n",u,f);
        //if(done[u][f]) continue;
        done[u][f]=1;
        if(u==ed){printf("%d\n",cost);return;}
        
        if(f+1<=c&&!done[u][f+1]&&d[u][f]+p[u]<d[u][f+1]){
            d[u][f+1]=d[u][f]+p[u];
            q.push(hn(u,d[u][f+1],f+1));
        }
        for(int i=h[u];i;i=e[i].ne){
            int v=e[i].v,w=e[i].w;
            if(f>=w&&!done[v][f-w]&&d[v][f-w]>cost){
                d[v][f-w]=cost;
                q.push(hn(v,cost,f-w));
            }
        }
    }
    printf("impossible\n");
}

int main(){
    n=read();m=read();
    for(int i=0;i<n;i++) p[i]=read();
    for(int i=1;i<=m;i++){
        u=read();v=read();w=read();
        ins(u,v,w);
    }
    Q=read();
    for(int i=1;i<=Q;i++){
        c=read();st=read();ed=read();
        bfs(c,st,ed);
    }
}

 

 

转载于:https://www.cnblogs.com/candy99/p/5911049.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值