2025-06-22 22:18:08 +08:00

53 lines
1.2 KiB
C++

#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
int dp[10005][105];
bool visited[10005][105];
struct node {
node(int i, int i1, int i2) {
this->u = i;
this->idx = i1;
this->d = i2;
}
int u, idx, d;
bool operator<(const node &other) const {
return d > other.d;
}
};
vector<pair<int, int>> graph[10005];
int main() {
int n, m, k;
cin >> n >> m >> k;
for (int i = 0; i < m; i++) {
int u, v, a;
cin >> u >> v >> a;
graph[u].emplace_back(v, a);
}
memset(dp, 0x3f, sizeof dp);
priority_queue<node> tasks;
tasks.emplace(1, 0, dp[1][0] = 0);
while (!tasks.empty()) {
int u = tasks.top().u;
int idx = tasks.top().idx;
tasks.pop();
if (visited[u][idx]) continue;
else visited[u][idx] = true;
for (auto [v, w]: graph[u]) {
int time = dp[u][idx];
int j = (idx + 1) % k;
if (time < w) time += (w - time + k - 1) / k * k;
if (dp[v][j] > time + 1) tasks.emplace(v, j, dp[v][j] = time + 1);
}
}
if (dp[n][0] == 0x3f3f3f3f) dp[n][0] = 1;
cout << dp[n][0] << endl;
}