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. Delphi7中ClientDataSet的排序

    http://eteda.iteye.com/blog/1141312 Delphi7中ClientDataSet的排序 博客分类: Delphi   1.控件ClientDataSet的属性Inde ...

  2. 如何快速查找到多个字典中的公共键(Key)---Python数据结构与算法相关问题与解决技巧

    如何快速查找到多个字典中的公共键(Key)-?   实际案例: 西班牙足球甲级联赛,每轮球员进球统计: 第1轮: { '苏亚雷斯':1,'梅西':2,'本泽马':1,...} 第2轮: { '苏亚雷斯 ...

  3. Jmeter之Json表达式关联

    Jmeter使用中,通常用的最多的是正则表达式和Xpath表达式,但是现在大多数网站都用的Json返回数据,而且数据还特长的那种,作为合格的测试人员也要适应技术潮流发展,下面介绍利用Json Extr ...

  4. linux下mysql 5.7编写存储过程一直报错说Mysql server version for the right syntax

    首先看下可以正确执行的. 再来看保存时提示出错的 我唯一的区别就是在传参的类型那里有了变化,然而,报错如下 难道是我的类型不支持了吗,最后在一个无意识操作下,直接在类型里面限定长度. 可以运行啦.经过 ...

  5. python+selenium链接对象操作

    对于链接对象常见的操作有:单击.获取链接文字.获取链接地址等: from selenium import webdriverfrom time import sleep driver = webdri ...

  6. JavaScript 开发的 睡眠状况自测(SRSS)

    Javascript 开发睡眠状况自测程序,手记!2019.11.13日... <script>//初始化fbox = new Findpair('fbox','output');fbox ...

  7. CodeForces.1174D.ArraySplitting.(后缀和数组 + 贪心)

    题目链接 参考代码: #include <cstdio> #include <algorithm> using namespace std; typedef long long ...

  8. Codeforces 475D 题解(二分查找+ST表)

    题面: 传送门:http://codeforces.com/problemset/problem/475/D Given a sequence of integers a1, -, an and q ...

  9. 小白学Python(18)——pyecharts 关系图 Graph

    Graph-基本示例 import json import os from pyecharts import options as opts from pyecharts.charts import ...

  10. js中return、return false 、return true各自代表什么含义

    return语句代表需要返回一个值,如果不需要就不需要使用return语句.都类似一个出口,return 可以结束方法体中 return后面部分代码的执行.return false 或者 return ...