A*算法—第K短路 ʕ •ᴥ•ʔ

概念啥的 看这位博主写的 https://blog.csdn.net/z_mendez/article/details/47057461?tdsourcetag=s_pctim_aiomsg

A*算法
本题有一种最朴素的办法:直接广度优先搜索,一开始路径(s, 0)(这里设路径(i, l)表示从s到i的一条长度为l的路径)入队。然后,每次取队中长度最短的路径,该路径(i, l)出队,可以证明,若这是终点为i的路径第x次出队,该路径一定是图中从s到i的第x短路(若x>K则该路径已无用,舍弃)。然后从点i扩展,将扩展到的路径全部入队。这样直到终点为t的路径第K次出队即可。
该算法容易实现(借助priority_queue),但时间复杂度可能达到O(MK),需要优化。
优化:容易发现该算法其实有A*的思想,或者说,该算法其实是所有结点的估价函数h()值均为0的A*算法。为了优化此题,需要将h()值改大。显然,h(i)值可以设为从i到t的最短路径长度(容易证明它是一致的),然后g(i)=目前结点代表的路径长度,f(i)=g(i)+h(i),然后A*即可。
注意:更改路径条数应该在出队时更改,而不能在入队时更改,因为可能在该路径出队之前会有新的比它更短的路径入队。

1.用dijkstra逆向计算出所有点到t的最短距离dis[i]
2.建立估价函数 f(i)= g(i)+h(i)
  其中g(i)为从s到i的实际距离,h(i)为从i到t的最短路dis[i]
3.使用上述估计函数用A*算法从s点出发BFS全图,用visited[i]表示点i的出队次数。
  当visited[i]=k时,即找到从s到t的k短路为f(i)
  其间若visited[i]>k则放弃该点

4.注意:若s==t,需 k+1;

上面的这些都是别人的见解 我看了几天 然后写一下自己的见解 就拿poj 2449 来说 我们根据上面的四步一步步的实现

第一步 我们需要用dijkstra逆向计算出所有点到t的最短距离dis[i]

第二步 建立估价函数 f(i)= g(i)+h(i) 其中g(i)为从s到i的实际距离,h(i)为从i到t的最短路dis[i]

为什么这样做? A*算法的精髓在于 你找到一个起点到i的距离加上i到e的最短距离 然后放入队列里面  这样就可以实现最小

第三步 用flag表示点 i 的出队次数 当visited[i]=k时,即找到从s到t的k短路为f(i)  其间若flag>k则放弃该点

需要主要的是 当s==t 需k+1

 

Remmarguts' Date

Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 37226 Accepted: 10244

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
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstring>
#define N 0x3f3f3f3f
#define maxn 200010
using namespace std;
struct ac
{
	int v,w,next;
}e[maxn],ee[maxn];
struct acc
{
	int v,g,f;
	bool operator<(const acc &r)const//bool operator<(const acc &r) const
	{
		if(r.f==f)
		return r.g<g;
		return r.f<f;
	}
};
int head[1010],head2[1010],vis[1010],dis[1010],cnt,cnt2;
int n,m,s,t,k,ans;

// 数组模拟链表  不会的先学这个哈~ 
void init()
{
	memset(head,-1,sizeof(head));
	memset(head2,-1,sizeof(head2));
	cnt=cnt2=1;
}
void add(int u,int v,int w)
{
	e[cnt].v=v;
	e[cnt].w=w;
	e[cnt].next=head[u];
	head[u]=cnt++;
}
void add2(int u,int v,int w)
{
	ee[cnt2].v=v;
	ee[cnt2].w=w;
	ee[cnt2].next=head2[u];
	head2[u]=cnt2++;
}

// spfa算法  上次有一道用这个写的用来 判断负环
// 我们这里反向dijkstra逆向计算出所有点到t最短距离
 
int spfa(int s,int n)
{
	queue<int>q;
	int num[1010];
	for(int i=1;i<=n;i++)
	{
		dis[i]=N;
		num[i]=0;
	}
	dis[s]=0;
	q.push(s);
	num[s]++;
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		num[u]--;
		if(num[u]>n)// 这里是判断负环 
		return 0;
		for(int i=head2[u];i+1;i=ee[i].next)
		{
			if(dis[ee[i].v]>dis[u]+ee[i].w)
			{
				dis[ee[i].v]=dis[u]+ee[i].w;
				if(!num[ee[i].v])
				{
					num[ee[i].v]++;
					q.push(ee[i].v);
				}
			}
		}
	}
	return 1;
}

// 建立估价函数 f(i)=g(i)+h(i) 
int a_star(int s,int t,int k)
{
	acc ne,nne;
	int flag=0;
	priority_queue<acc>qq;
	if(s==t)// 坑点  
	k++; //  如果一开始 s==t k要需要加一
	if(dis[s]==N)
	return -1;
	ne.v=s;
	ne.g=0;
	ne.f=ne.g+dis[ne.v];
	qq.push(ne);
	while(!qq.empty())
	{
		ne=qq.top();
		qq.pop();
		if(ne.v==t)
		flag++;
		if(flag==k)
		return ne.g;
		for(int i=head[ne.v];i+1;i=e[i].next)
		{
			// f(i)=g(i)+h(i)
			// g(i)为从s到i的实际距离
			// h(i)为从i到t的最短路dis[i]
			
			nne.v=e[i].v;//记录点
			nne.g=ne.g+e[i].w;// 记录实际路线
			nne.f=nne.g+dis[nne.v]; // 估价函数
			qq.push(nne); //放入优先队列
		}
	}
	return -1;
}
int main()
{
	while(cin>>n>>m)
	{
		init();
		for(int i=1;i<=m;i++)
		{
			int a,b,c;
			cin>>a>>b>>c;
			add(a,b,c);
			add2(b,a,c);//存反向图 
		}
		cin>>s>>t>>k;
		spfa(t,n);//反向跑一遍 
		ans=a_star(s,t,k);
		cout<<ans<<endl;
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值