POJ2449:K短路

Remmarguts‘ Date

Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 26355   Accepted: 7170

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

思路:利用A*算法求最短路。第一次搜到终点为最短路,第二次搜到终点为次短路...第k次搜到终点为k短路。A*算法的估价函数f(x)=g(x)+h(x)。g(x):从出发点到当前节点的距离。h(x):从当前节点到终点的距离。可构建反向边利用spfa求得。
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int MAXN=1005;
const int INF=0x3f3f3f3f;
struct Edge{
    int to,w,next;
}es[100005],rves[100005];
int n,m;
int src,tml,kth;
int head[MAXN],tot;
int rhead[MAXN],rtot;
void addedge(int u,int v,int w)
{
    es[tot].to=v;
    es[tot].w=w;
    es[tot].next=head[u];
    head[u]=tot++;

    rves[rtot].to=u;
    rves[rtot].w=w;
    rves[rtot].next=rhead[v];
    rhead[v]=rtot++;
}
int d[MAXN],vis[MAXN];
void spfa(int s)//利用反向边确定估价函数h(x):i到tml的距离
{
    for(int i=1;i<=n;i++)
    {
        d[i]=INF;
        vis[i]=0;
    }
    queue<int> que;
    que.push(s);
    d[s]=0;
    vis[s]=1;
    while(!que.empty())
    {
        int u=que.front();que.pop();
        vis[u]=0;
        for(int i=rhead[u];i!=-1;i=rves[i].next)
        {
            Edge e=rves[i];
            if(d[e.to]>d[u]+e.w)
            {
                d[e.to]=d[u]+e.w;
                if(!vis[e.to])
                {
                    vis[e.to]=1;
                    que.push(e.to);
                }
            }
        }
    }
}
struct Node{
    int nod,g,h;
    Node(){}
    Node(int cnod,int cg,int ch):nod(cnod),g(cg),h(ch){}
    bool operator<(const Node &node) const
    {
        return g+h > node.g+node.h;
    }
};
int astar(int s)
{
    priority_queue<Node> que;
    que.push(Node(s,0,d[s]));//g(x):src到i的距离.h(x):i到tml的距离.
    while(!que.empty())
    {
        Node now=que.top();que.pop();
        int u=now.nod;
        if(u==tml)
        {
            kth--;
            if(kth==0)    return now.g+now.h;//第k条最短路
        }
        for(int i=head[u];i!=-1;i=es[i].next)
        {
            Edge e=es[i];
            int g=now.g+e.w;
            int h=d[e.to];
            que.push(Node(e.to,g,h));
        }
    }
    return -1;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(head,-1,sizeof(head));
        memset(rhead,-1,sizeof(rhead));
        tot=0;
        rtot=0;
        for(int i=0;i<m;i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            addedge(u,v,w);
        }
        scanf("%d%d%d",&src,&tml,&kth);
        if(src==tml)    kth++;//若src等于tml那么最短路为0,不算
        spfa(tml);
        printf("%d\n",astar(src));
    }

    return 0;
}
时间: 2024-10-03 22:20:54

POJ2449:K短路的相关文章

poj2449 k短路问题

这个题就是让你求出S点到T点的第K短路, 使用A*搜索就可以, 搜索使用两个指标函数 h g, h表示从源点到当前点的最短路, g点表示从当前点到汇点的最短路, 搜索的时候v顶点第k次出队时的h就是第k短路的长度, 代码如下: #include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <vector> using namespace st

poj2449 Remmarguts&#39; Date,第K短路

点击打开链接 SPFA  + A* #include <cstdio> #include <queue> #include <cstring> #include <algorithm> using namespace std; struct node { int v, dis, f, next; friend bool operator <(node a, node b){ return a.f>b.f; } }; const int INF =

POJ2449 Remmarguts&#39; Date 【k短路】

Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 21064   Accepted: 5736 Description "Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. Softly touching his little ducks' head, h

POJ2449 Remmarguts&#39; Date 第K短路

POJ2449 比较裸的K短路问题 K短路听起来高大上 实际思路并不复杂 首先对终点t到其他所有点求最短路 即为dist[] 然后由起点s 根据当前走过的距离+dist[]进行A*搜索 第k次到达t即为第K短路 代码也很简单 //数组开的不够 不一定是运行时错误! 可能也会WA #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath&g

[poj2449]Remmarguts&#39; Date(K短路模板题,A*算法)

解题关键:k短路模板题,A*算法解决. #include<cstdio> #include<cstring> #include<algorithm> #include<cstdlib> #include<iostream> #include<cmath> #include<queue> using namespace std; typedef long long ll; const int N=1e3+10; const

(WA)POJ2449 第K短路

POJ2449 第K短路 改了好长时间发现读入读反了qwq A*,先在反向图上求出每个点到t的最短距离,作为估价函数即可 疑问:能不能直接记录h+g 1 #include <cstdio> 2 #include <cstring> 3 #include <cctype> 4 #include <algorithm> 5 #include <queue> 6 #include <iostream> 7 using namespace s

poj2449:第k短路问题

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

poj2449 第k短路

题目链接 学习博客:https://blog.csdn.net/Z_Mendez/article/details/47057461 k短路没有我想象的那么难,还是很容易理解的 求s点到t点的第k短路径 先求出t到所有点的最短路径,用g[i]表示t到i的距离 从s开始"bfs",按照(g[i]+bfs路过的长度)构造优先队列,比如刚开始bfs路过长度为0,所在点为s 一直选择最小的(g[i]+bfs路过的长度),第一次到达t一定是从s沿着最短路径到达. 直到第k次到达t 理解代码可能更容

次短路 + 第K短路 模版

虽然从字面上看,次短路和第2短路是一样的.但是我在题目中遇到的却不是这样的. 在有些题目中,需要判断次短路是否存在.比如说,u.v之间只有一条路径.那么只有最短路.次短路是不存在的.这时候,解题方法是先求出最短路,然后枚举删除最短路径中的边,然后求最小值.题目可以看poj3986. 第K短路的实现是 SPFA + A* 算法. A*算法通过一个估价函数f(h)来估计途中的当前点p到终点的距离,并由此决定它的搜索方向,当这条路径失败时,它会尝试其他路径.对于A*,估价函数 = 当前值 + 当前位置