我是靠谱客的博主 传统店员,最近开发中收集的这篇文章主要介绍POJ 2449 第k短路模版题 (dijkstra + A star),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Remmarguts' Date

Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 35009 Accepted: 9461

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

题意: 给你一张有向图, 求起点s 到 终点 t 的第 k 短路径

思路: 由 A* 算法, 设 f(x) = g(x) + h(x), 其中 h(x)表示点 x 到终点 t 的最短路径(所以我们需要先存一个反向图,用这张图做dijkstra算法, 求最短路径),  g(x) 则表示当前点到起点 s 走过的路径长度。 我们用一个优先队列维护 f(x), 每一次取 f(x) 值最小的

可看大牛的博客了解 A* 及其他 :https://blog.csdn.net/pi9nc/article/details/12256019

AC代码:

#include<cstdio>
#include<vector>
#include<queue>
using namespace std;

const int maxn = 50010;
vector<pair<int,int> >E[maxn];      ///E[i] 表示正向图, 点 i 到 点E[i].first 有一条长为 E[i].second 的路径
vector<pair<int,int> >REE[maxn];    ///反向图
int ans[maxn];                       ///ans[i] 数组表示到达 点i 的次数
int redis[maxn];                     ///redis[i] 表示 i 到终点 t 的最短路径
struct a_star{
    int hv,gx;      ///gx 表示当前点到起点 s 走过的路径长度, hv 表示点v 到终点 t 的 最短距离 的那个点
    friend bool operator < (const a_star &A,const a_star &B){
        return A.gx + redis[A.hv] > B.gx + redis[B.hv];
    }
};

void init(){
    for(int i = 0;i < maxn;i ++){
        E[i].clear();
        REE[i].clear();
        redis[i] = 1e9;
        ans[i] = 0;
    }
}

void dijkstra(int t,int s){ ///求反向图的最短距离,即t为起点,s为终点
    redis[t] = 0;
    priority_queue<pair<int,int> >Q;
    Q.push(make_pair(-redis[t],t));
    while(!Q.empty()){
        int u = Q.top().second;
        Q.pop();
        for(int j = 0;j < REE[u].size();j ++){
            int v = REE[u][j].first;
            if(redis[v] > redis[u] + REE[u][j].second){
                redis[v] = redis[u] + REE[u][j].second;
                Q.push(make_pair(-redis[v],v));
            }
        }
    }
}

int A_star(int s,int t,int k){
    if(s == t) k ++;
    if(redis[s] == 1e9) return -1;
    priority_queue<a_star>Q;
    a_star A;
    A.gx = 0; A.hv = s;             ///初始化
    Q.push(A);
    while(!Q.empty()){
        a_star tmp = Q.top();
        Q.pop();
        ans[tmp.hv] ++;
        if(ans[t] == k) return tmp.gx;
        for(int i = 0;i < E[tmp.hv].size();i ++){
            a_star tmp2;
            tmp2.gx = tmp.gx + E[tmp.hv][i].second;
            tmp2.hv = E[tmp.hv][i].first;
            Q.push(tmp2);
        }
    }
    return -1;
}

int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m)){
        init();
        int A,B,T;
        for(int i = 0;i < m;i ++){
            scanf("%d%d%d",&A,&B,&T);
            E[A].push_back(make_pair(B,T));
            REE[B].push_back(make_pair(A,T));
        }
        int s,t,k;
        scanf("%d%d%d",&s,&t,&k);
        dijkstra(t,s);
        printf("%dn",A_star(s,t,k));
    }
    return 0;
}

 

最后

以上就是传统店员为你收集整理的POJ 2449 第k短路模版题 (dijkstra + A star)的全部内容,希望文章能够帮你解决POJ 2449 第k短路模版题 (dijkstra + A star)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(42)

评论列表共有 0 条评论

立即
投稿
返回
顶部