飞行路线
实际上就是建立k + 1层图
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int N = 110010, M = 2500010;
int n, m, k, s, t;
int tot;
priority_queue<pair<int, int>> q;
int h[N], e[M], ne[M], dist[N], idx, w[M];
bool st[N];
void add(int a, int b, int c){
e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx ++ ;
}
void dijkstra(int s){
memset(dist, 0x3f, sizeof dist);
dist[s] = 0;
q.push({0, s});
while(q.size()){
int x = q.top().second;
q.pop();
if(st[x]) continue;
st[x] = true;
for(int i = h[x]; i != -1; i = ne[i]){
int j = e[i];
if(dist[j] > dist[x] + w[i]){
dist[j] = dist[x] + w[i];
q.push({-dist[j], j});
}
}
}
}
int main(){
scanf("%d%d%d", &n, &m, &k);
scanf("%d%d", &s, &t);
memset(h, -1, sizeof h);
for(int i = 0, a, b, c; i < m; i ++ ){
scanf("%d%d%d", &a, &b, &c);
add(a, b, c), add(b, a, c);
for(int j = 1; j <= k ; j ++ ){
add(a + (j - 1) * n, b + j * n, 0);
add(b + (j - 1) * n, a + j * n, 0);
add(a + j * n, b + j * n, c);
add(b + j * n, a + j * n, c);
}
}
for(int i = 1; i <= k; i ++ ){
add(t + (i - 1) * n, t + i * n, 0);
}
dijkstra(s);
printf("%d\n", dist[t + n * k]);
return 0;
}
想请教一下为什么你这个 dij 的板子和y总讲的不太一样?
hh,这个是堆优化版的啦