首页 > 代码库 > POJ 3635 Full Tank?

POJ 3635 Full Tank?

Full Tank?

Time Limit: 1000ms
Memory Limit: 65536KB
This problem will be judged on PKU. Original ID: 3635
64-bit integer IO format: %lld      Java class name: Main
 

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 510 10 20 12 130 1 90 2 81 2 11 3 112 3 7210 0 320 1 4

Sample Output

170impossible

Source

Nordic 2007
 
解题:dp+贪心+bfs。。。。。。。混合多种思想。dp[u][d]表示在城市u还剩油d时的最小花费。油就是距离。
 
  1 #include <iostream>  2 #include <cstdio>  3 #include <cstring>  4 #include <cmath>  5 #include <algorithm>  6 #include <climits>  7 #include <vector>  8 #include <queue>  9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 1010; 18 struct arc { 19     int to,w,next; 20     arc(int x = 0,int y = 0,int z = -1) { 21         to = x; 22         w = y; 23         next = z; 24     } 25 }; 26 struct node { 27     int u,d,cost; 28     node(int x = 0,int y = 0,int z = 0) { 29         u = x; 30         d = y; 31         cost = z; 32     } 33     bool operator<(const node &y) const { 34         return cost > y.cost; 35     } 36 }; 37 arc e[21000]; 38 int head[maxn],a[maxn],dp[maxn][102]; 39 int n,m,tot,beg,ed,c; 40 bool vis[maxn][102]; 41 priority_queue<node>q; 42 void add(int u,int v,int w) { 43     e[tot] = arc(v,w,head[u]); 44     head[u] = tot++; 45     e[tot] = arc(u,w,head[v]); 46     head[v] = tot++; 47 } 48 int bfs() { 49     while(!q.empty()) q.pop(); 50     for(int i = 0; i <= n; i++) 51         for(int j = 0; j <= c; j++) { 52             dp[i][j] = INF; 53             vis[i][j] = false; 54         } 55     dp[beg][0] = 0; 56     q.push(node(beg,0,0)); 57     while(!q.empty()){ 58         node now = q.top(); 59         q.pop(); 60         int u = now.u; 61         int d = now.d; 62         int cost = now.cost; 63         vis[u][d] = true; 64         if(u == ed) return cost; 65         if(d + 1 <= c && !vis[u][d+1] && dp[u][d+1] > dp[u][d] + a[u]){ 66             dp[u][d+1] = dp[u][d] + a[u]; 67             q.push(node(u,d+1,dp[u][d+1])); 68         } 69         for(int i = head[u]; ~i; i = e[i].next){ 70             int v = e[i].to; 71             int w = e[i].w; 72             if(d >= w && !vis[v][d-w] && dp[v][d-w] > cost){ 73                 dp[v][d-w] = cost; 74                 q.push(node(v,d-w,cost)); 75             } 76         } 77  78     } 79     return -1; 80 } 81 int main() { 82     int u,v,w,ask; 83     while(~scanf("%d %d",&n,&m)) { 84         for(int i = tot = 0; i < n; i++) 85             scanf("%d",a+i); 86         memset(head,-1,sizeof(head)); 87         for(int i = 0; i < m; i++) { 88             scanf("%d %d %d",&u,&v,&w); 89             add(u,v,w); 90         } 91         scanf("%d",&ask); 92         while(ask--) { 93             scanf("%d %d %d",&c,&beg,&ed); 94             int ans = bfs(); 95             if(ans == -1) puts("impossible"); 96             else printf("%d\n",ans); 97         } 98     } 99     return 0;100 }
View Code

 

POJ 3635 Full Tank?