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
| #include <bits/stdc++.h>
using namespace std;
const int MAXN = 510000,MAXM = 4010000;
struct Edge{
int to,len,nex;
}edge[MAXM];int ecnt = 2;
int fir[MAXN];
void addedge(int a,int b,int c){
edge[ecnt] = (Edge){b,c,fir[a]};
fir[a] = ecnt++;
}
int n,m,k,s,t;
int _hash(int nown,int c){
return (c-1) * n + nown;
}
void init(){
scanf("%d %d %d %d %d",&n,&m,&k,&s,&t);
s++,t++;
for(int i = 1;i<=m;i++){
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
a++,b++;
for(int w = 1;w<=k+1;w++){
addedge(_hash(a,w),_hash(b,w),c);
addedge(_hash(b,w),_hash(a,w),c);
if(w != k+1){
addedge(_hash(a,w),_hash(b,w+1),0);
addedge(_hash(b,w),_hash(a,w+1),0);
}
}
}
}
int dis[MAXN],vis[MAXN];
struct Node{
int x,d;
bool operator < (const Node &_n)const{
return d > _n.d;
}
};
priority_queue<Node> q;
void dij(){
while(!q.empty()) q.pop();
memset(dis,0x3f,sizeof(dis));
memset(vis,0,sizeof(vis));
int ss = _hash(s,1),tt = _hash(t,k+1);
dis[ss] = 0;
q.push((Node){ss,0});
while(!q.empty()){
Node now = q.top();q.pop();
int nown = now.x,nowd = now.d;
if(vis[nown] == 1) continue;
vis[nown] = 1,dis[nown] = nowd;
for(int nowe = fir[nown];nowe;nowe = edge[nowe].nex){
int v = edge[nowe].to,len = edge[nowe].len;
if(nowd + len < dis[v]){
dis[v] = nowd + len;
q.push((Node){v,dis[v]});
}
}
}
}
void solve(){
dij();
printf("%d\n",dis[_hash(t,k+1)]);
}
int main(){
init();
solve();
return 0;
}
|