1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| #include <bits/stdc++.h> #define fst first #define snd second #define re register using namespace std; typedef pair<int,int> pii; const int N = 1e4 + 10,M = 2e4 + 10,K = 110,inf = 0x3f3f3f3f; int n,m,k; int d[N][K]; int idx,h[N],ne[M],e[M],w[M]; bool vis[N][K]; inline int read(){ int r = 0,w = 1; char c = getchar(); while (c < '0' || c > '9'){ if (c == '-') w = -1; c = getchar(); } while (c >= '0' && c <= '9'){ r = (r << 3) + (r << 1) + (c ^ 48); c = getchar(); } return r * w; } inline void add(int a,int b,int c){ ne[idx] = h[a]; e[idx] = b; w[idx] = c; h[a] = idx++; } inline int up(int a,int b){ if (a % b == 0) return a / b; return a / b + 1; } inline int get(int x,int y){ if (x >= y) return x; return up(y - x,k) * k + x; } inline void dijkstra(int s){ priority_queue<pii,vector<pii>,greater<pii>> q; d[s][0] = 0; q.push({0,s}); while (!q.empty()){ pii t = q.top(); q.pop(); int dist = t.fst % k; if (vis[t.snd][dist]) continue; vis[t.snd][dist] = true; for (re int i = h[t.snd];~i;i = ne[i]){ int j = e[i],lim = w[i]; int ndist = (dist + 1) % k,ntim = get(t.fst,lim) + 1; if (d[j][ndist] > ntim){ d[j][ndist] = ntim; q.push({d[j][ndist],j}); } } } } int main(){ memset(h,-1,sizeof(h)); memset(d,inf,sizeof(d)); n = read(); m = read(); k = read(); for (re int i = 1;i <= m;i++){ int a,b,c; a = read(); b = read(); c = read(); add(a,b,c); } dijkstra(1); if (d[n][0] >= inf) puts("-1"); else printf("%d",d[n][0]); return 0; }
|