POJ 2449 A* K短路

Remmarguts' Date
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 24825 Accepted: 6752

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


#include <iostream>
#include <cstdio>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#define esp 1e-6
#define inf 0x0f0f0f0f
#define LL long long  
using namespace std;





/************************************************

Desiner:hl
time:2015/11/12
Exe.Time:313ms 
Exe.Memory:19760KB 


裸的K短路模板题。。纯裸。。而且不太会有别的变式 
************************************************/




const int maxn=1001;
const int maxm=100001;
struct edge
{
    int from,to,next,w;
};
edge e1[maxm];
edge e2[maxm];
int head1[maxn];
int head2[maxn];
int dist[maxn];
bool vis[maxn];
struct node//存储搜索过程中点的信息
{
    int to;
    int g,f;//f=g+h
    bool operator <(const node &r) const
    {
        if(r.f==f) return r.g<g;
        else return r.f<f;
    }
};
int n,m,t1,t2,k,cnt,s,end;//第k短路
void add1(int i,int j,int w)
{
    e1[t1].from=i;
    e1[t1].to=j;
    e1[t1].w=w;
    e1[t1].next=head1[i];
    head1[i]=t1++;
}
void add2(int i,int j,int w)
{
    e2[t2].from=i;
    e2[t2].to=j;
    e2[t2].w=w;
    e2[t2].next=head2[i];
    head2[i]=t2++;
}
void spfa(int s)//求出路径当前值到终点的最短距离
{
    queue <int> q;
    q.push(s);
    for(int i=1;i<=n;i++) dist[i]=inf;
    memset(vis,false,sizeof(vis));
    dist[s]=0;    
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=false;//用vis来标记点是否在队列中
        for(int i=head2[u];i!=-1;i=e2[i].next)
        {
            int v=e2[i].to;
            if(dist[v]>dist[u]+e2[i].w)
            {
                dist[v]=dist[u]+e2[i].w;
                if(!vis[v])
                {
                    q.push(v);
                    vis[v]=true;
                }
            }
        }
    }
}
int A(int s,int end,int k)  //开始  结束 和第k短路 
{
    int cnt=0;
    node e,ne;
    priority_queue <node> que;    
    if(s==end) k++;//特判
    if(dist[s]==inf) return -1;
    e.to=s;    
    e.f=e.g+dist[e.to];//估价函数的公式
    que.push(e);
    while(!que.empty())
    {
        e=que.top();
        que.pop();
        if(e.to==end) cnt++;//统计终点出队的次数
        if(cnt==k) return e.g;//计算t出队的次数,如果t出对的次数=k,则当前的路径长度g记为第k短路
        for(int i=head1[e.to];i!=-1;i=e1[i].next)
        {
            ne.to=e1[i].to;
            ne.g=e.g+e1[i].w;
            ne.f=ne.g+dist[ne.to];
            que.push(ne);
        }
    }
    return -1;
}
int main()
{
    int u,v,w;
    while(scanf("%d%d",&n,&m)==2)
    {
        memset(head1,-1,sizeof(head1));
        memset(head2,-1,sizeof(head2));
        memset(e1,0,sizeof(e1));
        memset(e2,0,sizeof(e2));
        t1=t2=0;
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            add1(u,v,w);
            add2(v,u,w);
        }
        scanf("%d%d%d",&s,&end,&k);
        spfa(end);
        int ans=A(s,end,k);
        printf("%d\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值