[题解] poj 1724 ROADS (dijkstra最短路+A*搜索)

- 传送门 -

 http://poj.org/problem?id=1724

#ROADS

| Time Limit: 1000MS |   | Memory Limit: 65536K |
| Total Submissions: 16053 |   | Accepted: 5758 |

Description

N cities named with numbers 1 ... N are connected with one-way roads. Each road has two parameters associated with it : the road length and the toll that needs to be paid for the road (expressed in the number of coins). 
Bob and Alice used to live in the city 1. After noticing that Alice was cheating in the card game they liked to play, Bob broke up with her and decided to move away - to the city N. He wants to get there as quickly as possible, but he is short on cash.

We want to help Bob to find the shortest path from the city 1 to the city N that he can afford with the amount of money he has.

Input

The first line of the input contains the integer K, 0 <= K <= 10000, maximum number of coins that Bob can spend on his way. 
The second line contains the integer N, 2 <= N <= 100, the total number of cities.

The third line contains the integer R, 1 <= R <= 10000, the total number of roads.

Each of the following R lines describes one road by specifying integers S, D, L and T separated by single blank characters :

  • S is the source city, 1 <= S <= N
  • D is the destination city, 1 <= D <= N
  • L is the road length, 1 <= L <= 100
  • T is the toll (expressed in the number of coins), 0 <= T <=100

Notice that different roads may have the same source and destination cities.

Output

The first and the only line of the output should contain the total length of the shortest path from the city 1 to the city N whose total toll is less than or equal K coins. 
If such path does not exist, only number -1 should be written to the output.

Sample Input

5
6
7
1 2 2 3
2 4 3 3
3 4 2 4
1 3 4 1
4 6 2 1
3 5 2 0
5 4 3 2

Sample Output

11

Source

CEOI 1998

[[Submit](http://poj.org/submit?problem_id=1724)]   [[Status](http://poj.org/problemstatus?problem_id=1724)]   [[Discuss](http://poj.org/bbs?problem_id=1724)]

- 题意 -

 图中有 n 个点, m 条有向边, 每条边有一个长度和花费, 求在花费不超过 k (已给出)的情况下的最短路径.
 

- 思路 -

 第一反应是像poj2449一样再来一发\(A^*\).
 
 然后就这样写了.
 
 (看了下别人的, 发现直接给dijkstra的入队元素设一个花费限制就好了, 花费超限则无法入队, 然后在把只能入队一次的条件去掉...面目全非...感觉像把dijkstra和\(A*\)结合了)
 
 细节见代码.
 

- 代码 -

#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;

const int N = 105;
const int M = 1e4 + 5;

int TO[M], FRM[M], V[M], C[M];
int NXT[M], NXT1[M], HD[N], HD1[N];
int DIS[N], VIS[N];
int mey, n, m, sz;

struct ASTAR {
	int v, pos, c;
	bool operator < (const ASTAR &tmp) const {
		return pos + DIS[v] > tmp.pos + DIS[tmp.v];
	}
};

priority_queue<ASTAR>Q;

void add(int x, int y, int v, int  c) {
	TO[++sz] = y; FRM[sz] = x; V[sz] = v; C[sz] = c;
	NXT[sz] = HD[x]; HD[x] = sz;
	NXT1[sz] = HD1[y]; HD1[y] = sz;
}

void dijkstra(int en) {
	memset(DIS, 0x3f, sizeof (DIS));
	DIS[en] = 0;
	ASTAR st;
	st.v = en; st.pos = 0;
	DIS[en] = 0;
	Q.push(st);
	while (!Q.empty()) {
		ASTAR x = Q.top();
		Q.pop();
		int u = x.v;
		if (VIS[u]) continue;
		VIS[u] = 1;
		for (int i = HD1[u]; i; i = NXT1[i]) {
			int v = FRM[i];
			if (!VIS[v] && DIS[v] > DIS[u] + V[i]) {
				DIS[v] = DIS[u] + V[i];
				ASTAR y;
				y.v = v; y.pos = 0;
				Q.push(y);
			}
		}
	}
}

int astar(int st, int en) {
	if (DIS[st] == 0x3f3f3f3f)
		return -1;
	while (!Q.empty())
		Q.pop();
	ASTAR tmp;
	tmp.v = st; tmp.pos = 0; tmp.c = 0;
	Q.push(tmp);
	while (!Q.empty()) {
		ASTAR x = Q.top();
		Q.pop();
		int u = x.v;
		if (u == en && x.c <= mey)
			return x.pos;
		for (int i = HD[u]; i; i = NXT[i]) {
			ASTAR y;
			y.v = TO[i]; y.pos = x.pos + V[i]; y.c = x.c + C[i];
			if (y.c <= mey) Q.push(y); //重要的剪枝, 不加会MLE
		}
	}
	return -1;
}

int main() {
	scanf("%d%d%d", &mey, &n, &m);
	for (int i = 1; i <= m; ++i) {
		int x, y, v, c;
		scanf("%d%d%d%d", &x, &y, &v, &c);
		add(x, y, v, c);
	}
	dijkstra(n);
	printf("%d\n", astar(1, n));
	return 0;
}
posted @ 2017-08-18 21:49  lstttt  阅读(163)  评论(0编辑  收藏  举报