poj 2449 Remmarguts' Date 第k短路 A*+spfa 解题报告

来源:互联网 发布:带着淘宝穿异界txt 编辑:程序博客网 时间:2024/05/19 05:03

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 21 2 52 1 41 2 2

Sample Output

14

题意:给一些边,最后问你从s点到t点的第k短路径的长度。

思路:A*中,f = g + h,g为到目前的花费,h为该点到目的点的估值,这里可以用spfa先计算从终点到之前点的最短路,作为h的值。

代码:

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <cstring>#include <queue>#define MAXN 1010#define MAXM 101000#define INF 0x3f3f3f3fusing 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 n, m, s, t, k, e, p, q;int vis[MAXN], dis[MAXN], head[MAXN], revhead[MAXN];void insert(int x, int y, int w){    edge[e].v = y;    edge[e].w = w;    edge[e].next = head[x];    head[x] = e;    revedge[e].v = x;    revedge[e].w = w;    revedge[e].next =revhead[y];    revhead[y] = e++;}void spfa(int src){    memset(vis, 0, sizeof(vis));    memset(dis, 0x3f, sizeof(dis));    vis[src] = 1;    dis[src] = 0;    queue<int> Q;    Q.push(src);    while(!Q.empty())    {        p = Q.front();        Q.pop();        vis[p] = 0;        for(int i = revhead[p]; i != -1; i = revedge[i].next)        {            int v = revedge[i].v;            int w = revedge[i].w;            if(dis[v] > dis[p] + w)            {                dis[v] = dis[p] + w;                if(!vis[v])                {                    vis[v] = 1;                    Q.push(v);                }            }        }    }}int Astar(int src, int des){    int cnt = 0;    priority_queue<A>Q;    if(src == des) k++;    if(dis[src] == INF) return -1;    A ta, tb;    ta.v = src, ta.g = 0, ta.f = ta.g + dis[src];    Q.push(ta);    while(!Q.empty())    {        ta = Q.top();        Q.pop();        if(ta.v == des)        {            cnt ++;            if(cnt == k) return ta.g;        }        for(int i = head[ta.v]; i != -1; i = edge[i].next)        {            tb.v = edge[i].v;            tb.g = ta.g + edge[i].w;            tb.f = tb.g + dis[tb.v];            Q.push(tb);        }    }    return -1;}int main(){    int a, b, c;    while(scanf("%d%d", &n, &m) != EOF)    {        e = 0;        memset(head, -1, sizeof(head));        memset(revhead, -1, sizeof(revhead));        for(int i = 0; i < m; i++)        {            scanf("%d%d%d", &a, &b, &c);            insert(a, b, c);        }        scanf("%d%d%d", &s, &t, &k);        spfa(t);        printf("%d\n", Astar(s, t));    }    return 0;}


0 0
原创粉丝点击