POJ 3635 Full Tank?
2014-09-30 20:19
274 查看
Full Tank?
Time Limit: 1000msMemory 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 ≤ 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
Nordic 2007解题:dp+贪心+bfs。。。。。。。混合多种思想。dp[u][d]表示在城市u还剩油d时的最小花费。油就是距离。
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <vector> #include <queue> #include <cstdlib> #include <string> #include <set> #include <stack> #define LL long long #define pii pair<int,int> #define INF 0x3f3f3f3f using namespace std; const int maxn = 1010; struct arc { int to,w,next; arc(int x = 0,int y = 0,int z = -1) { to = x; w = y; next = z; } }; struct node { int u,d,cost; node(int x = 0,int y = 0,int z = 0) { u = x; d = y; cost = z; } bool operator<(const node &y) const { return cost > y.cost; } }; arc e[21000]; int head[maxn],a[maxn],dp[maxn][102]; int n,m,tot,beg,ed,c; bool vis[maxn][102]; priority_queue<node>q; void add(int u,int v,int w) { e[tot] = arc(v,w,head[u]); head[u] = tot++; e[tot] = arc(u,w,head[v]); head[v] = tot++; } int bfs() { while(!q.empty()) q.pop(); for(int i = 0; i <= n; i++) for(int j = 0; j <= c; j++) { dp[i][j] = INF; vis[i][j] = false; } dp[beg][0] = 0; q.push(node(beg,0,0)); while(!q.empty()){ node now = q.top(); q.pop(); int u = now.u; int d = now.d; int cost = now.cost; vis[u][d] = true; if(u == ed) return cost; if(d + 1 <= c && !vis[u][d+1] && dp[u][d+1] > dp[u][d] + a[u]){ dp[u][d+1] = dp[u][d] + a[u]; q.push(node(u,d+1,dp[u][d+1])); } for(int i = head[u]; ~i; i = e[i].next){ int v = e[i].to; int w = e[i].w; if(d >= w && !vis[v][d-w] && dp[v][d-w] > cost){ dp[v][d-w] = cost; q.push(node(v,d-w,cost)); } } } return -1; } int main() { int u,v,w,ask; while(~scanf("%d %d",&n,&m)) { for(int i = tot = 0; i < n; i++) scanf("%d",a+i); memset(head,-1,sizeof(head)); for(int i = 0; i < m; i++) { scanf("%d %d %d",&u,&v,&w); add(u,v,w); } scanf("%d",&ask); while(ask--) { scanf("%d %d %d",&c,&beg,&ed); int ans = bfs(); if(ans == -1) puts("impossible"); else printf("%d\n",ans); } } return 0; }
View Code
相关文章推荐
- POJ 3635 Full Tank(最短路径变形 + 优先队列)
- poj 3635 Full Tank 最短路
- poj 3635 Full Tank? ( 图上dp )
- Full Tank? POJ - 3635 二维最短路(dp?)
- poj3635—Full Tank?(spfa+dp)
- poj 3635 Full Tank? ( 图上dp )
- [POJ 3635] Full Tank?
- POJ 3635 Full Tank?
- POJ 3635 Full Tank?(BFS)
- POJ 3635 Full Tank?
- POJ 3635 Full Tank? (dp)
- POJ-3635 Full Tank? 变形最短路
- poj 3635 Full Tank? (优先队列 + bfs)
- POJ-3635-Full Tank?-spfa+ 优先队列
- poj 3635(full tank?)
- poj 3635 Full Tank? 广搜?
- POJ 3635:Full Tank?
- poj 3635/hdu 1676 Full Tank? 车辆加油+最短路
- POJ 3635 Full Tank?
- POJ 3635 Full Tank? 最短路DP