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
思路:用优先队列,每次拿出cost最小的状态,该状态可以有2种操作,充1升油或者出发前往所达其他城市。
#include <iostream> #include <cstdio> #include <queue> #include <cstring> using namespace std; #define maxn 1080 #define inf 0x3f3f3f3f #define maxm 40800 #define min(a,b) a>b?b:a int first[maxn]; int vv[maxm],ww[maxm],nxt[maxm],cost[maxn]; int dp[maxn][108]; bool use[maxn][maxn]; int e; void addedge(int u,int v,int w) { vv[e] = v; nxt[e] = first[u]; ww[e] = w; first[u] = e++; vv[e] = u; nxt[e] = first[v]; ww[e] = w; first[v] = e++; } void init() { memset(first,-1,sizeof(first)); e = 0; } struct State { int u,oil,cos; State(){} State(int uu,int oo,int cc) { u = uu; oil = oo; cos = cc; } bool operator < (const State & a)const { return cos > a.cos; } }; priority_queue <State> q; int SPFA(int U,int V,int C) { while(!q.empty()) q.pop(); memset(dp,0x3f,sizeof(dp)); memset(use,0,sizeof(use)); q.push(State(U,0,0)); dp[U][0] = 0; while(!q.empty()) { State s = q.top(); q.pop(); int su = s.u,soil = s.oil,scos = dp[su][soil]; use[su][soil] = 1; if(su == V) return scos; if(soil < C && !use[su][soil+1] && dp[su][soil+1] > dp[su][soil] + cost[su]) { dp[su][soil+1] = dp[su][soil] + cost[su]; q.push(State(su,soil+1,dp[su][soil+1])); } for(int i = first[su];i != -1;i = nxt[i]) { int v = vv[i],w = ww[i]; if(soil >= w && !use[v][soil-w] && scos < dp[v][soil-w]) { dp[v][soil-w] = scos; q.push(State(v,soil-w,scos)); } } } return -1; } int main() { //freopen("in.txt","r",stdin); int n,m; while(scanf("%d%d",&n,&m)==2) { init(); for(int i = 0;i < n;i++) scanf("%d",&cost[i]); while(m--) { int u,v,w; scanf("%d%d%d",&u,&v,&w); addedge(u,v,w); addedge(v,u,w); } int qq; scanf("%d",&qq); while(qq--) { int u,v,c; scanf("%d%d%d",&c,&u,&v); int ans = SPFA(u,v,c); if(ans != -1) printf("%d\n",ans); else puts("impossible"); } } return 0; }