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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
| #include <iostream> #include <cstdio> #include <cstring> #include <cctype> #include <algorithm> #include <queue>
#define For(a,x,y) for (int a = x; a <= y; ++a) #define Forw(a,x,y) for (int a = x; a < y; ++a) #define Bak(a,y,x) for (int a = y; a >= x; --a)
namespace FastIO { inline int getint() { int s = 0, x = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') x = -1; ch = getchar(); } while (isdigit(ch)) { s = s * 10 + ch - '0'; ch = getchar(); } return s * x; } inline void __basic_putint(int x) { if (x < 0) { x = -x; putchar('-'); } if (x >= 10) __basic_putint(x / 10); putchar(x % 10 + '0'); } inline void putint(int x, char external) { __basic_putint(x); putchar(external); } }
namespace Solution { const int MAXN = 100000 + 10; const int MAXM = 500000 + 10; const int MAXK = 10 + 5; struct Node { int id, weight, now; Node() { id = weight = now = 0; } bool operator < (const Node &that) const { return weight > that.weight; } } head[MAXN]; struct Edge { int now, next, weight; } edge[MAXM]; int n, m, k, s, t, K, cnt, dis[MAXN][MAXK]; bool vis[MAXN][MAXK]; inline void addEdge(int prev, int next, int weight) { edge[++cnt].now = next; edge[cnt].weight = weight; edge[cnt].next = head[prev].id; head[prev].id = cnt; } Node NewNode(int id, int weight, int now) { Node tmp; tmp.id = id; tmp.weight = weight; tmp.now = now; return tmp; } void SPFA() { memset(dis, 0x7f, sizeof(dis)); std::priority_queue<Node> q; For (i, 0, K) dis[s][i] = 0; q.push(NewNode(s, 0, 0)); while (!q.empty()) { Node NowNode = q.top(); q.pop(); int Floor = NowNode.now; int now = NowNode.id; if (vis[now][Floor]) continue; vis[now][Floor] = true; for (int e = head[now].id; e; e = edge[e].next) { int to = edge[e].now; if (!vis[to][Floor] && dis[to][Floor] > dis[now][Floor] + edge[e].weight) { dis[to][Floor] = dis[now][Floor] + edge[e].weight; q.push(NewNode(to, dis[to][Floor], Floor)); } if (!vis[to][Floor] && Floor + 1 <= K && dis[to][Floor + 1] > dis[now][Floor]) { dis[to][Floor + 1] = dis[now][Floor]; q.push(NewNode(to, dis[to][Floor + 1], Floor + 1)); } } } } }
signed main() { using namespace Solution; using FastIO::getint; n = getint(); m = getint(); k = getint(); s = getint(); t = getint(); K = k; For (i, 1, m) { int prev = getint(); int next = getint(); int weight = getint(); addEdge(prev, next, weight); addEdge(next, prev, weight); } SPFA(); int ans = 2147482333; for (int i = 0; i <= k; ++i) { ans = std::min(ans, dis[t][i]); } FastIO::putint(ans, '\n'); return 0; }
|