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!& ...
随机推荐
- centos6,python3,通过pip安装pycurl出现报错提示
Centos6.7系统,python3.6.7,通过 pip 安装pycurl出现报错: __main__.ConfigurationError: Could not run curl-config: ...
- Redis(七):Redis的发布订阅
Redis的发布订阅导航目录: 是什么 命令 案例 是什么 进程间的一种消息通信模式:发送者(pub)发送消息,订阅者(sub)接收消息. 订阅/发布消息图 命令 案例 先订阅后发布后才能收到消息,1 ...
- Python文本和字符串常用操作
## 字符串分割 line = "This is my love!" fields = line.split(' ') print(fields) # ['This', 'is', ...
- tp5.1路由报错No input file specified.
问题: 按照官方教安装了框架,打开首页没问题,可是安装教程路由规则打开 "http://127.0.0.1/hello/2" 时, 却报错误 "No input fil ...
- MapReduce输入输出的处理流程及combiner
MapReduce 的输入输出 MapReduce 框架运转在<key,value> 键值对上,也就是说,框架把作业的输入看成是一组<key,value>键值对,同样也产生一组 ...
- ruby 反射机制常用方法
1. 获取类的名称: .class 2. 获取超类的名称:.superclass 3. 获取类包含的模块:.class.included_modules 4. 检查是否为实例对象:.instance_ ...
- Linux字符设备驱动--No.2
分析中断注册函数:request_irq int butsOpen(struct inode *p, struct file *f) { int irq; int i; ; printk(KERN_E ...
- transient是干嘛的
Java的serialization提供了一种持久化对象实例的机制.当持久化对象时,可能有一个特殊的对象数据成员,我们不想用 serialization机制来保存它.为了在一个特定对象的一个域上关闭s ...
- Caliburn.Micro 杰的入门教程1(翻译)
Caliburn.Micro 杰的入门教程1(原创翻译)Caliburn.Micro 杰的入门教程2 ,了解Data Binding 和 Events(翻译)Caliburn.Micro 杰的入门教程 ...
- 成都Uber优步司机奖励政策(3月26日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...