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. 分页查询 pagecount recordcount pagesize

    pagecount=(recordcount+pagesize-1)/pagesize

  2. 双系统(win10+ubuntu)卸载Ubuntu系统

    之前装的双系统,Win10 和Ubuntu ,系统引导使用的是Ubuntu的Grup的引导, 直接删除Ubuntu会导致引导丢失,会很麻烦,win10直接会挂掉,后期恢复需要重建引导 安全删除思路,先 ...

  3. java配置详解

    JAVA_HOMED:\JavaTools\Java\jdk1.7.0_80\ D:\JavaEnvironment\Java\jdk1.7.0_71D:\JavaEnvironment\Java\j ...

  4. dom元素的自上而下自动滚动

    dom元素的自上而下自动滚动 <!doctype html> <html lang="en"> <head> <meta charset= ...

  5. 无法打开内核设备"\\.\Global\vmx86":系统找不到指定的文件. 是否在安装 VMwareWorksation 后重新引到 ? 问题解决

    节前正常使用的工作环境, 过完春节后, 上班第一天就不正常工作了, 难不成机器也要放假休息, 虚拟机打不开了, 没办法办公可是不行的. 上网查原因, 解决问题. 上网看了很多关于此问题的解决办法, 很 ...

  6. STS安装Drools

    download drools (including "Drools Engine" & "Drools and jBPM tools") from o ...

  7. linux内核的gpiolib详解

    #include <linux/init.h> // __init __exit #include <linux/module.h> // module_init module ...

  8. dfs找环

    http://acm.hdu.edu.cn/showproblem.php?pid=6736 Forest Program Time Limit: 2000/1000 MS (Java/Others) ...

  9. 搜索(DFS)---填充封闭区域

    填充封闭区域 130. Surrounded Regions (Medium) For example, X X X X X O O X X X O X X O X X After running y ...

  10. Windows 中下载 Android Q 源码

      1.  安装软件 1.1.  安装 git A.git官网下载:https://git-scm.com/downloads/ 安装git到如下路径 C:/Program Files/Git B.图 ...