(beginer)最短路 UVA 11367 Full Tank?

  F: Full Tank? 

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?

\epsfbox{p11367.eps}

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$ \le$n$ \le$1000 and 0$ \le$m$ \le$10000 , the number of cities and roads. Then follows a line with n integers 1$ \le$pi$ \le$100 , where pi is the fuel price in the i th city. Then follow m lines with three integers 0$ \le$u , v < n and 1$ \le$d$ \le$100 , telling that there is a road between u and v with length d . Then comes a line with the number 1$ \le$q$ \le$100 , giving the number of queries, and q lines with three integers 1$ \le$c$ \le$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


题意:有很多个城市能加油,但是他们的价钱不一样,然后你在一个城市可以加油或者不加油,加多少也是随意。然后问到达目的地最少需要多少钱。

思路:dijkstra+优先队列可以过,但是在搜的过程中,我们不必枚举加油量,每一次我们只需要加一个单位就可以了。如果只加一个单位就能到达目的地当然是最小的啦,感觉还是有点模糊。。。在优先队列里面,现出来的是所有距离最小的,如果这个出来了,加1个单位的油后更新出来的值在队列里面还是最小的话,我们显然会用这个更小的,因为你加了油比其他还加油的消费还要小,这个不是更好吗,如果不是最小的,那么我们再把别的最小拿出来更新,看哪个更新出来的更小。所以其实每一次+1就可以了。大概是这样了,你叫我证明我是不行了。。。。

代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string.h>
#include<queue>
using namespace std;
const int maxn = 1000+5;
const int inf = 1e9;
int n , m , Q , ptr;
bool done[maxn][110];
int d[maxn][110] , price[maxn];

struct State 
{
	State(int uu,int cc,int dt) : dist(dt) , u(uu) , c(cc) { }
	int u;
	int c;
	int dist;
};

inline bool operator < (const State & st1 ,const State & st2)
{
	return st1.dist > st2.dist;
}

struct Edge
{
	int v;
	int w;
	Edge * next;
}edge[20000+5] , *first[maxn];

inline bool max(int a,int b)
{
	return a < b ? b : a;
}

void init()
{
	memset(first,0,sizeof(first));
	ptr = 0;
}

void add(int x,int y,int w)
{
	edge[++ptr].v = y;
	edge[ptr].w = w;
	edge[ptr].next = first[x];
	first[x] = &edge[ptr];
}

void input()
{
	int u , v , w;
	while (m--) {
		scanf("%d%d%d",&u,&v,&w);
		add(u,v,w);
		add(v,u,w);
	}
}

void Dij(int s,int e,int C)
{
	memset(done,false,sizeof(done));
	memset(d,0x3f,sizeof(d));
	priority_queue<State> q;
	q.push(State(s,0,0));
	d[s][0] = 0;
	int u , v , w , c;
	Edge * p;
	while (q.size())
	{
		State tmp = q.top(); q.pop();
		u = tmp.u;
		c = tmp.c;
		if (done[u][c]) continue;
		if (u==e) { printf("%d\n",tmp.dist); return; }
		done[u][c] = true;
		for (p = first[u] ; p ; p = p->next) {
			v = p->v;
			w = p->w;
			if (c-w >= 0 && d[v][c-w] > d[u][c]) 
			{
				d[v][c-w] = d[u][c];
				q.push(State(v,c-w,d[v][c-w]));
			}
		}
		if (c < C && d[u][c+1] > d[u][c]+price[u])
		{
			d[u][c+1] = d[u][c] + price[u];
			q.push(State(u,c+1,d[u][c+1]));
		}
	}
	printf("impossible\n");
}

void solve()
{
	scanf("%d",&Q);
	int s , e , c;
	while (Q--)
	{
		scanf("%d%d%d",&c,&s,&e);
		Dij(s,e,c);
	}
}

int main()
{
	while (scanf("%d%d",&n,&m)==2)
	{
		init();
		for (int i = 0 ; i < n ; ++i) scanf("%d",price+i);
		input();
		solve();
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值