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
edges[maxm],edges1[maxm];
int head[maxn],head1[maxn];
struct node
};int vis[maxn],dist[maxn];
int a_star(int start,int end,int n,int k)
}return -1;
}void spfa(int s,int n)}}
}}int main()
scanf("%d%d%d",&s,&t,&k);
spfa(t,n);
int kthlenth = a_star(s,t,n,k);
printf("%d\n",kthlenth);
}return
0;}
第K短路(模板)
沒太想明白 就當存在模板吧 include include include include include include include include include include using namespace std typedef long long ll const int maxn ...
K短路問題模板(spfa A )
給乙個圖,起點s 終點t k,求起點到終點的第k短路。基本思路 首先spfa求出反向圖中求出終點t到其他所有點的距離 預處理 再從起點開始使用優先佇列進行寬搜,用cnt記錄到達終點的次數,當cnt k時的路徑長度即為所得。搜尋的方向用乙個估價函式f g dis來確定,其中g表示起點到當前點的路徑長度...
poj 2449 k短路模板
include include include include include using namespace std const int maxn 1001 const int maxm 100001 const int inf 1 30 struct edge edge e1 maxm edge...