Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 30725   Accepted: 8389

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
 
 
丧心病狂的一道题!
丫的卡我的scanf!
k短路模版
做完这题大脑彻底短路了。
过程非常艰辛。。
从MLE RE 到 WA 的时候 我好激动啊。。

屠龙宝刀点击就送

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#define M 100005
#define N 1005
using namespace std;
struct node
{
int to,f,g;
bool operator<(node a)const
{
if(f==a.f) return g>a.g;
return f>a.f;
}
};
struct Edge
{
int next,to,val;
};
Edge edge1[M*],edge2[M*];
int n,m,dist[N],head1[N],head2[N],cnt;
bool vis[N];
void spfa(int s)
{
queue<int>Q;
for(int i=;i<=n;++i) dist[i]=M,vis[i]=;
dist[s]=;
Q.push(s);
vis[s]=;
while(!Q.empty())
{
int now=Q.front();Q.pop();
vis[now]=;
for(int i=head2[now];i;i=edge2[i].next)
{
int v=edge2[i].to;
if(dist[v]>dist[now]+edge2[i].val)
{
dist[v]=dist[now]+edge2[i].val;
if(!vis[v])
{
vis[v]=;
Q.push(v);
}
}
}
}
}
inline void ins(int u,int v,int w)
{
edge1[++cnt].next=head1[u];
edge1[cnt].to=v;
edge1[cnt].val=w;
head1[u]=cnt;
edge2[cnt].next=head2[v];
edge2[cnt].to=u;
edge2[cnt].val=w;
head2[v]=cnt;
}
int Astar(int s,int t,int k)
{
priority_queue<node>Q;
if(s==t) k++;
int cnt=;
if(dist[s]==M) return -;
node a,tmp;
a.to=s;
a.g=;
a.f=a.g+dist[a.to];
Q.push(a);
while(!Q.empty())
{
node now=Q.top();Q.pop();
if(now.to==t) cnt++;
if(cnt==k) return now.g;
for(int i=head1[now.to];i;i=edge1[i].next)
{
int v=edge1[i].to;
tmp.to=v;
tmp.g=now.g+edge1[i].val ;
tmp.f=tmp.g+dist[tmp.to];
Q.push(tmp);
}
}
return -;
}
inline void init()
{
cnt=;
memset(head1,,sizeof(head1));
memset(head2,,sizeof(head2));
}
int main()
{
for(int s,t,k;cin>>n>>m;)
{
init();
for(int x,y,z;m--;)
{
cin>>x>>y>>z;
ins(x,y,z);
}
cin>>s>>t>>k;
spfa(t);
int ans=Astar(s,t,k);
cout<<ans<<endl;
}
return ;
}

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

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

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

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

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

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

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

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

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

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

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

  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短路模板题][优先队列BFS]

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

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

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

  9. K短路模板POJ 2449 Remmarguts' Date

      Time Limit: 4000MS   Memory Limit: 65536K Total Submissions:32863   Accepted: 8953 Description &qu ...

随机推荐

  1. SyntaxError: can't assign to operator

    变量名不能有'-'

  2. c++拷贝函数详解(转)

    一. 什么是拷贝构造函数 首先对于普通类型的对象来说,它们之间的复制是很简单的,例如 int a = 100; int b = a; 而类对象与普通对象不同,类对象内部结构一般较为复杂,存在各种成员变 ...

  3. 利用YYLabel 进行图文混排+高度计算

    利用YYLabel 进行图文混排+高度计算 1.项目需求: 用一个控件显示图片和文字,并且依据图片和文字动态计算控件的高度. 2.方案: 利用YYLabel控件和属性字符串处理. 注:(在使用YYLa ...

  4. HDU1083 【匹配问题】

    题意: 有P门课,N个学生,给出每门课上的人. 然后问你能不能使得每门课有一个课代表 思路: 课和学生是两类,且同类之间没有关系,构成二分图:直接就是一个最大匹配问题: 注意点: 1.是给课进行匹配不 ...

  5. Ogre的mesh和skeleton文件数据格式分析

    转载自: http://www.cnblogs.com/topicofkevin/archive/2012/03/05/2380808.html 首先看一下skeleton文件,skeleton文件描 ...

  6. css 的继承性

    目录 css 的继承性是什么? 父元素的属性那些可以被子元素继承,哪些不能呢? css 的继承性是什么? 在面向对象语言都会存在继承的概念,在面向对象语言中,继承的特点:继承了父类的属性和方法. 那么 ...

  7. 15.split分割注意事项

    1.v = 'k1,v1- k2,v2- k3,v3-'变成一个字典{'k1':'v1','k2':'v2','k3:'v3'...} 变成一个字典 {'k1':'v1','k2':'v2','k3: ...

  8. 多次页面跳转后pop回主界面的问题

    最近写代码的时候出了点BUG, 查阅资料后终于解决了. 问题原因大概是: 项目中所有的viewController都是继承自一个封装好的viewController. navigationbar, n ...

  9. mysql项目实战经验

    一.项目的编码设置   目的:避免出现莫名其妙错误,笔者曾经就碰到因编码不对返回null而浪费大量时间:统一的编码可以减少解析的时间,提高效率 1.1修改my.ini文件 一般在C:\Program ...

  10. Linux权限相关

    权限分组 用户:文件所有者 用户组:多个用户的集合 其他:除了用户和用户组之外的任何用户 权限类别 r:表示读的权限 w:表示写的权限 x:表示执行的权限 s:表示setuid权限,允许用户以其拥有者 ...