题解(简洁版)
2026-09-17 21:57:11
发布于:湖南
10阅读
0回复
0点赞
思路:
由于列车发车时间连续(第到分钟每分钟一班),且换乘只需等待到到达时刻之后,因此从某站出发时间能到达的最早时间具有单调性。使用算法,以“最早到达时间”为距离,每次取出当前最早到达的站点进行松弛。若能在某时刻到达终点,则答案为。
代码:
#include <iostream>
#include <vector>
#include <queue>
#include <tuple>
#include <cstring>
using namespace std;
const int INF = 0x3f3f3f3f;
struct Edge {
int to, l, t;
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, q;
cin >> n >> m >> q;
vector<vector<Edge>> adj(n + 1);
for (int i = 0; i < m; i++) {
int u, v, l, t;
cin >> u >> v >> l >> t;
adj[u].push_back({v, l, t});
}
while (q--) {
int x, y, s;
cin >> x >> y >> s;
vector<int> dist(n + 1, INF);
dist[x] = s;
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;
pq.push({s, x});
while (!pq.empty()) {
auto [d, u] = pq.top();
pq.pop();
if (d > dist[u]) continue;
if (u == y) break;
for (auto &e : adj[u]) {
if (d > e.l) continue;
int nd = d + e.t;
if (nd < dist[e.to]) {
dist[e.to] = nd;
pq.push({nd, e.to});
}
}
}
cout << (dist[y] != INF ? "Yes" : "No") << "\n";
}
return 0;
}
这里空空如也





有帮助,赞一个