POJ 2449 Remmarguts' Date (第k短路 A*搜索算法模板)

Remmarguts‘ Date

Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 22412   Accepted: 6085

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

Source

POJ Monthly,Zeyuan Zhu

题目链接:http://poj.org/problem?id=2449

题目大意:n个点,m条路,给出起点s和终点t,求从s到t的第k短路的长度

题目分析:A*算法模板题

#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
int const INF = 0xfffffff;
int const MAX1 = 1005;
int const MAX2 = 1e5 + 5;

int dis[MAX1], head[MAX1], head2[MAX1];
int n, m, cnt;
bool vis[MAX1];

struct node
{
    int v, w;
    int next;
}e[MAX2], e2[MAX2];

struct node1
{
    int v, g, f;
    bool operator < (const node1 &r) const
    {
        if(r.f == f)
            return r.g < g;
        return r.f < f;
    }
};

void Add(int u, int v, int w)
{
    e[cnt].v = v;
    e[cnt].w = w;
    e[cnt].next = head[u];
    head[u] = cnt;

    e2[cnt].v = u;
    e2[cnt].w = w;
    e2[cnt].next = head2[v];
    head2[v] = cnt++;
}

bool spfa(int s)
{
    memset(vis, false, sizeof(vis));
    dis[s] = 0;
    queue <int> q;
    q.push(s);
    while(!q.empty())
    {
        int cur = q.front();
        q.pop();
        vis[cur] = false;
        for(int i = head2[cur]; i != -1; i = e2[i].next)
        {
            if(dis[e2[i].v] > dis[cur] + e2[i].w)
            {
                dis[e2[i].v] = dis[cur] + e2[i].w;
                if(!vis[e2[i].v])
                {
                    vis[e2[i].v] = true;
                    q.push(e2[i].v);
                }
            }
        }
    }
}

int A_star(int s, int t, int k)
{
    if(s == t)
        k++;
    if(dis[s] == INF)
        return -1;
    priority_queue <node1> q;
    int num = 0;
    node1 tmp, to;
    tmp.v = s;
    tmp.g = 0;
    tmp.f = tmp.g + dis[tmp.v];
    q.push(tmp);
    while(!q.empty())
    {
        tmp = q.top();
        q.pop();
        if(tmp.v == t)
            num++;
        if(num == k)
            return tmp.g;
        for(int i = head[tmp.v]; i != -1; i = e[i].next)
        {
            to.v = e[i].v;
            to.g = tmp.g + e[i].w;
            to.f = to.g + dis[to.v];
            q.push(to);
        }
    }
    return -1;
}

int main()
{
    scanf("%d %d", &n, &m);
    cnt = 0;
    memset(head, -1, sizeof(head));
    memset(head2, -1, sizeof(head2));
    for(int i = 0; i < MAX1; i++)
        dis[i] = INF;
    while(m--)
    {
        int u, v, w;
        scanf("%d %d %d", &u, &v, &w);
        Add(u, v, w);
    }
    int s, t, k;
    scanf("%d %d %d", &s, &t, &k);
    spfa(t);
    printf("%d\n", A_star(s, t, k));
}

POJ 2449 Remmarguts' Date (第k短路 A*搜索算法模板)

时间: 2024-10-21 09:35:39

POJ 2449 Remmarguts' Date (第k短路 A*搜索算法模板)的相关文章

poj 2449 Remmarguts&#39; Date(K短路,A*算法)

http://poj.org/problem?id=2449 大致题意:给出一个有向图,求从起点到终点的第K短路. K短路与A*算法详解  学长的博客... 算法过程 #include <stdio.h> #include <iostream> #include <algorithm> #include <set> #include <map> #include <vector> #include <math.h> #in

poj 2449 Remmarguts&#39; Date (k短路模板)

Remmarguts' Date http://poj.org/problem?id=2449 Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 30772   Accepted: 8397 Description "Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. Softly tou

POJ 2449 Remmarguts&#39; Date ( 第 k 短路 &amp;&amp; A*算法 )

题意 : 给出一个有向图.求起点 s 到终点 t 的第 k 短路.不存在则输出 -1 #include<stdio.h> #include<string.h> #include<queue> #include<algorithm> using namespace std; const int INF = 0x3f3f3f3f; const int maxn = 1024; const int maxm = 100008; struct EDGE{ int v

【POJ】2449 Remmarguts&#39; Date(k短路)

http://poj.org/problem?id=2449 不会.. 百度学习.. 恩. k短路不难理解的. 结合了a_star的思想.每动一次进行一次估价,然后找最小的(此时的最短路)然后累计到k 首先我们建反向边,跑一次从汇到源的最短路,将跑出来的最短路作为估价函数h 根据f=g+h 我们将源s先走,此时实际价值g为0,估价为最短路(他们的和就是s-t的最短路) 将所有s所连的边都做相同的处理,加入到堆中(假设此时到达的点为x,那么x的g等于s到这个点的边权,因为根据最优,g+h此时是从x

图论(A*算法,K短路) :POJ 2449 Remmarguts&#39; Date

Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 25216   Accepted: 6882 Description "Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. Softly touching his little ducks' head, h

poj 2449 Remmarguts&#39; Date k短路

/*poj 2449 k短路 A* 估价函数是 s到i的距离+i到t的距离 */ #include<cstdio> #include<queue> #include<vector> #define inf 1e7 #define maxn 100010 using namespace std; int n,m,S,T,K,num1,num2,head1[maxn],head2[maxn],dis[maxn]; int q[maxn],hea,tai,f[maxn],cn

POJ 2449 Remmarguts&#39; Date ( Dijkstra + A* 求解第K短路 )

#include <iostream> #include <cstring> #include <queue> #include <fstream> using namespace std; #define E 100005 #define V 1005 #define INF 1 << 30 int heads[V], r_heads[V]; int dists[V]; bool visits[V]; int nEdgeNum, nNodeNu

poj 2449 Remmarguts&#39; Date A*+spfa求第k短路

题意: 经典的第k短路,A*算法的经典应用之一. 分析: A*,已走的路程g+到终点的最短距离为启发函数,搜索过程中不判重,第k次到t节点时就求出了第k短路. 代码: //poj 2449 //sep9 #include <iostream> #include <queue> using namespace std; const int maxN=1024; const int maxM=100024; int n,m,s,t,k,e,ne; int head[maxN],nhea

POJ 2449 Remmarguts&#39; Date (A*搜索求K短路)

传送门 这是一道裸的K短路的问题,我们将会用A*解决. 我们设计估值函数h的时候可以像这样想.因为h(n) <= h*(n)而且要尽量接近h*(n),所以我们想到,可以求一个从目标节点到其余节点的最短路,这个一定是小于等于实际值的.然后就用A*从起点开始搜索,找到一个节点v,就使cnt[v]加1.当cnt[v] > k时就可以剪枝了,因为这一定不再K短路的路线上了.很好通过反证法得到证明.当目标节点被搜索到了第k次的时候就可以结束搜索了. 要注意这道题有一个很坑的地方,就是若给出的起点=终点,