Description
"Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. Softly touching his little ducks' head, he told them a story.Input
The first line contains two integer numbers N and M (1 <= N <= 1000, 0 <= M <= 100000). Stations are numbered from 1 to N. Each of the following M lines contains three integer numbers A, B and T (1 <= A, B <= N, 1 <= T <= 100). It shows that there is a directed sideway from A-th station to B-th station with time T.Output
A single line consisting of a single integer number: the length (time required) to welcome Princess Uyuw using the K-th shortest path. If K-th shortest path does not exist, you should output "-1" (without quotes) instead.Sample Input
2 2Sample Output
14Source
POJ Monthly,Zeyuan Zhu
题目大意:公主要求王子通过第k短的路径去找她。给出了N个点,M条单向边的图。也给出了
起点s(王子所在的点)、终点t(公主所在的点)和k。问:K短路是多少。
思路:第一次做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的邻接点信息加入
到优先队列中取。
注意:当s == t的时候,需要计算第k+1短路。因为s到t这条距离为0的路不能算是这k短路里边,
当s == t的时候,只需要将k = k+1后再求第k短路就可以了。这也是这道题的关键点之一。
#include
#include
#include
#include
#include
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];
int Head[MAXN],Head1[MAXN];
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 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 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));
memset(Head,-1,sizeof(Head));
memset(Head1,-1,sizeof(Head1));
for(int i = 0; i < M; ++i)
{
scanf("%d%d%d",&u,&v,&w);
Edges[i].to = v;
Edges[i].w = w;
Edges[i].next = Head[u];
Head[u] = i;
Edges1[i].to = u;
Edges1[i].w = w;
Edges1[i].next = Head1[v];
Head1[v] = i;
}
scanf("%d%d%d",&s,&t,&k);
SPFA(t,N);
int kthlenth = A_Star(s,t,N,k);
printf("%d\n",kthlenth);
}
return 0;
}