在进行最短路搜索时,会有大量无效的搜索。A*使用一个估价函数,估计当前点到终点的最短距离,在估计值小于等于真实值却大于等于0的情况下,通过BFS可以更快的找到最短路径。
求第K短路
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
typedef pair<int, PII> PIII;
const int N = 1010, M = 200010;
int n, m, S, T, K;
int h[N], rh[N], e[M], w[M], ne[M], idx;
int dist[N];
bool st[N];
void add(int h[], int a, int b, int c) // 添加一条边a->b,边权为c
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}
void dijkstra() // 求1号点到n号点的最短路距离
{
priority_queue<PII, vector<PII>, greater<PII>>heap;
heap.push({0,T});
memset(dist, 0x3f, sizeof dist);
dist[T] = 0;
while(heap.size())
{
auto t = heap.top();
heap.pop();
int ver = t.y;
if(st[ver])continue;
st[ver] = true;
for(int i = rh[ver]; ~i; i = ne[i])
{
int j = e[i];
if(dist[j] > dist[ver] + w[i])
{
dist[j] = dist[ver] + w[i];
heap.push({dist[j], j});
}
}
}
}
int cnt[N];
int astar()
{
priority_queue<PIII, vector<PIII>, greater<PIII>> heap;
heap.push({dist[S], {0, S}});
while(heap.size())
{
auto t = heap.top();
heap.pop();
int ver = t.y.y, dis = t.y.x;
cnt[ver]++;
if(cnt[T] == K) return dis;
for(int i = h[ver]; ~i; i = ne[i])
{
int j = e[i];
if(cnt[j] < K)
heap.push({dis+ w[i] + dist[j], {dis + w[i], j}});
}
}
return -1;
}
int main()
{
scanf("%d%d", &n, &m);
memset(h, -1, sizeof h);
memset(rh, -1, sizeof rh);
for(int i = 0; i < m; i ++ )
{
int a, b, c;
scanf("%d%d%d", &a, &b,&c);
add(h, a, b, c);
add(rh, b, a, c);
}
scanf("%d%d%d", &S,&T,&K);
if(S == T) K ++;
dijkstra();
cout << astar();
}
AcWing 179. 八数码
在这里插入代码片