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
|
#include<bits/stdc++.h> using namespace std;
#define x first #define y second
const int N=1010, M=20010;
typedef pair<int, int> PII; typedef pair<int, PII> PIII;
int n, m, S, T, K; int h[N], rh[N], e[M], w[M], ne[M], idx; int dist[N]; bool st[N];
void add(int h[], int a, int b, int c){ e[idx]=b, w[idx]=c, ne[idx]=h[a], h[a]=idx++; }
void dijstra(int sta, int ed){ priority_queue<PII, vector<PII>, greater<PII>> heap; heap.push({0, sta});
memset(dist, 0x3f, sizeof dist); dist[sta]=0; memset(st, 0, sizeof st);
while(heap.size()){ auto[d, t]=heap.top(); heap.pop();
if(st[t]) continue; st[t]=true;
for(int i=rh[t];~i;i=ne[i]){ int j=e[i]; if(dist[j]>dist[t]+w[i]){ dist[j]=dist[t]+w[i]; heap.push({dist[j], j}); } } } }
int astar(int sta, int ed){ if(dist[sta]==0x3f3f3f3f) return -1;
priority_queue<PIII, vector<PIII>, greater<PIII>> heap; heap.push({0+dist[sta], {0, sta}});
int cnt=0; while(heap.size()){ auto[val, u]=heap.top(); auto[d, t]=u; heap.pop();
if(t==ed) cnt++; if(cnt==K) return d;
if(d>1e6+10) break;
for(int i=h[t];~i;i=ne[i]){ int j=e[i]; heap.push({d+w[i]+dist[j], {d+w[i], j}}); } }
return -1; }
void work(){ cin>>n>>m;
memset(h, -1, sizeof h); memset(rh, -1, sizeof rh); for(int i=0;i<m;i++){ int a, b, c; cin>>a>>b>>c; add(h, a, b, c), add(rh, b, a, c); }
cin>>S>>T>>K;
if(S==T) K++;
dijstra(T, S);
int res=astar(S, T);
cout<<res<<endl; }
int main(){ work(); return 0; }
|