标签:
给一个n个点m条边的图,然后给一个起点和一个终点,求起点到终点的第K短路.
求第K短路。一个经典的问题。
SPFA+A*
核心思想在A*搜索的估计函数的建立上。
F(x) = g(x) + h(x)
估价函数 = s到x的距离 + x到t的距离
估价函数的含义就是经过x这个点的路径的距离。
我们在搜索的时候每次选择估价函数较小的值,进行拓展。这样我们搜索到t点的状态出来顺序就是,最短路…次短路….第三短路…
就减少了我们搜索的状态数。
代码实现上,实现一个估价函数的结构体,然后是实现一个优先队列.x到t的距离直接建立一个反图,跑一遍SPFA,就可以得到所以点到终点的最短路了。
/*
#pragma warning (disable: 4786)
#pragma comment (linker, "/STACK:0x800000")
*/
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <iterator>
#include <utility>
using namespace std;
template< class T > T _abs(T n)
{
return (n < 0 ? -n : n);
}
template< class T > T _max(T a, T b)
{
return (!(a < b) ? a : b);
}
template< class T > T _min(T a, T b)
{
return (a < b ? a : b);
}
template< class T > T sq(T x)
{
return x * x;
}
template< class T > T gcd(T a, T b)
{
return (b != 0 ? gcd<T>(b, a%b) : a);
}
template< class T > T lcm(T a, T b)
{
return (a / gcd<T>(a, b) * b);
}
template< class T > bool inside(T a, T b, T c)
{
return a<=b && b<=c;
}
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define F(i, n) for(int (i)=0;(i)<(n);++(i))
#define rep(i, s, t) for(int (i)=(s);(i)<=(t);++(i))
#define urep(i, s, t) for(int (i)=(s);(i)>=(t);--(i))
#define repok(i, s, t, o) for(int (i)=(s);(i)<=(t) && (o);++(i))
#define MEM0(addr) memset((addr), 0, sizeof((addr)))
#define MP(x, y) make_pair(x, y)
#define REV(s, e) reverse(s, e)
#define SET(p) memset(pair, -1, sizeof(p))
#define CLR(p) memset(p, 0, sizeof(p))
#define MEM(p, v) memset(p, v, sizeof(p))
#define CPY(d, s) memcpy(d, s, sizeof(s))
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define SZ(c) (int)c.size()
#define PB(x) push_back(x)
#define ff first
#define ss second
#define ll long long
#define ld long double
#define pii pair< int, int >
#define psi pair< string, int >
#define ls u << 1
#define rs u << 1 | 1
#define lson l, mid, u << 1
#define rson mid, r, u << 1 | 1
#define debug(x) cout << #x << " = " << x << endl
const int MAXN = 1010;
const int MAXM = 100010;
const int maxnum = 100010;
const int INF = 0x3f3f3f3f;
const double eps = 1e-10;
const int mod = 1e9;
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 vis[MAXN],d[MAXN],cnt;
int head[MAXN],revhead[MAXN];
int m,n,s,t,k;
void init(){
cnt = 0;
MEM(head,-1);
MEM(revhead,-1);
}
void addedge(int x,int y,int t){
edge[cnt].v = y;
edge[cnt].w = t;
edge[cnt].next = head[x];
head[x] = cnt;
revedge[cnt].v = x;
revedge[cnt].w = t;
revedge[cnt].next = revhead[y];
revhead[y] = cnt++;
}
void spfa(int src){
rep(i,1,n) d[i] = INF;
MEM0(vis);
vis[src] = 0;
d[src] = 0;
queue<int> que;
//while(!que.empty()) que.pop();
que.push(src);
while(!que.empty()){
int u = que.front();
que.pop();
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]){
que.push(v);
vis[v] = 1;
}
}
}
}
}
int solve(int src,int des){
priority_queue<A> q;
int cnt = 0;
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(){
//READ("in.txt");
while(scanf("%d%d",&n,&m)!=EOF){
init();
rep(i,1,m){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w);
}
scanf("%d%d%d",&s,&t,&k);
spfa(t);
//debug(d[2]);
printf("%d\n", solve(s,t));
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 2448(K短路,A*+SPFA) Remmarguts' Date
标签:
原文地址:http://blog.csdn.net/notdeep__acm/article/details/47259949