Poj 3635 full tank?

Source: Internet
Author: User
Full tank? Time Limit: 1000 msmemory limit: 65536 kbthis 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 realized that the gas prices varied between the cities you visited. maybe you cocould 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 lineNIntegers 1 ≤Pi≤ 100, wherePiIs the fuel price inITh city. Then followMLines with three integers 0 ≤U,V<NAnd 1 ≤D≤ 100, telling that there is a roadUAndVWith LengthD. Then comes a line with the number 1 ≤Q≤ 100, giving the number of queries, andQLines with three integers 1 ≤C≤ 100,SAndE, WhereCIs the fuel capacity of the vehicle,SIs the starting city, andEIs the goal.

Output

For each query, output the price of the cheapest trip fromSToEUsing a car with the given capacity, or "impossible" if there is no way of getting fromSToEWith 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
Sourcenordic 2007: DP + greedy + BFS ....... A mixture of ideas. DP [u] [d] indicates the minimum cost when the City U has oil d left. Oil is the distance.
  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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.