#include<cstdio> #include<queue> #include<cstring> using namespace std; const int N=500005; typedef pair<int, int> PII; typedef pair<int, PII> PIII; int head1[N], head2[N], net[N], edge[N], ver[N], tot; int dist[N], vis[N], n, m, k, a, b, ans[N];
void add(int h[],int a,int b,int c) { net[++tot]=h[a]; h[a]=tot; edge[tot]=c; ver[tot]=b; }
void astar() { int t=0; priority_queue<PIII, vector<PIII>, greater<PIII> > q; q.push({ dist[a], { 0, a } }); while(!q.empty()) { int v1=q.top().second.second; int distence=q.top().second.first; //printf("%d%d\n", v1,distence); q.pop(); if (distence+dist[v1]>=0x3f3f3f3f) continue; if (v1==b) { t++; if (t==k) { printf("%d",distence); return; } } for(int i=head1[v1];i;i=net[i]) { int v2=ver[i]; q.push({ distence+dist[v2]+edge[i], { distence+edge[i], v2 } }); } } printf("-1"); }
void dij() { memset(dist, 0x3f, sizeof(dist)); memset(vis, 0, sizeof(vis)); priority_queue <PII, vector<PII>, greater<PII> > q; q.push({ 0, b }); dist[b]=0; while(!q.empty()) { int v1=q.top().second;
q.pop(); if (vis[v1]) continue; vis[v1]=1; for(int i=head2[v1];i;i=net[i]) { int v2=ver[i]; if(dist[v2]>dist[v1]+edge[i]) { dist[v2]=dist[v1]+edge[i]; q.push({ dist[v2], v2 }); } } } }
int main() { scanf("%d%d",&n,&m); for(int i=1;i<=m;i++) { int u, v, l; scanf("%d%d%d",&u,&v,&l); add(head1, u, v, l); add(head2, v, u, l); } scanf("%d%d%d",&a,&b,&k); if (a==b) k++; dij(); astar(); }
|