分析
分层最短路模板题。
话说我卡了半天发现是 insert
函数写错了???
代码
#include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define N 100001 #define il inline #define re register #define INF 0x3f3f3f3f #define tie0 cin.tie(0),cout.tie(0) #define fastio ios::sync_with_stdio(false) #define File(x) freopen(x".in","r",stdin);freopen(x".out","w",stdout) using namespace std; typedef long long ll; template <typename T> inline void read(T &x) { T f = 1; x = 0; char c; for (c = getchar(); !isdigit(c); c = getchar()) if (c == '-') f = -1; for ( ; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); x *= f; } struct edge { int to, val, nxt; } e[N]; struct node { int id, d; friend bool operator < (node x, node y) { return x.d > y.d; } }; int n, m, k; int head[N], cnt, dis[N]; bool inq[N]; void insert(int u, int v, int w) { e[++cnt].to = v, e[cnt].val = w, e[cnt].nxt = head[u], head[u] = cnt; } void add(int u, int v, int w) { insert(u, v, w), insert(v, u, w); } void Dijkstra() { priority_queue <node> q; memset(dis, INF, sizeof dis); dis[1] = 0; q.push((node){1, 0}); while (!q.empty()) { int u = q.top().id; q.pop(); if (inq[u]) continue; inq[u] = 1; for (int i = head[u]; i; i = e[i].nxt) { int v = e[i].to, w = e[i].val; if (dis[v] > max(dis[u], w)) { dis[v] = max(dis[u], w); if (!inq[v]) q.push((node){v, dis[v]}); } } } } int main() { int u, v, w; read(n), read(m), read(k); for (int i = 1; i <= m; ++i) { read(u), read(v), read(w); add(u, v, w); for (int j = 1; j <= k; ++j) { add(u + j * n, v + j * n, w); insert(u + (j - 1) * n, v + j * n, 0); insert(v + (j - 1) * n, u + j * n, 0); } } Dijkstra(); if (dis[k*n+n] < INF) printf("%d", dis[k*n+n]); else puts("-1"); return 0; }