码迷,mamicode.com
首页 > 其他好文 > 详细

poj3635Full Tank?[分层图最短路]

时间:2016-09-26 23:05:08      阅读:284      评论:0      收藏:0      [点我收藏+]

标签:

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);
    }
}

 

 

poj3635Full Tank?[分层图最短路]

标签:

原文地址:http://www.cnblogs.com/candy99/p/5911049.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!