#POJ 1724 ROADS (二维最短路)

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 
#include 
#include 
#include 
#include 
#include 
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 
#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  G[MAXN];
int dis[MAXM][MAXN], n, m, k;
bool vis[MAXM][MAXN];

void dijkstra(int x) {
	priority_queue  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;  // 改数组大小!!!
}

 

你可能感兴趣的:(最短路)