POJ 2449 Remmarguts' Date(第k短路のA*算法)
Description
"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 last line consists of three integer numbers S, T and K (1 <= S, T <= N, 1 <= K <= 1000).
Output
#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*算法)的更多相关文章
- POJ 2449 Remmarguts' Date ( 第 k 短路 && A*算法 )
题意 : 给出一个有向图.求起点 s 到终点 t 的第 k 短路.不存在则输出 -1 #include<stdio.h> #include<string.h> #include ...
- poj 2449 Remmarguts' Date(K短路,A*算法)
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u013081425/article/details/26729375 http://poj.org/ ...
- poj 2449 Remmarguts' Date (k短路模板)
Remmarguts' Date http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Total Subm ...
- POJ 2449 - Remmarguts' Date - [第k短路模板题][优先队列BFS]
题目链接:http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Description "Good m ...
- poj 2449 Remmarguts' Date 第k短路 (最短路变形)
Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 33606 Accepted: 9116 ...
- 【POJ】2449 Remmarguts' Date(k短路)
http://poj.org/problem?id=2449 不会.. 百度学习.. 恩. k短路不难理解的. 结合了a_star的思想.每动一次进行一次估价,然后找最小的(此时的最短路)然后累计到k ...
- POJ 2449 Remmarguts' Date (SPFA + A星算法) - from lanshui_Yang
题目大意:给你一个有向图,并给你三个数s.t 和 k ,让你求从点 s 到 点 t 的第 k 短的路径.如果第 k 短路不存在,则输出“-1” ,否则,输出第 k 短路的长度. 解题思路:这道题是一道 ...
- 【POJ】2449.Remmarguts' Date(K短路 n log n + k log k + m算法,非A*,论文算法)
题解 (搬运一个原来博客的论文题) 抱着板题的心情去,结果有大坑 就是S == T的时候也一定要走,++K 我发现按照论文写得\(O(n \log n + m + k \ log k)\)算法没有玄学 ...
- poj 2449 Remmarguts' Date(第K短路问题 Dijkstra+A*)
http://poj.org/problem?id=2449 Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Subm ...
- poj 2449 Remmarguts' Date K短路+A*
题目链接:http://poj.org/problem?id=2449 "Good man never makes girls wait or breaks an appointment!& ...
随机推荐
- 个人免签即时到账收款接口 bufpay.com 支持多账号收款
有很多 bufpay 的用户反馈,单个手机收款有些时候不太方便,切换手机太麻烦:或者是营业额比较多,希望分摊到多个账号上面. 基于以上的问题,bufpay 开发了多手机收款的功能:每个收款的手机安装 ...
- sublime text3配置python开发环境(windows版)
安装阶段: sublime text3的安装: 下载网址:https://www.sublimetext.com/ 下载完成后 ,点击安装即可. 安装Package Control: 点击 Tools ...
- JavaScript手绘风格的图形库RoughJS使用指南
RoughJS是一个轻量级的JavaScript图形库(压缩后约9KB),可以让你在网页上绘制素描风格.手绘样式般的图形.RoughJS定义了绘制直线,曲线,圆弧,多边形,圆和椭圆的图元,同时它还支持 ...
- 移动端利用canvas画布简单实现刮刮乐效果
为了研究canvas一些属性简单实现的一个小效果 代码样式不太规范 随手写的 请问喷 初学者可以看下 css代码 <style> * { margin: 0; padding: 0; } ...
- C# Server.MapPath的使用方法
(1)WebForm中: HttpContext.Current.Server.MapPath("~/Files/car/"); (2)Mvc中: Server.MapPath() ...
- php 冒泡法 排序
<?php /** * php 冒泡法 * @param $arr * @param string $order 排序符 * @return $arr */ function orderarr( ...
- DQL数据查询
set hive.fetch.task.conversion=more; -- 避免触发MR job select distinct name from employee_id limit 2; -- ...
- Linux服务器安全检测维护基础汇总/持续更新
登陆系统查询可以用户 w命令可以显示在线用户,passwd -l xxx可以锁定xxx用户无法登陆,如果此时可以用户在线,使用kill命令踢下线 查看可疑进程 ps -ef命令锁定pid,或者pido ...
- Go编写一个比特币交易自动出价程序
语言环境为>=go1.10 go语言环境不多说 实现目的能与BitMEX api进行交互自动交易,目前虚拟币平台很多,平台API实现也很容易.后续会加上其它平台和自动交易算法策略,具体看平台交易 ...
- 分治与递归-找k个临近中位数的数
问题描述:给定由n个互不相同的数组成的集合S以及正整数k≤n,试设计一个O(n)时间算法找出S中最接近S的中位数的k个数. 算法描述: 用线性时间选择实现的算法找到中位数 S’=除去中位数外的S S& ...