洛谷P4568 [JLOI2011]飞行路线
\(\Large\textbf{Solution: } \large{容易想到跑分层图。就是细节比较多,数组一定要开大!!!}\)
\(\Large\textbf{Code: }\)
#include <bits/stdc++.h>
#define gc() getchar()
using namespace std;
const int N = 220005;
const int inf = 0x7fffffff;
int n, m, s, t, k, cnt, ret, head[N], dis[N], vis[N];
struct Edge {
int to, next, val;
}e[50005 * 45];
struct Node {
int num, d;
friend bool operator < (Node a, Node b) {
return a.d > b.d;
}
};
priority_queue<Node> q;
inline int read() {
int x = 0;
char ch = gc();
while (!isdigit(ch)) ch = gc();
while (isdigit(ch)) x = x * 10 + ch - '0', ch = gc();
return x;
}
inline void add(int x, int y, int val) {
e[++cnt].to = y;
e[cnt].val = val;
e[cnt].next = head[x];
head[x] = cnt;
}
inline void Dijkstra() {
for (int i = 1; i <=(k + 1) * n; ++i) dis[i] = inf;
dis[s] = 0;
q.push((Node){s, 0});
while (!q.empty()) {
Node cur = q.top(); q.pop();
int fr = cur.num;
if (vis[fr]) continue;
vis[fr] = 1;
for (int i = head[fr]; i ; i = e[i].next) {
int u = e[i].to;
if (vis[u]) continue;
if (dis[u] > dis[fr] + e[i].val) dis[u] = dis[fr] + e[i].val, q.push((Node) {u, dis[u]});
}
}
return;
}
int main() {
n = read(), m = read(), k = read();
s = read(), t = read();
++s, ++t;
int x, y, w;
for (int i = 1; i <= m; ++i) {
x = read(), y = read(), w = read(); ++x, ++y;
add(x, y, w), add(y, x, w);
for (int j = 1; j <= k; ++j) {
add(x + (j - 1) * n, y + j * n, 0); add(y + (j - 1) * n, x + j * n, 0);
add(x + j * n, y + j * n, w); add(y + j * n, x + j * n, w);
}
}
for (int i = 1; i <= k; ++i) add(n * (i - 1) + t, n * i + t, 0);
Dijkstra();
printf("%d\n", dis[n * k + t]);
return 0;
}