POJ 2449 Remmarguts' Date(K短路A*算法)

版权声明:随意转载,转载请注明出处。 https://blog.csdn.net/qq_36258516/article/details/81937643

POJ 2449 Remmarguts’ Date(K短路A*)

Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 35806 Accepted: 9704

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

题意

  n个点,m条有向边,最后询问从s到t的第k短的路。

解题思路

  k短路问题,最有效的就是A* 算法。A* 算法最重要的是估值函数,在k短路问题中,显然可以知道,估值函数就是当前走过的路+剩余的路。剩余的路,我们可以用dijkstra反向跑图,就可以得到每个点距离终点的距离;走过的路我们可以用BFS。最后利用优先队列,将估值函数小的值排到前面。最后走到终点的时候,第k短的路是第k次到达终点的路。

代码

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<string.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxm = 1e3+50;

int n,m,vis[maxm],d[maxm],s,t,k;
struct node
{
    int x,val;
    node() {}
    node(int a,int b):x(a),val(b) {}
    friend bool operator < (node a,node b)
    {
        return a.val>b.val;
    }
};
vector<node> v[maxm],_v[maxm];

void init()
{
    for(int i=0; i<maxm; i++)
    {
        v[i].clear();
        _v[i].clear();
    }
}
void dijkstra(int t)
{
    memset(d,inf,sizeof d);
    memset(vis,0,sizeof vis);
    d[t]=0;
    priority_queue<node> q;
    while(!q.empty()) q.pop();
    q.push(node(t,0));
    while(!q.empty())
    {
        node x=q.top();
        q.pop();
        if(vis[x.x]) continue;
        vis[x.x]=1;
        int len=_v[x.x].size();
        for(int i=0; i<len; i++)
        {
            node y=_v[x.x][i];
            if(!vis[y.x]&&d[y.x]>x.val+y.val)
            {
                d[y.x]=x.val+y.val;
                q.push(node(y.x,d[y.x]));
            }
        }
    }
}

struct road
{
    int x,h,g;
    road() {}
    road(int a,int b,int c):x(a),h(b),g(c) {}
    friend bool operator <(road a,road b)
    {
        return a.h+a.g>b.h+b.g;
    }
};
void Astar()
{
    priority_queue<road> q;
    while(!q.empty()) q.pop();
    q.push(road(s,0,d[s]));
    int cnt=0;
    while(!q.empty())
    {
        road now=q.top();
        q.pop();
        if(now.x==t) cnt++;
        if(cnt>=k)
        {
            printf("%d\n",now.h);
            return;
        }
        int len=v[now.x].size();
        for(int i=0; i<len; i++)
        {
            node next=v[now.x][i];
            q.push(road(next.x,now.h+next.val,d[next.x]));
        }
    }
    printf("-1\n");
}
int main()
{
#ifdef DEBUG
    freopen("in.txt","r",stdin);
#endif // DEBUG
    while(~scanf("%d%d",&n,&m))
    {
        init();
        int x,y,z;
        for(int i=0; i<m; i++)
        {
            scanf("%d%d%d",&x,&y,&z);
            v[x].push_back(node(y,z));
            _v[y].push_back(node(x,z));
        }
        scanf("%d%d%d",&s,&t,&k);
        dijkstra(t);
        if(s==t) k++;
        Astar();
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36258516/article/details/81937643
今日推荐