#POJ 1724 ROADS (二维最短路)

本文介绍了一个有向图中,每条边有权值和花费,在花费不超过限定值的情况下,寻找从起点到终点的最短路径问题。通过使用多状态Dijkstra算法,文章详细解释了如何考虑路径长度和费用限制,最终找到满足条件的最短路径。

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

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

题目 大意 : 输入一个有向图, 每条边有权值和花费, 输出在满足花费小于等于 K的情况下从1到N的最短路

思路 : dis多开一个状态, 前一个状态表示点, 后一个状态表示花费, 跑最短路时加一个判断条件, 也就是加上下一步的花费之和要小于等于K, 最后从0遍历到K(因为你不知道你花费多少), 找到最小值即可

Accepted code

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
#include <cstdio>
using namespace std;

#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define pir pair <int, int>
#define MK(x, y) make_pair(x, y)
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))

typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 1e5 + 100;
const int MAXM = 110;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }

struct Edge
{
	int v, w, cp;
};
struct node
{
	int id, k, w;
	bool operator < (const node &oth) const {
		return w > oth.w;
	}
};
vector <Edge> G[MAXN];
int dis[MAXM][MAXN], n, m, k;
bool vis[MAXM][MAXN];

void dijkstra(int x) {
	priority_queue <node> q;
	MEM(dis, INF);
	dis[x][0] = 0;
	q.push({ x, 0, 0 });
	while (!q.empty()) {
		node now = q.top();
		q.pop();
		int ans = now.id, ki = now.k;
		if (vis[ans][ki])
			continue;
		vis[ans][ki] = true;
		for (int i = 0; i < SZ(G[ans]); i++) {
			int vi = G[ans][i].v, ci = G[ans][i].cp;
			int wi = G[ans][i].w;
			if (dis[vi][ki + ci] > dis[ans][ki] + wi && ki + ci <= k) {
				dis[vi][ki + ci] = dis[ans][ki] + wi;
				q.push({ vi, ki + ci, dis[vi][ki + ci] });
			}
		}
	}
}

int main()
{
	cin >> k >> n >> m;
	for (int i = 0; i < m; i++) {
		int ui, vi, wi, fi;
		sc("%d %d %d %d", &ui, &vi, &wi, &fi);
		G[ui].push_back({ vi, wi, fi }); 
	}
	int ans = INF;
	dijkstra(1);
	for (int i = 0; i <= k; i++)
		Min(ans, dis[n][i]);
	if (ans == INF)
		printf("-1\n");
	else
		printf("%d\n", ans);
	return 0;  // 改数组大小!!!
}

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值