Full Tank?
Time Limit: 1000MS Memory Limit: 65536K
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
Nordic 2007
题意:给出一张图,n<=1000,m<=10000. 有一辆车想从图的一个地方到达另外一个地方,每个点是一个卖油的地方,每个地方买的有价格不一样,车的最大装油量是c,求初始点到终止点的最小花费。
网上大部分的思路都是类似于dij的那种扩展。
首先定义一个二维数组dp。 dp[i][j] 表示走到i点剩余j个单位的汽油时的最小花费
然后维护一个优先队列。 每次有两种可扩展的状态,一是加一个单位的油,二是走向邻接点,然后不断的将状态加入优先队列中
心情不来刷一发搜索
//长得像Dijkstra的BFS
//刚开始都没想到用dp并且思路很乱,不是一个单位一个单位加油的,一次加好多,
//类似贪心的一个思路,结果.............
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<queue>
using namespace std;
#define MAXN 1005
#define MAXM 100005
#define INF 100000000
struct Edge{ int to,w,next; }e[MAXM];
struct Node{
int v,cost,f;
bool operator < (const Node &a) const {
return a.cost < cost;
}
};
int head[MAXN],tot,n,m,cap;
int dp[MAXN][105],used[MAXN][105],p[MAXN];
int s,t,ask;
priority_queue<Node> q;
void prepare(){
for(int i=0;i<=n;++i)
for(int j=0;j<=100;++j) dp[i][j]=INF;
dp[s][0]=0;
memset(used,0,sizeof used );
while(!q.empty()) q.pop();
}
inline void Add_Edge(int u,int v,int w){
e[++tot].to=v,e[tot].w=w,e[tot].next=head[u],head[u]=tot;
}
int Dijkstra(){
Node pc,pk; pc.v=s; pc.cost=0; pc.f=0; q.push(pc);
while(!q.empty()){
pc=q.top();q.pop();
int u=pc.v,cost=pc.cost,f=pc.f;
used[u][f]=1;
if(u==t) return cost;
if(f+1<=cap&&!used[u][f+1]&&dp[u][f+1]>dp[u][f]+p[u]){
dp[u][f+1]=dp[u][f]+p[u];
pk.v=u; pk.f=f+1; pk.cost=dp[u][f+1];
q.push(pk);
}
for(int i=head[u];i!=-1;i=e[i].next){
int v=e[i].to,w=e[i].w;
if(f>=w&&!used[v][f-w]&&dp[v][f-w]>cost){
dp[v][f-w]=cost,pk.v=v,pk.f=f-w;
pk.cost=dp[v][f-w];q.push(pk);
}
}
}
return -1;
}
int main(){
scanf("%d%d",&n,&m);
memset(head,-1,sizeof head ); tot=0;
for(int i=0;i<n;i++) scanf("%d",&p[i]);//price
for(int u,v,w,i=1;i<=m;++i){
scanf("%d%d%d",&u,&v,&w);
Add_Edge(u,v,w),Add_Edge(v,u,w);
}
scanf("%d",&ask);
for(int u,v,i=1;i<=ask;++i){
scanf("%d%d%d",&cap,&s,&t);
prepare();
int ans=Dijkstra();
if(ans!=-1) printf("%d\n",ans);
else printf("impossible\n");
}
return 0;
}