POJ3635 Full Tank?
Posted 嘒彼小星
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ3635 Full Tank?相关的知识,希望对你有一定的参考价值。
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7543 | Accepted: 2444 |
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
Source
1 #include <iostream> 2 #include <cstdlib> 3 #include <cstring> 4 #include <cstdio> 5 #include <algorithm> 6 #include <queue> 7 8 inline void read(int &x) 9 { 10 x = 0;char ch = getchar(), c = ch; 11 while(ch < ‘0‘ || ch > ‘9‘)c = ch, ch = getchar(); 12 while(ch <= ‘9‘ && ch >= ‘0‘)x = x * 10 + ch - ‘0‘, ch = getchar(); 13 if(c == ‘-‘)x = -x; 14 } 15 16 const int MAXN = 2000 + 10; 17 const int MAXM = 20000 + 10; 18 const int MAXC = 200 + 10; 19 20 struct Edge 21 { 22 int u,v,w,next; 23 Edge(int _u, int _v, int _w, int _next){u = _u;v = _v; w = _w; next = _next;} 24 Edge(){} 25 }edge[MAXM << 2]; 26 int head[MAXN], cnt; 27 28 inline void insert(int a, int b, int c) 29 { 30 edge[++cnt] = Edge(a,b,c,head[a]); 31 head[a] = cnt; 32 } 33 34 int n,m,value[MAXN],qq,b[MAXN][MAXC],s,e,c; 35 36 struct Node 37 { 38 int now, v, ans; 39 Node(){} 40 Node(int _now, int _v, int _ans){now = _now;v = _v; ans = _ans;} 41 }; 42 43 struct cmp 44 { 45 bool operator()(Node a, Node b) 46 { 47 return a.ans > b.ans; 48 } 49 }; 50 51 std::priority_queue<Node, std::vector<Node>, cmp> q; 52 53 void bfs(int s, int e) 54 { 55 memset(b, 0x3f, sizeof(b)); 56 while(q.size())q.pop(); 57 q.push(Node(s, 0, 0)); 58 b[0][0] = 1; 59 if(s == e) 60 { 61 printf("0\n"); 62 return; 63 } 64 register Node tmp; 65 while(q.size()) 66 { 67 tmp = q.top();q.pop(); 68 if(tmp.v + 1 <= c && b[tmp.now][tmp.v + 1] > tmp.ans + value[tmp.now])q.push(Node(tmp.now, tmp.v + 1, tmp.ans + value[tmp.now])), b[tmp.now][tmp.v + 1] = tmp.ans + value[tmp.now]; 69 for(register int pos = head[tmp.now];pos;pos = edge[pos].next) 70 { 71 if(tmp.v < edge[pos].w || b[edge[pos].v][tmp.v - edge[pos].w] <= tmp.ans)continue; 72 if(edge[pos].v == e) 73 { 74 printf("%d\n", tmp.ans); 75 return; 76 } 77 q.push(Node(edge[pos].v, tmp.v - edge[pos].w, tmp.ans)); 78 b[edge[pos].v][tmp.v - edge[pos].w] = tmp.ans; 79 } 80 } 81 printf("impossible\n"); 82 } 83 84 int main() 85 { 86 read(n);read(m); 87 register int tmp1, tmp2, tmp3; 88 for(register int i = 1;i <= n;++ i)read(value[i]); 89 for(register int i = 1;i <= m;++ i) 90 { 91 read(tmp1), read(tmp2), read(tmp3); 92 insert(tmp1 + 1, tmp2 + 1, tmp3); 93 insert(tmp2 + 1, tmp1 + 1, tmp3); 94 } 95 read(qq); 96 for(;qq;-- qq) 97 { 98 read(c);read(s);read(e); 99 ++ s;++ e; 100 bfs(s, e); 101 } 102 return 0; 103 }
以上是关于POJ3635 Full Tank?的主要内容,如果未能解决你的问题,请参考以下文章