poj3635 Full Tank

Full Tank?
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7046   Accepted: 2286

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

题目描述

给一张n个点(n<=1000)m条边(m<=10000)的图,有一辆车从一个地方到达另外一个地方,每个点是一个卖油的地方,每个地方的价格不一样,车的最大装油量是c(c<=100),求初始点到终止点的最小花费。

思路

对一个点拆成c个点,cost[i][j]表示到达第i个点还剩j升油的最小花费。维护一个优先队列,每次扩展两种状态,在当前点加一升油或者移动到相邻点。

剪枝:

代码实现

(有错误,请勿抄代码)

#include
#include
#include
#include
using namespace std;
const int MAXn=1005,MAXm=10005;
int n,m,q;
int p[MAXn];//d[MAXn][MAXn];
int cost[MAXn][105];
int vis[MAXn][105];//是否拜访第i个地点有剩油x的情况 
struct node{
	int cap,v,cost,next;
	bool operator<(const node &temp) const  //优先队列 
    {  
        return cost>temp.cost;  
    }  
}city[2*MAXm];
int head[MAXn];
int t=1;
int c,s,e;
priority_queueque;
void add(int u,int v,int w)
{
	city[t].v=v;
	city[t].cost=w;
	city[t].next=head[u];
	head[u]=t++;
}
int bfs()
{
	while(!que.empty()) que.pop();//出队 
	node temp1,temp2;//俩指针:1是出口,2为入口 
	temp1.cap=0;
    temp1.v=s;//起点 
    temp1.cost=0;
	que.push(temp1);//入队 
	while(!que.empty())
	{
		temp1=que.top();
        que.pop();
		if(temp1.v==e) return temp1.cost; //找到 
		if(vis[temp1.v][temp1.cap]) continue;// 如果已经访问过该情况,进行下一个while 
		vis[temp1.v][temp1.cap]=1;
		temp2.v=temp1.v;temp2.cap=temp1.cap+1;temp2.cost=temp1.cost+p[temp1.v];
		if(!vis[temp2.v][temp2.cap]&&temp2.cap<=c&&temp2.cost0;i=city[i].next)
		{
			if(temp1.cap



你可能感兴趣的:(搜索)