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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
| #include <bits/stdc++.h> #define ll long long #define lll long long namespace FAST_IO {
inline char nextChar() { static char buf[1000000], *p1 = buf, *p2 = buf; return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2) ? EOF : *p1++; } #define getch getchar template <class T> inline void read(T &x) { T flag = 1; x = 0; char ch = getch(); while (ch < '0' || ch > '9') { if (ch == '-') flag = -1; ch = getch(); } while (ch >= '0' && ch <= '9') { x = (x << 3) + (x << 1) + (ch ^ 48), ch = getch(); } x *= flag; }
template <class T, class... _T> inline void read(T &x, _T &...y) { return read(x), read(y...); }
inline void print128(lll x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print128(x / 10); putchar(x % 10 + '0'); }
} using namespace std; using namespace FAST_IO; const ll mod = 1e9 + 7; const int INF = 0x3f3f3f3f; const ll INF_LL = 0x3f3f3f3f3f3f3f3f; const double eps = 1e-5; const int maxn = 1e3 + 10; const int maxm = 2e5 + 10; int t, n, m, k; int s, e;
struct Edge { int u, v, w, next; } edge[maxm]; int head[maxn], rhead[maxn], cnt = 0;
inline void addEdge(int u, int v, int w) { edge[++cnt] = {u, v, w, head[u]}; head[u] = cnt; edge[++cnt] = {v, u, w, rhead[v]}; rhead[v] = cnt; } int h[maxn]; struct Node { int u; int dis; bool operator<(const Node &p) const { return dis < p.dis; } };
bool vis[maxn]; void dijkstra(int s) { priority_queue<Node> q; memset(h, 0x3f, sizeof(h)); memset(vis, false, sizeof(vis)); h[s] = 0; q.push({s, 0}); while (q.size()) { auto out = q.top(); q.pop();
int u = out.u; if (vis[u]) continue; vis[u] = true;
for (int i = rhead[u]; i; i = edge[i].next) { int v = edge[i].v; if (h[v] > h[u] + edge[i].w) { h[v] = h[u] + edge[i].w; q.push({v, -h[v]}); } } } }
int times[maxn]; int astar() { priority_queue<Node> q; q.push({s, -h[s]}); while (q.size()) { auto out = q.top(); q.pop(); int u = out.u; int gdis = -out.dis - h[u]; times[u]++; if (times[e] == k) { return gdis; } for (int i = head[u]; i; i = edge[i].next) { int v = edge[i].v; if (times[v] < k) { q.push({v, -(gdis + edge[i].w + h[v])}); } } } return -1; }
int main() {
#ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); #endif read(n, m); for (int i = 1, u, v, w; i <= m; i++) { read(u, v, w); addEdge(u, v, w); } read(s, e, k); if (s == e) k++; dijkstra(e); int ret = astar(); cout << ret << endl; return 0; }
|