uva 11367 最短路
F: Full Tank? |
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 1n1000 and 0m10000 , the number of cities and roads. Then follows a line with n integers 1pi100 , where pi is the fuel price in the i th city. Then follow m lines with three integers 0u , v < n and 1d100 , telling that there is a road between u and v with length d . Then comes a line with the number 1q100 , giving the number of queries, and q lines with three integers 1c100 , 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
建模:(参考:https://gist.github.com/andmej/1360763 && uva 论坛)
点为d[i][j],表示到第i个城市当前有j units油的状态;边表示花费,对d[i][j]按一下方式引出弧:
1.若k为i的直接后继,且j>=dis(i, k),则引弧d[i][j]到d[k][j-dis(i,k)],费用0;
2.若当前车还可加油,即j<c,则引弧d[i][j]到d[i][j+1]。
另外,这种弧是在dijkstra时建立的。
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<vector> #include<cstdlib> #include<algorithm> #include<queue> #include<map> #include<stack> using namespace std; #define LL long long #define UINT unsigned int #define MAX_INT 0x7fffffff #define cint const int #define INF 100000000 #define MAXN 1111 #define MAXM 22222 struct edge{ int u, v, w, nxt; }e[MAXM]; int h[MAXN], cc; int n, m, p[MAXN], c; void add(int u, int v, int w){ e[cc]=(edge){u, v, w, h[u]}; h[u]=cc++; e[cc]=(edge){v, u, w, h[v]}; h[v]=cc++; } struct node{ int u, cost, g; bool operator < (const node &rhs)const{ return cost > rhs.cost; } }; int d[1111][111]; int dijkstra(cint s, cint t){ for(int i=0; i<n; i++) fill_n(d[i], c+1, INF); priority_queue<node> q; d[s][0]=0; q.push((node){s, 0, 0}); while(!q.empty()){ node ut = q.top(); q.pop(); int u=ut.u; if(d[u][ut.g]<ut.cost) continue; if(u == t) return d[u][ut.g]; if(ut.g<c && d[u][ut.g+1]>ut.cost+p[u]){ d[u][ut.g+1] = ut.cost + p[u]; q.push((node){u, d[u][ut.g+1], ut.g+1}); } for(int i=h[u]; i!=-1; i=e[i].nxt){ int v = e[i].v , dis = e[i].w, new_g = ut.g-dis; if(new_g>-1 && d[v][new_g]>d[u][ut.g]){ d[v][new_g] = d[u][ut.g]; q.push((node){v, d[v][new_g], new_g}); } } } return INF; } int main(){ // freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin); while(scanf(" %d %d", &n, &m)==2){ int i, u, v, w; for(i=0; i<n; i++) scanf(" %d", p+i); fill_n(h, n, -1); cc=0; for(i=0; i<m; i++){ scanf(" %d %d %d", &u, &v, &w); add(u, v, w); } int q, s, e; scanf(" %d", &q); while(q--){ scanf(" %d %d %d", &c, &s, &e); int ans = dijkstra(s, e); if(ans == INF) printf("impossible\n"); else printf("%d\n", ans); } } return 0; }