Remmarguts’ Date

Time Limit: 4000MS Memory Limit: 65536K

Total Submissions: 33081 Accepted: 8993

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


#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const int maxn = 1e3+10;
vector <pair<int,int> > ve[maxn],ve2[maxn];
int n,m,s,e,k,dis[maxn];
bool vis[maxn]; struct NODE {
int to,g,f;
bool operator < (const NODE &a) const {
if(a.f == f)
return a.g < g;
return a.f < f;
}
}; void init(){
for(int i=0;i<=n;i++) {
ve[i].clear();
ve2[i].clear();
}
for(int i=1;i<=m;i++){
int a,b,len;
scanf("%d%d%d",&a,&b,&len);
ve[a].push_back(make_pair(len,b));
ve2[b].push_back(make_pair(len,a));
}
scanf("%d%d%d",&s,&e,&k);
} void spfa() {
memset(dis,0x7f,sizeof(dis));
queue <int> qu;
qu.push(e);
dis[e] = 0;
vis[e] = true;
while(!qu.empty()) {
int u = qu.front(); qu.pop();
int d = dis[u];
vis[u] = false;
for(int i=0;i<ve2[u].size();i++) {
int v = ve2[u][i].second;
int d2 = d + ve2[u][i].first;
if(d2 < dis[v]){
dis[v] = d2;
if(!vis[v]) {
qu.push(v);
vis[v] = true;
}
}
}
}
} int A_star() {
if(dis[s] == 0x7f7f7f7f)
return -1;
NODE temp,Next;
int cnt = 0;
if(s == e)
k++;
temp.to = s; temp.g = 0; temp.f = temp.g + dis[temp.to];
priority_queue <NODE> qu;
qu.push(temp); while(!qu.empty()){
temp = qu.top(); qu.pop();
if(temp.to == e) cnt++;
if(cnt == k) return temp.g; int u = temp.to;
int d = temp.g;
for(int i=0;i<ve[u].size();i++) {
Next.to = ve[u][i].second;
Next.g = d + ve[u][i].first;
Next.f = Next.g + dis[Next.to];
qu.push(Next);
}
}
return -1;
} int main(){
while(scanf("%d%d",&n,&m) != EOF) {
init();
spfa();
int ans = A_star();
printf("%d\n", ans);
return 0;
}
}

POJ:2449-Remmarguts' Date(单源第K短路)的更多相关文章

  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. K短路模板POJ 2449 Remmarguts' Date

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

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

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

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

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

  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*算法)

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

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

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

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

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

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

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

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

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

随机推荐

  1. bai_du 采集代码(已过期)

    <?php $url = "http://www.baidu.com/s?wd=site:www.xxxxxx.com+inurl:hot&tn=baidulaonian&am ...

  2. MySQL累加值时,考虑到值有为NULL的情况.

    一个字段,表示报名人数,默认为null,经考虑,以以下sql执行加1: ) where id='xxx'

  3. Azure 7 月新公布

    Azure 7月新发布:Cosmos DB,事件中心捕捉功能,Hybrid Connections,流量管理器快速故障转移功能. 您现有的 DocumentDB 资源现已作为 Azure 门户上 Az ...

  4. Oracle 安装教程图解

    一.Oracle 下载 注意Oracle分成两个文件,下载完后,将两个文件解压到同一目录下即可. 路径名称中,最好不要出现中文,也不要出现空格等不规则字符. 官方下地址: http://www.ora ...

  5. 光标显示样式 css 中 cursor 属性使用

    记录一下 cursor 的各种样式,方便自己查找.之前用到不常用的每次去 百度 或 Google 找不如自己记录下好找些. cursor光标类型 auto default none context-m ...

  6. azkaban调度

    azkaban调度 1.概述 azkaban是一套调度系统,常用大数据作业调度.azkaban包括web和executor两套程序,web主要完成展示和交互,executor上完成调度和作业提交执行. ...

  7. C#学习基础,面向对象的三大特征

    学习C#编程,相信大家除了经常接触的是hello world之外,更多的是进一步的去熟悉这门语言的基本特征,以及有哪些概念是我们必要掌握了解的,相信大家都是会知道面向对象的三大特性分别是:封装,继承, ...

  8. 大数据量高并发的数据库优化详解(MSSQL)

    转载自:http://www.jb51.net/article/71041.htm 如果不能设计一个合理的数据库模型,不仅会增加客户端和服务器段程序的编程和维护的难度,而且将会影响系统实际运行的性能. ...

  9. spring教程(一):简单实现(转)

    转:https://www.cnblogs.com/Lemon-i/p/8398263.html  一.概念介绍 1. 一站式框架:管理项目中的对象.spring框架性质是容器(对象容器) 2. 核心 ...

  10. 轻量级HTTP服务器Nginx(常用配置实例)

    轻量级HTTP服务器Nginx(常用配置实例)   文章来源于南非蚂蚁   Nginx作为一个HTTP服务器,在功能实现方面和性能方面都表现得非常卓越,完全可以与Apache相媲美,几乎可以实现Apa ...