POJ 2449 Remmarguts' Date K短路

本文介绍了一道关于寻找第K短路径的问题,并通过SPFA预处理最短路径及A*算法求解具体问题。文章详细解释了算法流程与核心代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Remmarguts' Date
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 21013 Accepted: 5717

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

[Submit]   [Go Back]   [Status]   [Discuss]

Home Page   Go Back  To top


spfa+A*算法模板题


#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>

using namespace std;

#define PB push_back
#define MP make_pair
#define CLR(vis) memset(vis,0,sizeof(vis))
#define MST(vis,pos) memset(vis,pos,sizeof(vis))
#define MAX3(a,b,c) max(a,max(b,c))
#define MAX4(a,b,c,d) max(max(a,b),max(c,d))
#define MIN3(a,b,c) min(a,min(b,c))
#define MIN4(a,b,c,d) min(min(a,b),min(c,d))
#define PI acos(-1.0)
#define INF 1000000000
#define LINF 1000000000000000000LL
#define eps 1e-8

typedef long long ll;
typedef unsigned long long ull;

const int N=1000+10;
const int M=100000+10;

int n,m;
int s,t,k;
int dis[N],vis[N];

vector< pair<int,int> > e[M];
vector< pair<int,int> > re[M];

struct edge
{
    int v;
    int w;
    friend bool operator<(edge a,edge b)
    {
        return (a.w+dis[a.v])>(b.w+dis[b.v]);
    }
};

void init()
{
    for(int i=0;i<N;i++)
        e[i].clear(),re[i].clear();
}

void spfa(int s)
{
    CLR(vis);
    for(int i=0;i<N;i++)
        dis[i]=INF;
    dis[s]=0;
    vis[s]=1;
    queue<int> q;
    q.push(s);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=0;
        for(int i=0;i<re[u].size();i++)
        {
            int v=re[u][i].first,w=re[u][i].second;
            if(dis[v]>dis[u]+w)
            {
                dis[v]=dis[u]+w;
                if(!vis[v])
                {
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
}

int a_star()
{
    if(s==t)
        k++;
    if(dis[s]==INF)
        return -1;
    int v,w,cnt[N];
    edge n1,n2;
    priority_queue<edge> q;
    CLR(cnt);
    n1.v=s;
    n1.w=0;
    q.push(n1);
    while(!q.empty())
    {
        n2=q.top();
        q.pop();
        v=n2.v;
        w=n2.w;
        cnt[v]++;
        if(cnt[t]==k)
            return w;
        if(cnt[v]>k)
            continue;
        for(int i=0;i<e[v].size();i++)
        {
            n1.v=e[v][i].first;
            n1.w=w+e[v][i].second;
            q.push(n1);
        }
    }
    return -1;
}


int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        init();
        int u,v,w;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            e[u].PB( MP(v,w) );
            re[v].PB( MP(u,w));
        }
        scanf("%d%d%d",&s,&t,&k);
        spfa(t);
        int ans=a_star();
        printf("%d\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值