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.
 
题目大意:求S到T的第K短路
思路:A*算法,f(n) = h(n) + g(n),h(n)为从S走过重重关卡到点n的距离,g(n)为从n走到T的最短距离,在这里估价函数是完美的,所以是不会出错的。至于第k短路,只要找第k次走到终点即可。
PS:S == T的情况下,k要加一,因为不走好像是不算的……英文抓鸡完全看不到题目有这么说过>_<
 
 #include <cstdio>
#include <cstring>
#include <queue>
#include <utility>
using namespace std; const int INF = 0x3f3f3f3f;
const int MAXN = ;
const int MAXM = ; #define X first
#define Y second typedef pair<int, int> PII; int head[MAXN], rhead[MAXN];
int next[MAXM], rnext[MAXM], to[MAXM], rto[MAXM], cost[MAXM];
int ecnt; void init() {
ecnt = ;
memset(head, , sizeof(head));
memset(rhead, , sizeof(rhead));
} void add_edge(int u, int v, int c) {
cost[ecnt] = c;
to[ecnt] = v; next[ecnt] = head[u]; head[u] = ecnt;
rto[ecnt] = u; rnext[ecnt] = rhead[v]; rhead[v] = ecnt++;
} int dis[MAXN];
bool vis[MAXN]; void dijkstra(int st, int n) {
for(int i = ; i <= n; ++i) dis[i] = INF;
memset(vis, , sizeof(vis));
priority_queue<PII> Q;
dis[st] = ; Q.push(make_pair(, st));
while(!Q.empty()) {
int u = Q.top().Y; Q.pop();
if(vis[u]) continue;
vis[u] = true;
for(int p = rhead[u]; p; p = rnext[p]) {
int v = rto[p];
if(dis[v] > dis[u] + cost[p]) {
dis[v] = dis[u] + cost[p];
Q.push(make_pair(-dis[v], v));
}
}
}
} int a_star(int st, int ed, int n, int k) {
priority_queue<PII> Q;
Q.push(make_pair(-dis[st], st));
while(!Q.empty()) {
int u = Q.top().Y, c = -Q.top().X; Q.pop();
if(u == ed && --k == ) return c;
for(int p = head[u]; p; p = next[p])
Q.push(make_pair(-(c - dis[u] + cost[p] + dis[to[p]]), to[p]));
}
return -;
} int main() {
int n, m, st, ed, k;
while(scanf("%d%d", &n, &m) != EOF) {
init();
for(int i = ; i < m; ++i) {
int u, v, c;
scanf("%d%d%d", &u, &v, &c);
add_edge(u, v, c);
}
scanf("%d%d%d", &st, &ed, &k);
dijkstra(ed, n);
if(dis[st] == INF) {
printf("-1\n");
continue;
}
if(st == ed) ++k;
printf("%d\n", a_star(st, ed, n, k));
}
}

POJ 2449 Remmarguts' Date(第k短路のA*算法)的更多相关文章

  1. POJ 2449 Remmarguts' Date ( 第 k 短路 && A*算法 )

    题意 : 给出一个有向图.求起点 s 到终点 t 的第 k 短路.不存在则输出 -1 #include<stdio.h> #include<string.h> #include ...

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

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

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

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

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

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

  6. 【POJ】2449 Remmarguts' Date(k短路)

    http://poj.org/problem?id=2449 不会.. 百度学习.. 恩. k短路不难理解的. 结合了a_star的思想.每动一次进行一次估价,然后找最小的(此时的最短路)然后累计到k ...

  7. POJ 2449 Remmarguts' Date (SPFA + A星算法) - from lanshui_Yang

    题目大意:给你一个有向图,并给你三个数s.t 和 k ,让你求从点 s 到 点 t 的第 k 短的路径.如果第 k 短路不存在,则输出“-1” ,否则,输出第 k 短路的长度. 解题思路:这道题是一道 ...

  8. 【POJ】2449.Remmarguts' Date(K短路 n log n + k log k + m算法,非A*,论文算法)

    题解 (搬运一个原来博客的论文题) 抱着板题的心情去,结果有大坑 就是S == T的时候也一定要走,++K 我发现按照论文写得\(O(n \log n + m + k \ log k)\)算法没有玄学 ...

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

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

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

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

随机推荐

  1. 洛谷P4525 【模板】自适应辛普森法1(simpson积分)

    题目描述 计算积分 结果保留至小数点后6位. 数据保证计算过程中分母不为0且积分能够收敛. 输入输出格式 输入格式: 一行,包含6个实数a,b,c,d,L,R 输出格式: 一行,积分值,保留至小数点后 ...

  2. MySQL索引的使用及注意事项

    索引是存储引擎用于快速找到记录的一种数据结构.索引优化应该是对查询性能优化最有效的手段了.索引能够轻易将查询性能提高几个数量级,"最优"的索引有时比一个"好的" ...

  3. MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

    背景描述 # Time: :: # User@Host: **[**] @ [**] Id: ** # Killed: # Query_time: Rows_examined: Rows_affect ...

  4. vue调用豆瓣API加载图片403问题

    "豆瓣API是有请求次数限制的”,这会引发图片在加载的时候出现403问题,视图表现为“图片加载不出来”,控制台表现为报错403. 其实是豆瓣限制了图片的加载,我自己用了一个办法把图片缓存下来 ...

  5. 关于php中数字0与其他变量的相等判断

    在实践过程中,经常需要做`==`判断,有时候会把0当做false一样用,但是0和false在用来做比较的时候还是不一样的, false false==0 等于true false=='0' 等于tru ...

  6. Django自定制分页功能

    URL: """django_paginner URL Configuration The `urlpatterns` list routes URLs to views ...

  7. Django的安装创建与连接数据库

    HTTP协议简介 HTTP是一个客户端终端(用户)和服务器端(网站)请求和应答的标准(TCP).通过使用网 页浏览器.网络爬虫或者其它的工具,客户端发起一个HTTP请求到服务器上指定端口(默认端 口为 ...

  8. JZ2440开发板:修改ARM芯片时钟(学习笔记)

    想要修改ARM芯片的时钟,需要去查询芯片手册和原理图,获取相关的信息(见下方图片) 首先来看时钟的结构图 根据结构图可以看出,时钟源有两种选择:1. XTIpll和XTOpll所连接的晶振 2. EX ...

  9. vim 配色方案

    1. 自己电脑上的vim 注释很难看清,又不想取消高亮.原来显示: 在 if has("syntax") syntax onendif 语句下面追加一句: colorscheme ...

  10. WebRTC中Android Demo中的远程视频流的获取到传输

    1.CallActivity#onCreate 执行startCall开始连接或创建房间 2.WebSocketClient#connectToRoom 请求一次服务器 3.回调到CallActivi ...