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

Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 30754   Accepted: 8394

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
 
K短路模板
 #include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue> using namespace std; const int INF(0x3f3f3f3f);
const int M();
const int N();
int n,m,s,t,k;
int sumedge,had[N],hed[N];
struct Edge
{
int v,next,w;
}edge1[M<<],edge2[M<<];
inline void ins(int u,int v,int w)
{
edge1[++sumedge].v=v;
edge1[sumedge].next=hed[u];
edge1[sumedge].w=w;
hed[u]=sumedge;
edge2[sumedge].v=u;
edge2[sumedge].next=had[v];
edge2[sumedge].w=w;
had[v]=sumedge; } int dis[N];
bool inq[N];
void SPFA(int s)
{
for(int i=;i<=n;i++) dis[i]=INF;
queue<int>que; que.push(s);
inq[s]=true; dis[s]=;
for(;!que.empty();)
{
int u=que.front();
que.pop(); inq[u]=;
for(int v,i=had[u];i;i=edge2[i].next)
{
v=edge2[i].v;
if(dis[v]>dis[u]+edge2[i].w)
{
dis[v]=dis[u]+edge2[i].w;
if(!inq[v]) que.push(v),inq[v]=;
}
}
}
} struct Node
{
int g,f,to;
bool operator < (const Node x) const
{
if(f==x.f) return g>x.g;
return f>x.f;
}
};
int Astar()
{
priority_queue<Node>que;
if(dis[s]==INF) return -;
int cnt=; Node now,v;
now.f=dis[s];now.g=;now.to=s;
que.push(now);
for(;!que.empty();)
{
now=que.top(); que.pop();
if(now.to==t) cnt++;
if(cnt==k) return now.g;
for(int i=hed[now.to];i;i=edge1[i].next)
{
v.to=edge1[i].v;
v.g=now.g+edge1[i].w;
v.f=v.g+dis[edge1[i].v];
que.push(v);
}
}
return -;
} int main()
{
scanf("%d%d",&n,&m);
for(int u,v,w;m--;)
{
scanf("%d%d%d",&u,&v,&w);
ins(u,v,w);
}
scanf("%d%d%d",&s,&t,&k);
if(s==t) k++; SPFA(t);
printf("%d\n",Astar());
return ;
}

POJ——T 2449 Remmarguts' Date的更多相关文章

  1. 【POJ】2449 Remmarguts' Date(k短路)

    http://poj.org/problem?id=2449 不会.. 百度学习.. 恩. k短路不难理解的. 结合了a_star的思想.每动一次进行一次估价,然后找最小的(此时的最短路)然后累计到k ...

  2. 【POJ】2449.Remmarguts' Date(K短路 n log n + k log k + m算法,非A*,论文算法)

    题解 (搬运一个原来博客的论文题) 抱着板题的心情去,结果有大坑 就是S == T的时候也一定要走,++K 我发现按照论文写得\(O(n \log n + m + k \ log k)\)算法没有玄学 ...

  3. poj 2449 Remmarguts' Date(第K短路问题 Dijkstra+A*)

    http://poj.org/problem?id=2449 Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Subm ...

  4. poj 2449 Remmarguts' Date (k短路模板)

    Remmarguts' Date http://poj.org/problem?id=2449 Time Limit: 4000MS   Memory Limit: 65536K Total Subm ...

  5. 图论(A*算法,K短路) :POJ 2449 Remmarguts' Date

    Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 25216   Accepted: 6882 ...

  6. poj 2449 Remmarguts' Date 第k短路 (最短路变形)

    Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 33606   Accepted: 9116 ...

  7. POJ 2449 Remmarguts' Date (第k短路径)

    Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions:35025   Accepted: 9467 ...

  8. poj 2449 Remmarguts' Date K短路+A*

    题目链接:http://poj.org/problem?id=2449 "Good man never makes girls wait or breaks an appointment!& ...

  9. POJ 2449 - Remmarguts' Date - [第k短路模板题][优先队列BFS]

    题目链接:http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Description "Good m ...

随机推荐

  1. Thread-local storage

    Thread-local storage (TLS) is a computer programming method that uses static or global memory local ...

  2. ListNode的python 实现

    class Node(object): def __init__(self): self.val = None self.next = None class Node_handle(): def __ ...

  3. hadoop从wordCount开始

    最近一段时间大数据很火,我有稍微有点java基础,自然选择了由java编写的hadoop框架,wordCount是hadoop中类似于java中helloWorld的存在,自然不能错过. packag ...

  4. ActiveMQ:JMS开源框架入门介绍

    介绍基本的JMS概念与开源的JMS框架ActiveMQ应用,内容涵盖一下几点: 基本的JMS概念 JMS的消息模式 介绍ActiveMQ 一个基于ActiveMQ的JMS例子程序 一:JMS基本概念 ...

  5. caioj 1155 同余方程组(模版)

    第一步,和同余方程一样,转化一下 两式相减得 这就转化为了求不定方程,用exgcd 求出x,要化成最小正整数解,避免溢出 然后可以求出P出来. 这个时候要把前两个式子转化成一个式子 设求出来的是P' ...

  6. 洛谷P5087 数学

    DP. 设f[i][j]为前j个数中选i个数的所有组合的分数之和 决策: 不选这个数,得分为f[i][j - 1] 选这个数,得分为f[i - 1][j - 1] * a[j] 可以得到状态转移方程为 ...

  7. LaTeX 写算法伪码

    本系列文章由 @YhL_Leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50054953 LaTeX写算法伪码,需 ...

  8. solr启动时报错org.apache.solr.common.SolrException: undefined field text的解决办法

    solr启动时报错org.apache.solr.common.SolrException: undefined field text的解决办法 原创 2015年08月21日 20:47:40 标签: ...

  9. Hololens官方教程精简版 - 08. Sharing holograms(共享全息影像)

    前言 注意:本文已更新到5.5.1f1版本号 本篇集中学习全息影像"共享"的功能,以实如今同一房间的人,看到"同一个物体".之所以打引號,是由于.每一个人看到的 ...

  10. hadoop-03-安装java

    hadoop-03-安装java 1,su root 2, rpm -qa|grep jdk #查看已经安装的jdk 3,rpm -e --nodeps `rpm -qa|grep jdk ` #删除 ...