Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions:32863   Accepted: 8953

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短路模板
注意点:读题不认真,忽略了题目中无解输出“-1”,解果一直WA
板子不会多打几遍。
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<cmath>
#define ll long long
#define inf 1047483600
#define mod 317847191
using namespace std;
inline int read()
{
int x=,w=;char ch=getchar();
while(!isdigit(ch)){if(ch=='-') w=-;ch=getchar();}
while(isdigit(ch)) x=(x<<)+(x<<)+(ch-''),ch=getchar();
return x*w;
}
const int N=;
struct node{
int u,v,c,ne;
}e[N],e2[N];
int h[N],h2[N],tot,n,m;
void add(int u,int v,int c)
{
tot++;e[tot]=(node){u,v,c,h[u]};h[u]=tot;
e2[tot]=(node){v,u,c,h2[v]};h2[v]=tot;
}
int S,T,K;
int d[N];
bool v[N];
struct kk{
int id,f,g;
bool operator<(const kk&x)const{
if(f!=x.f) return f>x.f;
else return g>x.g;
}
};
priority_queue<kk>q;
void spfa(int s)
{
queue<int>q;
for(int i=;i<=n;++i) d[i]=inf;
d[s]=;v[s]=;q.push(s);
while(!q.empty())
{
int ff=q.front();q.pop();v[ff]=;
for(int i=h2[ff];i;i=e2[i].ne)
{
int rr=e2[i].v;
if(d[rr]>d[ff]+e2[i].c)
{
d[rr]=d[ff]+e2[i].c;
if(!v[rr]) v[rr]=,q.push(rr);
}
}
}
}
void A_star(int S,int T,int K)
{
if(S==T) K++;
int cnt=;
q.push((kk){S,,});
while(!q.empty())
{
kk ff=q.top();q.pop();
if(ff.id==T)
{
cnt++;
if(cnt==K){printf("%d",ff.f);exit();}
}
for(int i=h[ff.id];i;i=e[i].ne)
{
kk rr;rr.id=e[i].v;
rr.g=ff.g+e[i].c;
rr.f=rr.g+d[rr.id];
q.push(rr);
}
}
cout<<"-1";
}
int main()
{
n=read();m=read();
for(int i=;i<=m;++i)
{
int x,y,z;x=read();y=read();z=read();
add(x,y,z);
}
S=read();T=read();K=read();
spfa(T);
A_star(S,T,K);
return ;
}

败后或反成功,顾拂心处莫便放手。

K短路模板POJ 2449 Remmarguts' Date的更多相关文章

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

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

  2. A*算法求K短路模板 POJ 2449

    #include<cstdio> #include<queue> #include<cstring> using namespace std; const int ...

  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. POJ 2449 - Remmarguts' Date - [第k短路模板题][优先队列BFS]

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

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

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

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

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

  8. poj 2449 Remmarguts' Date(K短路,A*算法)

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u013081425/article/details/26729375 http://poj.org/ ...

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

    题目链接 Description "Good man never makes girls wait or breaks an appointment!" said the mand ...

随机推荐

  1. windows10下安装docker报错:error during connect

    详细报错信息如下: C:\Users\zig>docker info error during connect: Get http://%2F%2F.%2Fpipe%2Fdocker_engin ...

  2. 44 答疑(三)--join的写法/Simple nested loop join的性能问题/Distinct和group by的性能/备库自增主键问题

    44 答疑(三) Join的写法 35节介绍了join执行顺序,加了straight_join,两个问题: --1 如果用left join,左边的表一定是驱动表吗 --2 如果两个表的join包含多 ...

  3. Visual Studio关于项目迁移或拉取代码产生的dll黄色感叹号警告问题解决方案

    今天换了台大电脑,准备好好爽一下, 就把笔记本上的项目拷贝到了台式机上, 但是我没有拷贝解决方案整个文件夹,因为其中项目太多了,我就把其中一个项目的文件夹直接拷贝到电脑上,然后就出现了下面的情况. 这 ...

  4. python 正则表达式 re.match

    #coding:utf-8 import re #匹配内容:单词+空格+单词+任意字符 #\w 单词字符[A-Za-z0-9_] #(?P<name>...) 分组,除了原有的编号外在指定 ...

  5. 【MM系列】SAP MM模块-移动类型全部列表

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP MM模块-移动类型全部列表 ...

  6. Angular5 错误: ngModel cannot be used to register form controls with a parent formGroup directive

    在创建一个表单时,出现了这样的错误: 原因是,在最外层的form中使用了 formGroup 指令,但在下面的某个input 元素中,使用了ngModel 指令,但没有加入formControl 指令 ...

  7. 转载-linux挂载的意思

    挂载:Liunx采用树形的文件管理系统,也就是在Linux系统中,可以说已经没有分区的概念了.分区在Linux和其他设备一样都只是一个文件.要使用一个分区必须把它加载到文件系统中.这可能难于理解,继续 ...

  8. java高级开发面试总结

    Java高级工程师面试题总结及参考答案 (转载)博客原文链接:https://www.cnblogs.com/java1024/p/8594784.html 一.面试题基础总结 1. JVM结构原理. ...

  9. Leading and Trailing LightOJ - 1282 题解

    LightOJ - 1282 Leading and Trailing 题解 纵有疾风起 题目大意 题意:给你一个数n,让你求这个数的k次方的前三位和最后三位. \(2<=n<2^{31} ...

  10. IE, Chrome和Firefox浏览器 差异对比

    最近的项目中使用Extjs5.6, 其中主要的一个特点就是js文件的动态加载,之前使用Firefox浏览器对js文件进行调试,打断点时,只对当次调试有效,刷新之后,由于动态加载的js文件(文件名后面加 ...