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. VMware 虚拟化编程(8) — 多线程中的 VixDiskLib

    目录 目录 前文列表 多线程注意事项 多线程中的 VixDiskLib 前文列表 VMware 虚拟化编程(1) - VMDK/VDDK/VixDiskLib/VADP 概念简析 VMware 虚拟化 ...

  2. 《计算机程式设计》Week4 课堂笔记

    本笔记记录自 Coursera课程 <计算机程式设计> 台湾大学 刘邦锋老师 Week4 Functions 4-1 System Function 函数主要分为两大类系统定义函数与使用者 ...

  3. layui框架中layer父子页面交互的方法分析

    本文实例讲述了layui框架中layer父子页面交互的方法.分享给大家供大家参考,具体如下: layer是一款近年来备受青睐的web弹层组件,官网地址是:http://layer.layui.com/ ...

  4. Token 认证

    Token 认证 From今日头条:https://www.toutiao.com/i6516654967204348430/?tt_from=weixin&utm_campaign=clie ...

  5. oracle--表分区、分区索引

    --|/ range分区 create table sale( product_id varchar2(5), sale_count number(10,2) ) partition by range ...

  6. synchronized的底层实现原理

    转自:http://www.cnblogs.com/paddix/p/5367116.html 如果对上面的执行结果还有疑问,也先不用急,我们先来了解Synchronized的原理,再回头上面的问题就 ...

  7. RESUful风格

    1.7 RESTful风格 1.7.1 RESTful风格介绍 RESTful是一种软件架构风格! RESTful架构风格规定,数据的元操作,即CRUD(create, read, update和de ...

  8. [Codeforces 1199C]MP3(离散化+二分答案)

    [Codeforces 1199C]MP3(离散化+二分答案) 题面 给出一个长度为n的序列\(a_i\)和常数I,定义一次操作[l,r]可以把序列中<l的数全部变成l,>r的数全部变成r ...

  9. BZOJ4753: [Jsoi2016]最佳团体(分数规划+树上背包)

    BZOJ4753: [Jsoi2016]最佳团体(分数规划+树上背包) 标签:题解 阅读体验 BZOJ题目链接 洛谷题目链接 具体实现 看到分数和最值,考虑分数规划 我们要求的是一个\(\dfrac{ ...

  10. day16 django 笔记

    一 jQuery是什么? [1]   jQuery由美国人John Resig创建,至今已吸引了来自世界各地的众多 javascript高手加入其team. [2]   jQuery是继prototy ...