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!& ...
随机推荐
- 四种常见的 POST 提交数据方式对应的content-type
原文地址:https://www.cnblogs.com/wushifeng/p/6707248.html application/x-www-form-urlencoded 这应该是最常见的 POS ...
- Django学习笔记1
重点在注释# 1.views.py from django.shortcuts import render from django.http import * #from django.templat ...
- Eclipse切换字体颜色
打开window-preferences
- Python入门(案例)
Python入门(案例) #一.上课案例: #输出hello wordprint('hello word') #python注释有两种#1.单行注释#这是单行注释#2.多行注释'''这是多行注释''' ...
- js新开窗口避免浏览器拦截解决方案
Ajax回调函数中,打开新窗口解决方案 var w = window.open(); $.post("GetHomToTestPaper", {homeworkName:homew ...
- 利用haohedi ETL将数据库中的数据抽取到hadoop Hive中
采用HIVE自带的apache 的JDBC驱动导入数据基本上只能采用Load data命令将文本文件导入,采用INSERT ... VALUES的方式插入速度极其慢,插入一条需要几十秒钟,基本上不可用 ...
- MPP调研
一.MMP数据库 MPP是massively parallel processing,一般指使用多个SQL数据库节点搭建的数据仓库系统.执行查询的时候,查询可以分散到多个SQL数据库节点上执行,然后汇 ...
- 前端css之float浮动
浮动的准则,先找前一个块标签,在确认有否清除浮动的条件或者是距离的情况下,如果这一行能摆得下,就继续紧贴前一个标签 如果摆不下,就会另起一行 浮动只有左边和右边 如果是块标签,设置浮动,先把displ ...
- 虚拟机的三种联网模式(桥接模式、NAT 模式、仅主机模式)
虚拟机的网络连接方式分为三种,分别是桥接模式.NAT 模式.和仅主机模式,三种连接模式存在着一定的差异,那么我们该如何选择适合自己的连接模式呢? 1.桥接模式:在此模式下,虚拟机相当于一台独立的电脑, ...
- linux进程篇 (二) 进程的基本控制
2. 进程的基本操作 接口函数 #include <unistd.h> //创建子进程 pid_t fork(void); //结束子进程 void exit(int status); / ...