POJ 3635 Full Tank? 【分层图/最短路dp】

时间:2023-03-08 15:39:56

任意门:http://poj.org/problem?id=3635

Full Tank?
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8388   Accepted: 2734

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 ≤ 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 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

题意概括:

有 N 个城市 M 条道路(双向通行),每个城市都有一个加油站(每单位汽油售价分别为 pi ),道路每单位距离花费一单位汽油。求从起点到终点得最小花费(如果不能到达输出-1,因为小车车油箱有容量限制)。

解题思路:

终于还是把这题做了,也忘了是哪一次得集训遇到这道题,是一道好题,可惜当时自己做不出。

这道题正确的打开方式:分层图或者叫最短路dp(个人感觉分层图其实就是搞最短路dp,动态规划的思想)。

状态:dp[ i ][ j ] 到了第 i 个城市 还剩下 j 单位的油的最小花费。

状态转移分两种:

1、在当前城市加油(精妙之处在于每次只加 1 单位的油,以最少的油跑最远的地方,如果未来发现这里的油不划算庆幸没有多加而且加到刚刚好,如果未来发现这里的油划算,那就跑回来多加一点咯,因为我们这里是用一个优先队列来保存状态);        

2、不加油,直接跑到下一个城市(当然要当前油箱的油可以行驶到下一个城市);

tip:

注意判重, 每种状态只入队一次。

AC code:

 //poj 3635 最短路dp
//未优化输入
/*
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <cstring>
#define INF 0x3f3f3f3f
#define LL long long int
using namespace std;
const int MAX_M = 1e4+10;
const int MAX_N = 1e3+10;
const int MAX_K = 105;
struct date
{
int v, nxt, val;
}edge[MAX_M<<1]; struct node
{
int u, k, w;
bool operator < (const node& a)const{
return w > a.w;
}
node(int a=0, int b=0, int c=0):u(a),k(b),w(c){}
};
int N, M, tank, st, ed;
int coco[MAX_N];
int head[MAX_N], cnt;
int dis[MAX_N][MAX_K];
bool vis[MAX_N][MAX_K];
//priority_queue<node> Q; void init()
{
memset(head, -1, sizeof(head));
cnt = 1;
} void add(int from, int to, int weight)
{
edge[cnt].nxt = head[from];
edge[cnt].v = to;
edge[cnt].val = weight;
head[from] = cnt++;
} void Dijkstra(int s)
{
memset(dis, INF, sizeof(dis));
memset(vis, false, sizeof(vis));
priority_queue<node> Q;
node tp;
tp.u = s, tp.k = 0, dis[s][0] = tp.w = 0;
Q.push(tp);
while(!Q.empty()){
tp = Q.top();Q.pop();
vis[tp.u][tp.k] = true;
if(tp.u == ed){
printf("%d\n", tp.w);return;
} if(tp.k+1<=tank && !vis[tp.u][tp.k+1] && dis[tp.u][tp.k]+coco[tp.u] < dis[tp.u][tp.k+1]){
dis[tp.u][tp.k+1] = dis[tp.u][tp.k]+coco[tp.u];
Q.push(node(tp.u, tp.k+1, dis[tp.u][tp.k+1]));
} for(int i = head[tp.u]; i != -1; i = edge[i].nxt){
int v = edge[i].v, cost = edge[i].val;
if(tp.k >= cost && !vis[v][tp.k-cost] && dis[v][tp.k-cost] > tp.w){
dis[v][tp.k-cost] = tp.w;
Q.push(node(v, tp.k-cost, tp.w));
}
}
}
printf("impossible\n");
} int main()
{
int u, v, w;
scanf("%d%d", &N, &M);
init();
for(int i = 0; i < N; i++){
scanf("%d", &coco[i]);
}
for(int i = 1; i <= M; i++){
scanf("%d%d%d", &u, &v, &w);
add(u, v, w);
add(v, u, w);
}
int T;
scanf("%d", &T);
while(T--){
scanf("%d%d%d", &tank, &st, &ed);
Dijkstra(st);
}
return 0;
}
*/ //优化输入
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <cstring>
#define INF 0x3f3f3f3f
#define LL long long int
using namespace std;
const int MAXN = ;
const int MAXM = 1e4+;
const int MAXK = ;
int N, M, st, ed, tank;
int p[MAXN]; ///每一站的油
int cost[MAXN][MAXK]; ///cost[i][j] 到达第i站还剩下j油的最小花费
int head[MAXN], cnt;
bool vis[MAXN][MAXK]; int read()
{
int f=, x=;char ch = getchar();
while(ch < '' || ch > '') {if(ch=='-')f=-;ch=getchar();}
while(ch >= '' && ch <= '') {x = x*+(ch-'');ch=getchar();}
x=x*f;return x;
}
struct node
{
int v, k, val;
bool operator < (const node& a)const{
return val > a.val;
}
node(int a=, int b=, int c=):v(a),k(b),val(c){};
}; struct date
{
int v, nxt, w;
}edge[MAXM<<]; void init()
{
memset(head, -, sizeof(head));
cnt = ;
}
void add(int from, int to, int weight)
{
edge[cnt].nxt = head[from];
edge[cnt].v = to;
edge[cnt].w = weight;
head[from] = cnt++;
} void Dijkstra(int S)
{
memset(cost, INF, sizeof(cost));
memset(vis, false, sizeof(vis));
node tp;
priority_queue<node> Q;
tp.v = S; tp.k = ; tp.val = ;
Q.push(tp);
cost[S][] = ;
while(!Q.empty()){
tp = Q.top(); Q.pop();
int fr = tp.v, oil = tp.k;
vis[fr][oil] = true;
if(fr == ed) {printf("%d\n", tp.val);return;} //因为是优先队列优化的所以到达终点值得第一个值就是最小值 if(oil+ <= tank && !vis[fr][oil+] && cost[fr][oil+] > cost[fr][oil]+p[fr]){ ///在当前站一滴一滴地加,最好的情况是加的少跑的远
cost[fr][oil+] = cost[fr][oil]+p[fr];
Q.push(node(fr, oil+, cost[fr][oil+]));
}
for(int i = head[fr]; i != -; i = edge[i].nxt){ ///跑到当前的油可以跑到的地方
int v = edge[i].v;
if(oil >= edge[i].w && !vis[v][oil-edge[i].w] && tp.val < cost[v][oil-edge[i].w]){
cost[v][oil-edge[i].w] = tp.val;
Q.push(node(v, oil-edge[i].w, tp.val));
}
}
}
printf("impossible\n");
} int main()
{
int u, v, w;
//scanf("%d%d", &N, &M);
init();
N = read(); M = read();
//printf("%d %d\n", N, M);
for(int i = ; i < N; i++) p[i] = read();
for(int i = ; i <= M; i++){
//scanf("%d%d%d", &u, &v, &w);
u = read(), v = read(), w = read();
add(u, v, w); ///无向图
add(v, u, w);
}
int T_case;
T_case = read();
while(T_case--){
tank = read(); st = read(); ed = read();
Dijkstra(st);
}
return ;
}