POJ 2449 Remmarguts' Date (K短路 A*算法)

时间:2022-08-03 23:57:48

题目链接

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.

"Prince Remmarguts lives in his kingdom UDF – United Delta of Freedom. One day their neighboring country sent them Princess Uyuw on a diplomatic mission."

"Erenow, the princess sent Remmarguts a letter, informing him that she would come to the hall and hold commercial talks with UDF if and only if the prince go and meet her via the K-th shortest path. (in fact, Uyuw does not want to come at all)"

Being interested in the trade development and such a lovely girl, Prince Remmarguts really became enamored. He needs you - the prime minister's help!

DETAILS: UDF's capital consists of N stations. The hall is numbered S, while the station numbered T denotes prince' current place. M muddy directed sideways connect some of the stations. Remmarguts' path to welcome the princess might include the same station twice or more than twice, even it is the station with number S or T. Different paths with same length will be considered disparate.

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.

The last line consists of three integer numbers S, T and K (1 <= S, T <= N, 1 <= K <= 1000).

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 2

1 2 5

2 1 4

1 2 2

Sample Output

14

分析:

对于A* ,它通过一个估价函数F(h)来估计图中的当前点p到终点的距离,并由此决定它的搜索方向,当这条路径失败时,它会尝试其他路径。

估价函数 = 当前值+当前位置到终点的距离,即 F(p)=g(p)+h(p),每次扩展估价函数值中最小的一个。对于k短路算法来说,g(p)为当前从s到p所走的路径的长度,h(p)为从点p到点 t 的最短路的长度,则F(p)的意义就是从s按照当前路径走到 p 后要走到终点 t 一共至少要走多远。也就是说我们每次的扩展都是有方向的扩展,这样就可以提高求解速度和降低扩展的状态数目。为了加速计算,h(p)需要在A*搜索之前进行预处理,只要将原图的所有边反向,再从终点 t 做一次单源最短路径就可以得到每个点的h(p)了。

具体步骤:

使用链式前向星来存储图,由于需要预处理所有点到终点T的最短路径,就需要将图G中所有的边反向得到图G',再从终点T做单元最短路径,所以实际上时两张图。

1.将有向图的所有边反向。以t(终点)为源点,求解点T到所有点的最短距离。其目的是要求所有点到终点T的最短距离,用数组dis[i]表示点i到点T的最短距离,其实dis[i]就是后面要用到的h(x)。这一步可以用Dijkstra算法或则SPFA算法。

2.新建一个优先队列,将源点S加入到队列中。

3.从优先队列中弹出f(p)最小的点p,如果点p就是t,则计算t出队的次数,如果当前为T的第k次出队,则当前路径的长度就是S到T的第k短路的长度,算法结束,否则遍历与p相连的所有的边,将扩展出的到p的邻接点信息加入到优先队列。

值得注意的是当S==T时需要计算k+1短路,因为S到T这条距离为0的路不能算在这k短路中,这时只需要将k加一后再求第k短路即可。

代码:dij

#include <stdio.h>
#include <string.h>
#include <queue>
#define M 100010
#define N 1005
const int inf = 0x3f3f3f3f;
using namespace std;
struct E //邻接表建边,to是下个结点,w 是权值 nxt 是下条边的位置
{
int to,w,nxt;
}edge[2*M]; struct data //g 表示起点到当前点的距离,h表终点到当前点的距离
{
int g,h;
int to;
bool operator < (data a) const //优先队列的排序(其实也不能这么讲) 使g+h小的在队首
{
return a.h + a.g < h + g;
}
};
int e,n,src,des,head[N],tail[N],dis[N];//head 是正向边,tail是逆向边 dis是des(终点)到各点的距离
void addedge (int cu,int cv,int cw)
{
edge[e].to = cv;
edge[e].w = cw;
edge[e].nxt = head[cu];
head[cu] = e ++;
edge[e].to = cu;
edge[e].w = cw;
edge[e].nxt = tail[cv];
tail[cv] = e ++;
} void dij () //dijstra算法求des到各点的距离 用于估价函数h
{
int i,j,k;
int vis[N];
memset (vis,0,sizeof(vis));
memset (dis,0x3f,sizeof(dis));
dis[des] = 0;
for (i = 1;i <= n;i ++)
{
k = -1;
int min = inf;
for (j = 1;j <= n;j ++)
if (!vis[j]&&min > dis[j])
{
k = j;
min = dis[j];
}
// if (k == -1) //因为这里图肯定是连通的 可加可不加
// break;
vis[k] = 1;
for (int u = tail[k];u != -1;u = edge[u].nxt)
{
int v;
v = edge[u].to;
if (!vis[v]&&dis[v] > dis[k] + edge[u].w)
dis[v] = dis[k] + edge[u].w;
}
}
} int Astar (int k) //A*算法求第k短路
{
int cnt[N];
data cur,nxt; //当前结点 下个结点
priority_queue <data> node;
memset (cnt,0,sizeof(cnt));
cur.to = src; //当前结点初始化 这就不用多说了
cur.g = 0;
cur.h = dis[src];
node.push (cur);
while (!node.empty())
{
cur = node.top ();
node.pop();
cnt[cur.to] ++;
if (cnt[cur.to] > k)//如果当前想拓展的点cnt>k就没必要拓展了
continue; //因为这个点已经是求到k+1短路了 从这个点继续往下搜肯定得到的是大于等于k+1短路的路径
if (cnt[des] == k) //找到第K短路 返回
return cur.g;
for (int u = head[cur.to];u != -1;u = edge[u].nxt) //相连的点入队列
{
int v = edge[u].to;
nxt.to = v;
nxt.g = cur.g + edge[u].w;
nxt.h = dis[v];
node.push (nxt);
}
}
return -1;
}
int main ()
{
int m,u,v,w,k;
while (~scanf ("%d%d",&n,&m))
{
e = 0;
memset (head,-1,sizeof (head));
memset (tail,-1,sizeof (tail));
while (m --)
{
scanf ("%d%d%d",&u,&v,&w);
addedge (u,v,w);
}
scanf ("%d%d%d",&src,&des,&k);
if (src == des) //起点和终点相同时,k要++
k ++;
dij ();
int ans = Astar (k);
printf ("%d\n",ans);
}
return 0;
}

spfa:

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#define MAXN 1005
#define MAXM 500005
#define INF 1000000000
using namespace std;
struct node
{
int v, w, next;
}edge[MAXM], revedge[MAXM];
struct A
{
int f, g, v;
bool operator <(const A a)const {
if(a.f == f) return a.g < g;
return a.f < f;
}
};
int e, vis[MAXN], d[MAXN], q[MAXM * 5];
int head[MAXN], revhead[MAXN];
int n, m, s, t, k;
void init()
{
e = 0;
memset(head, -1, sizeof(head));
memset(revhead, -1, sizeof(revhead));
}
void insert(int x, int y, int w)
{
edge[e].v = y;
edge[e].w = w;
edge[e].next = head[x];
head[x] = e;
revedge[e].v = x;
revedge[e].w = w;
revedge[e].next =revhead[y];
revhead[y] = e++;
}
void spfa(int src)
{
for(int i = 1; i <= n; i++) d[i] = INF;
memset(vis, 0, sizeof(vis));
vis[src] = 0;
int h = 0, t = 1;
q[0] = src;
d[src] = 0;
while(h < t)
{
int u = q[h++];
vis[u] = 0;
for(int i = revhead[u] ; i != -1; i = revedge[i].next)
{
int v = revedge[i].v;
int w = revedge[i].w;
if(d[v] > d[u] + w)
{
d[v] = d[u] + w;
if(!vis[v])
{
q[t++] = v;
vis[v] = 1;
}
}
}
}
}
int Astar(int src, int des)
{
int cnt = 0;
priority_queue<A>Q;
if(src == des) k++;
if(d[src] == INF) return -1;
A t, tt;
t.v = src, t.g = 0, t.f = t.g + d[src];
Q.push(t);
while(!Q.empty())
{
tt = Q.top();
Q.pop();
if(tt.v == des)
{
cnt++;
if(cnt == k) return tt.g;
}
for(int i = head[tt.v]; i != -1; i = edge[i].next)
{
t.v = edge[i].v;
t.g = tt.g + edge[i].w;
t.f = t.g + d[t.v];
Q.push(t);
}
}
return -1;
}
int main()
{
int x, y, w;
while(scanf("%d%d", &n, &m) != EOF)
{
init();
for(int i = 1; i <= m; i++)
{
scanf("%d%d%d", &x, &y, &w);
insert(x, y, w);
}
scanf("%d%d%d", &s, &t, &k);
spfa(t);
printf("%d\n", Astar(s, t));
}
return 0;
}