poj 2449 Remmarguts' Date(K短路 Spfa+A*)

时间:2022-09-19 17:53:45
Remmarguts' Date
Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 15717   Accepted: 4293

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

题目:http://poj.org/problem?id=2449

题意:求给定起点到终点的第K短的路

分析:裸的K短路题,表示我不会啊= =,然后学了下,挺简单的,就是最短路计算每个点到终点的最短路,作为估价函数,然后用A*搜索,从起点开始搜,每次都优先搜索最短的路。。。。知道终点出队列K次就是k短路了

PS:这题会出现起点和终点一样,要将K+1= =

代码:

#include<cstdio>
#include<cstring>
#include<queue>
#include<iostream>
using namespace std;
const int mm=111111;
const int mn=1111;
const int oo=1e8;
struct node
{
    int d,f,v;
    node(int a,int b,int c):d(a),f(b),v(c){}
    bool operator < (const node&a)const
    {
        return a.d<d;
    }
};
int ver[mm],cost[mm],next[mm],v[mm];
int head[mn],dis[mn],vis[mn],q[mm];
int i,n,m,s,t,k;
void Spfa(int s,int t)
{
    int i,u,v,l,r=0,tmp;
    for(i=0;i<=n;++i)dis[i]=oo,vis[i]=0;
    dis[q[r++]=s]=0;
    for(l=0;l<r;++l)
        for(i=head[u=q[l]],vis[u]=0;i>=0;i=next[i])
            if((tmp=dis[u]+cost[i])<dis[v=ver[i]])
            {
                dis[v]=tmp;
                if(vis[v])continue;
                vis[q[r++]=v]=1;
            }
}
int Astar(int s,int t)
{
    if(dis[s]>=oo)return -1;
    priority_queue<node>q;
    int i;
    for(i=0;i<=n;++i)vis[i]=0;
    q.push(node(dis[s],0,s));
    while(!q.empty())
    {
        node u=q.top();
        q.pop();
        ++vis[u.v];
        if(vis[u.v]>k)continue;
        if(vis[t]==k)return u.d;
        for(i=head[u.v];i>=0;i=next[i])
        {
            node now(0,0,0);
            now.f=u.f+cost[i];
            now.d=now.f+dis[now.v=ver[i]];
            q.push(now);
        }
    }
    return -1;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        memset(head,-1,sizeof(head));
        for(i=0;i<m;++i)
        {
            scanf("%d%d%d",&ver[i],&v[i],&cost[i]);
            next[i]=head[v[i]],head[v[i]]=i;
        }
        scanf("%d%d%d",&s,&t,&k);
        if(s==t)++k;
        Spfa(t,s);
        memset(head,-1,sizeof(head));
        for(i=0;i<m;++i)
            next[i]=head[ver[i]],head[ver[i]]=i,ver[i]=v[i];
        printf("%d\n",Astar(s,t));
    }
    return 0;
}