ROADS

该博客介绍了一个寻找在预算约束下从城市1到城市N的最短路径问题。输入包含城市的数量、道路的数量以及每条道路的长度和通行费。输出是最短路径的总长度,如果不存在这样的路径则输出-1。提供的解决方案采用了深度优先搜索(DFS)并剪枝策略,通过邻接表存储图,并使用动态规划记录到达每个节点的最小花费路径。

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

来源:http://bailian.openjudge.cn/practice/1724/

一、题目

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.

二、输入

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.

三、输出

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.

四、样例

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

output:

11

五、思路 + 代码

dfs + 剪枝

代码如下:

#include<iostream>
#include<vector>
#define INF 0x7ffffff
using namespace std;
int Min(int a, int b) {
	return a < b ? a : b;
}
typedef struct Road {
	int d, l, t;//n 编号 l 长度 t 价格
}Road;
int K, N, R;
int curCost, minRoad, curRoad;
vector<vector<Road>> G(110);//邻接表
int visit[110];
int minL[110][11000];//min i j 表示 到达i城市花了j个硬币的最短路径
//对第i个城市进行dfs
void dfs(int i) {
	if (i == N) {
		minRoad = Min(curRoad, minRoad);
		return;
	}
	for (unsigned int j = 0; j < G[i].size(); j++) {
		Road r = G[i][j];
		if (!visit[r.d]) {
			if (curCost + r.t > K) continue;
			if (curRoad + r.l >= minRoad) continue;
			if (curRoad + r.l >= minL[r.d][curCost + r.t]) continue;
			visit[r.d] = 1;
			curCost += r.t;
			curRoad += r.l;
			minL[r.d][curCost + r.t] = curRoad + r.l;
			dfs(r.d);
			visit[r.d] = 0;
			curCost -= r.t;
			curRoad -= r.l;
		}
	}
}
int main() {
	cin >> K >> N >> R;
	minRoad = INF;
	for (int i = 0; i < 110; i++) {
		for (int j = 0; j < 11000; j++) {
			minL[i][j] = INF;
		}
	}
	for (int i = 0; i < R; i++) {
		int s, d, l, t;
		cin >> s >> d >> l >> t;
		if (s != d) {
			Road r;
			r.l = l;
			r.d = d;
			r.t = t;
			G[s].push_back(r);
		}
	}
	dfs(1);
	if (minRoad == INF) cout << "-1" << endl;
	else cout << minRoad << endl;
	return 0;
}
/*
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
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值