poj 2449 Remmarguts' Date(第K短路)

本文介绍了一种结合最短路径算法与A*算法解决寻找从起点到终点的第K条最短路径问题的方法。该问题设定在一个包含多个站点的网络中,每两个站点间可能有带权的有向边相连,目标是找到连接特定起点和终点的第K条最短路径。
摘要由CSDN通过智能技术生成
Remmarguts' Date
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 33704 Accepted: 9145

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

思路:这题直接最短路加上A*算法就可以了。

代码:

#include<iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
using namespace std;
const int N=100000+10;
const int inf=0x3f3f3f3f;
struct edge
{
    int u,v,w,next;
}e[N<<1];
int head[N<<1],head1[N<<1],dis[N<<1],vis[N<<1],cnt[N<<1];
int num;
int n,m;
int s,t,k;
struct node
{
    int g,h;
    int to;
    bool operator<(node a)const
    {
        return a.h+a.g<h+g;
    }
};
void init()
{
    num=0;
    memset(head1,-1,sizeof(head1));
    memset(head,-1,sizeof(head));
}
void addegde(int u,int v,int w)
{
    e[num].v=v;
    e[num].w=w;
    e[num].next=head[u];
    head[u]=num++;
    e[num].v=u;
    e[num].w=w;
    e[num].next=head1[v];
    head1[v]=num++;
}
void spfa()
{
    memset(vis,0,sizeof(vis));
    memset(dis,inf,sizeof(dis));
    dis[t]=0;
    vis[t]=1;
    queue<int>q;
    q.push(t);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=0;
        for(int i=head1[u];i!=-1;i=e[i].next)
        {
            int v=e[i].v;
            if(dis[v]>dis[u]+e[i].w)
            {
                dis[v]=dis[u]+e[i].w;
                if(!vis[v])
                {
                    q.push(v);
                    vis[v]=1;
                }
            }
        }
    }
}
int AA()
{
    memset(cnt,0,sizeof(cnt));
    priority_queue<node>Q;
    node p,q;
    p.g=0;
    p.to=s;
    p.h=dis[s];
    Q.push(p);
    while(!Q.empty())
    {
        q=Q.top();
        Q.pop();
        cnt[q.to]++;
        if(cnt[q.to]>k)
            continue;
        if(cnt[t]==k)
            return q.g;
        for(int i=head[q.to];i!=-1;i=e[i].next)
        {
            int v=e[i].v;
            p.to=v;
            p.g=q.g+e[i].w;
            p.h=dis[v];
            Q.push(p);
        }
    }
    return -1;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        init();
        for(int i=0;i<m;i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            addegde(u,v,w);
        }
        scanf("%d%d%d",&s,&t,&k);
        spfa();
        if(s==t)k++;
        //cout<<dis[s]<<endl;
        int ans=AA();
        printf("%d\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值