首页 > 代码库 > poj3635Full Tank?[分层图最短路]
poj3635Full 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 ≤ 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 510 10 20 12 130 1 90 2 81 2 11 3 112 3 7210 0 320 1 4
Sample Output
170impossible
Source
题意:有了油量限制,每个点加油价格不一样,对于每个询问求最少花费
一看到d[i][j]这样的状态表示,马上想到分层图最短路,有点类似DP的思想
#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?[分层图最短路]