POJ2499:Remmarguts' Date(第K短路 SPFA+A*)

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,要求s到t的第k短路是多少
 
首先这肯定是一个最短路的问题,我们可以用SPFA算出t到其他所有点的最短路径,然后使用A*去迭代搜索,一开始,把s到t的最短路先压入队列中,那么就变成了以t为起点去遍历,在第k次回到t点的时候所走过的路径最短的值便是第k短路径
 
#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std;
const int L = 100005;
const int inf = 1<<30;
struct node
{
    int now,g,f;
    bool operator <(const node a)const
    {
        if(a.f == f) return a.g < g;
        return a.f < f;
    }
};
struct edges
{
    int x,y,w,next;
} e[L<<2],re[L<<2];//e是输入给出的定向图,re为其逆向图,用于求t到其他所有点的最短路径

int head[1005],dis[1005],vis[1005],n,m,k,s,t,rehead[1005];

void Init()//初始化
{
    memset(e,-1,sizeof(e));
    memset(re,-1,sizeof(re));
    for(int i = 0; i<=n; i++)
    {
        dis[i] = inf;
        vis[i] = 0;
        head[i] = -1;
        rehead[i] = -1;
    }
}

void AddEdges(int x,int y,int w,int k)
{
    e[k].x = x,e[k].y = y,e[k].w = w,e[k].next = head[x],head[x] = k;
    re[k].x = y,re[k].y = x,re[k].w = w,re[k].next = rehead[y],rehead[y] = k;
}

int relax(int u,int v,int c)
{
    if(dis[v]>dis[u]+c)
    {
        dis[v] = dis[u]+c;
        return 1;
    }
    return 0;
}

void SPFA(int src)
{
    int i;
    dis[src] = 0;
    queue<int> Q;
    Q.push(src);
    while(!Q.empty())
    {
        int u,v;
        u = Q.front();
        Q.pop();
        vis[u] = 0;
        for(i = rehead[u]; i!=-1; i = re[i].next)
        {
            v = re[i].y;
            if(relax(u,v,re[i].w) && !vis[v])
            {
                Q.push(v);
                vis[v] = 1;
            }
        }
    }
}

int Astar(int src,int to)
{
    priority_queue<node> Q;
    int i,cnt = 0;
    if(src == to) k++;//在起点与终点是同一点的情况下,k要+1
    if(dis[src] == inf) return -1;
    node a,next;
    a.now = src;
    a.g = 0;
    a.f = dis[src];
    Q.push(a);
    while(!Q.empty())
    {
        a = Q.top();
        Q.pop();
        if(a.now == to)
        {
            cnt++;
            if(cnt == k)
                return a.g;
        }
        for(i = head[a.now]; i!=-1; i = e[i].next)
        {
            next = a;
            next.now = e[i].y;
            next.g = a.g+e[i].w;
            next.f = next.g+dis[next.now];
            Q.push(next);

        }
    }
    return -1;
}

int main()
{
    int i,j,x,y,w;
    while(~scanf("%d%d",&n,&m))
    {
        Init();
        for(i = 0; i<m; i++)
        {
            scanf("%d%d%d",&x,&y,&w);
            AddEdges(x,y,w,i);
        }
        scanf("%d%d%d",&s,&t,&k);
        SPFA(t);
        printf("%d\n",Astar(s,t));
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值