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. js实现点击按钮可实现编辑

    <script type="text/javascript">//修改密码//抓取到的数据 function edit() { document.getElementB ...

  2. Ubuntu更换国内源

    打开终端,输入:sudo gedit /etc/apt/sources.list 在文件最底部输入以下内容: deb http://mirrors.ustc.edu.cn/ubuntu/ xenial ...

  3. MySQL(mariadb)多实例应用与多实例主从复制

    MySQL多实例 mysql多实例,简单理解就是在一台服务器上,mysql服务开启多个不同的端口(如3306.3307,3308),运行多个服务进程.这些 mysql 服务进程通过不同的 socket ...

  4. 『Linux基础 - 4 』linux常用命令(1)

    这篇笔记包含以下知识点: 几个概念的理解:Linux命令,控制台,终端, 终端提示符 对文件目录的操作的相关命令: 切换目录,列出目录下的文件等 对文件的操作的相关命令: 创建,删除,复制,修改,移动 ...

  5. Python中的封装,继承和多态

    面向对象的三大特性:封装,继承和多态 封装:在类的内部定义属性和方法,通过对象或类名来访问属性和方法,隐藏功能的实现细节,也可以设置访问权限. 广义的封装:实例化一个对象,给对象空间封装一些属性:狭义 ...

  6. Go搭建一个博客系统

    go语言环境就不用多说了,版本肯定越高越好,这里用go1.10 先放着

  7. 20145207 ms08_067攻击实验

    ms08_067攻击实验原理 实验过程 查看两台主机ip,并ping通 启动msf,查看关于ms08_067漏洞的基本信息 查看其可攻击的操作系统 查看可用载荷 查看需要设定的参数 修改LHOST(攻 ...

  8. 使用putty远程登录Ubuntu时,报Network error:Connection refused错误及解决

    putty远程登录Ubuntu,弹出Network error:Connection refused的错误提示框,就是因为Ubuuntu没有安装ssh服务. 执行命令: sudo apt instal ...

  9. 北京Uber优步司机奖励政策(1月3日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  10. 佛山Uber优步司机奖励政策(12月28日到1月3日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...