# K短路【模板】

A*+SPFA算法：
(1)将有向图的所有边正向、反向分别存入两个不同的边集(Edges，Edges1)中。用反向边集，以所求终点t为源点，利用SPFA或Dijkstra求解出所有点到t的最短路径，用Dist[i]数组来表示点i到点t的最短距离。
(2)建立一个优先队列，将源点s加入到队列中。
(3)从优先队列中取出最小的点p，如果点p == t，则计算t出队的次数。如果当前路径长度就是s到t的第k短路长度，算法结束。否则遍历与p相连的所有的边，将扩展出的到p的邻接点信息加入到优先队列中取。

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int MAXN = 1100;
const int MAXM = 110000;
const int INF = 0xffffff0;

struct EdgeNode
{
int to;
int w;
int next;
}Edges[MAXM],Edges1[MAXM];

struct Node
{
int to;
int g,f;
bool operator < (const Node &r) const
{
if(r.f == f)
return r.g < g;
return r.f < f;
}
};
int vis[MAXN],Dist[MAXN];

int A_Star(int start,int end,int N,int k)
{
Node e,ne;
int Cnt = 0;
priority_queue<Node> que;
if(start == end)
k++;
if(Dist[start] == INF)
return -1;
e.to = start;
e.g = 0;
e.f = e.g + Dist[e.to];
que.push(e);
while( !que.empty() )
{
e = que.top();
que.pop();
if(e.to == end)
Cnt++;
if(Cnt == k)
return e.g;

for(int i = Head[e.to]; i != -1; i = Edges[i].next)
{
ne.to = Edges[i].to;
ne.g = e.g + Edges[i].w;
ne.f = ne.g + Dist[ne.to];
que.push(ne);
}
}
return -1;
}
void SPFA(int s,int N)
{
for(int i = 0; i <= N; ++i)
Dist[i] = INF;
memset(vis,0,sizeof(vis));
vis[s] = 1;
Dist[s] = 0;
queue<int> Q;
Q.push(s);
while( !Q.empty() )
{
int u = Q.front();
Q.pop();
vis[u] = 0;
for(int i = Head1[u]; i != -1; i = Edges1[i].next)
{
int temp = Dist[u] + Edges1[i].w;
if(temp < Dist[Edges1[i].to])
{
Dist[Edges1[i].to] = temp;
if(!vis[Edges1[i].to])
{
vis[Edges1[i].to] = 1;
Q.push(Edges1[i].to);
}
}
}
}
}

int main()
{
int N,M,u,v,w,s,t,k;
while(~scanf("%d%d",&N,&M))
{
memset(Edges,0,sizeof(Edges));
memset(Edges1,0,sizeof(Edges1));
for(int i = 0; i < M; ++i)
{
scanf("%d%d%d",&u,&v,&w);
Edges[i].to = v;
Edges[i].w = w;

Edges1[i].to = u;
Edges1[i].w = w;
}
scanf("%d%d%d",&s,&t,&k);
SPFA(t,N);
int kthlenth = A_Star(s,t,N,k);
printf("%d\n",kthlenth);
}

return 0;
}