码迷,mamicode.com
首页 > 其他好文 > 详细

poj2449--Remmarguts' Date------第k短路

时间:2018-08-13 15:52:17      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:lov   nod   dom   math.h   ==   point   一点   ffffff   top   

第k短路:

K短路的定义:假设从1出发,有M条长度不同的路径可以到达点N,则K短路就是这M条路径中第K小的路径长度。 
以上所述,设f[n]为最终所求,则f(n)=g(n)+h(n);h(n)就是我们所说的‘启发式函数’,表示为重点t到其余一点p的路径长度,g(n)表示g当前从s到p所走的路径的长度。 

估价函数=当前值+当前位置到终点的距离

Solution:

(1)将有向图的所有边反向,以原终点t为源点,求解t到所有点的最短距离; 
(2)新建一个优先队列,将源点s加入到队列中; 
(3)从优先级队列中弹出f(p)最小的点p,如果点p就是t,则计算t出队的次数; 
如果当前为t的第k次出队,则当前路径的长度就是s到t的第k短路的长度,算法结束; 
否则遍历与p相连的所有的边,将扩展出的到p的邻接点信息加入到优先级队列;

 

 

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


/*
    data:2018.8.13
    author:tonygsw
    account:zj1228
    link:http://poj.org/problem?id=2449
    A*第k短路 
*/
#define ll long long
#define IO ios::sync_with_stdio(false);

#include<map>
#include<queue>
#include<vector>
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;

#define maxn 100010

int n,m,s,t,k;
class Edge{
    public:
        int to,weight;
        bool operator <(const Edge &a) const
        {
            return a.weight<weight;
        }
};
class Node{
    public:
        int to,g,h;//h表示预测距离
        bool operator <(const Node &a)const
        {
            if(a.g+a.h==g+h)return a.g<g;
            return a.g+a.h<g+h;
        }    
};
vector<Edge> edge1[maxn],edge2[maxn];
int mindis[maxn];bool minvis[maxn];
void init()
{
    for(int i=0;i<maxn;i++)
        edge1[i].clear(),edge2[i].clear();
    memset(minvis,0,sizeof(minvis));
}

void dijkst(int beginn)
{
    for(int i=0;i<=n;i++)
        mindis[i]=0x3fffffff;
    Edge beg,nex;
    beg.to=beginn,beg.weight=0;
    priority_queue<Edge> way;
    way.push(beg);
    while(!way.empty())
    {
        beg=way.top();
        way.pop();
        mindis[beg.to]=min(mindis[beg.to],beg.weight);
        minvis[beg.to]=1;
        for(int i=0;i<edge2[beg.to].size();i++)
        {
            nex.to=edge2[beg.to][i].to;
            if(minvis[nex.to])continue;
            nex.weight=beg.weight+edge2[beg.to][i].weight;
            way.push(nex);
        }
    }
    return;
}
int a_star(int beginn,int endd,int k)
{
    Node beg,nex;
    beg.to=beginn,beg.g=0,beg.h=mindis[beginn];
    priority_queue<Node> way;
    way.push(beg);
     if(s==t) k++;
    if(mindis[beginn]==0x3fffffff)return -1;
    while(!way.empty())
    {
        beg=way.top();
        way.pop();
        if(beg.to==endd)
        {
            k--;
            if(k==0)return beg.g;
        }
        for(int i=0;i<edge1[beg.to].size();i++)
        {
            nex.to=edge1[beg.to][i].to;
            nex.g=beg.g+edge1[beg.to][i].weight;
            nex.h=mindis[nex.to];
            way.push(nex);
        }
    }
    return -1;
}
int main()
{
    Edge tem1,tem2;
    while(~scanf("%d%d",&n,&m))
    {
        init();
        for(int i=0;i<m;i++)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            tem1.to=b,tem1.weight=c;
            tem2.to=a,tem2.weight=c;
            edge1[a].push_back(tem1);
            edge2[b].push_back(tem2);
        }
        scanf("%d%d%d",&s,&t,&k);
        dijkst(t);
        cout<<a_star(s,t,k)<<endl;    
    }
}
/*
3 4
1 2 3
1 3 4
2 3 3
3 1 2
1 2 3
*/

 

poj2449--Remmarguts' Date------第k短路

标签:lov   nod   dom   math.h   ==   point   一点   ffffff   top   

原文地址:https://www.cnblogs.com/fantastic123/p/9468304.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!