POJ1724 - ROADS - 限制最短路

本文介绍了一道经典的图论问题——在限定花费内寻找从起点到终点的最短路径。文章提供了完整的解题思路及AC代码实现,采用优先队列优化BFS算法确保效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.题目描述:

ROADS
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 15020 Accepted: 5427

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

2.题意概述:

图中每条路都有 路长 和 “过路费” 两个参数,现在只有 K 块钱,要你求起点到终点的最短路,也就是说在 K 花费内的最短路。

3.解题思路:

深搜bfs+优先队列,保证堆顶都是价值最高,这样得到的第一个解就是最优解

4.AC代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <functional>
#include <cmath>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <ctime>
#define INF 0x3f3f3f3f
#define maxn 10010
#define N 111
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
using namespace std;
const int mod = 1e9 + 7;
typedef long long ll;
struct node
{
	int to, val, cost, next;
} E[maxn];
struct que
{
	int to, val, cost;
	que(int a, int b, int c)
	{
		to = a;
		val = b;
		cost = c;
	}
	friend bool operator< (que a, que b)
	{
		if (a.val == b.val)
			return a.cost > b.cost;
		return a.val > b.val;
	}
};
int cnt;
int head[N];
void init()
{
	memset(head, -1, sizeof(head));
	cnt = 0;
}
void addedge(int u, int v, int w, int c)
{
	E[cnt].to = v;
	E[cnt].val = w;
	E[cnt].cost = c;
	E[cnt].next = head[u];
	head[u] = cnt++;
}
int bfs(int sta, int n, int k)
{
	priority_queue<que> q;
	q.push(que(sta, 0, 0));
	while (!q.empty())
	{
		que pre = q.top();
		q.pop();
		if (pre.to == n)
			return pre.val;
		for (int i = head[pre.to]; i != -1; i = E[i].next)
		{
			if (pre.cost + E[i].cost <= k)
				q.push(que(E[i].to, pre.val + E[i].val, pre.cost + E[i].cost));
		}
	}
	return -1;
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	long _begin_time = clock();
#endif
	int k, n, r;
	while (~scanf("%d%d%d", &k, &n, &r))
	{
		init();
		while (r--)
		{
			int s, d, l, t;
			scanf("%d%d%d%d", &s, &d, &l, &t);
			addedge(s, d, l, t);
		}
		int ans = bfs(1, n, k);
		printf("%d\n", ans);
	}
#ifndef ONLINE_JUDGE
	long _end_time = clock();
	printf("time = %ld ms.", _end_time - _begin_time);
#endif
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值