2023年码蹄杯二赛 自驾游 题型:bfs

49 阅读1分钟

码题集OJ-自驾游 (matiji.net)

#include <bits/stdc++.h>

using namespace std;
typedef pair<int, int>PII;
#define x first
#define y second
const int N = 2010;
PII a[N];
int n, L, mx, s; //多组数据,全程长度,最大油量和,预算油钱

struct Car {
	int distance;  //能跑的最大路程
	int money;     //跑的路程花费的油钱
	int idx;       //汽车所在的当前位置
};

bool dfs() {
	queue<Car> q;
	q.push({0, s, -1});

	while (!q.empty()) {
		Car t = q.front();
		q.pop();

		if (t.distance + mx >= L)
			return true;  //L是最大路程

		for (int i = t.idx + 1; i <= n; i++) {
			//距离太远,无法到达
			if (a[i].x > t.distance + mx)
				break;  //如果前面的加油站是我当前位置加满油也到达不了,那就结束程序
			//油钱不够
			if (a[i].y > t.money)
				continue;

			q.push({a[i].x, t.money - a[i].y, i}); //当前到达的加油站的距离   到达当前加油站还剩的油量  当前加油站的下标
		}
	}

	return false;
}

int main() {

	while (cin >> n >> L >> mx >> s) {
		for (int i = 0; i < n; i++)cin >> a[i].x >> a[i].y;


		if (dfs())
			puts("Yes");
		else
			puts("No");
	}

	return  0;
}