Remmarguts' Date POJ - 2449 (A*搜索|k短路)
"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
Sample Input
2 2
1 2 5
2 1 4
1 2 2
Sample Output
14 题意:n个点,m条边的有向图,每个点可以多次抵达,给出s、t、k,求从s到t的第k短路
思路:求最短路最常用的就是堆优化的dijkstra,那么很容易想到的一种写法:用dijkstra,当t第k次出现,那就是第k短路;但是如果有些状态当前值很小,但是未来值很大,优先队列就会先去扩展这个状态,
这样的情况多了,就会导致搜索量增大。 优先队列是以当前所走过的路程最小的为优先的,它并不知道该状态后面的情况,所有排序并不是很完美,这时候我们就可以想到为其加入评估函数(估计该状态后面所需的路程),并且评估值<=实际值。
这样我们使用优先队列是,将(该状态花费+评估值,节点)送入队列,取出时是总体评估最小,也就是不仅考虑了已走路程,顺便考虑了其后续可能路程,取出之后,花费-该点评估值==实际花费,然后扩展该状态,
将(状态实际花费+下个点评估值+该路径长度)送入队列
这样当t出项k次时出现的就是第k短路
因为 评估值<=实际值,所以状态花费+评估值 <= 状态花费+实际值 == s(总花费),所以最优值肯定会在次优值之前出栈,(虽然之前的优先队列也这样)而且因为评估了后续所需,所以不会出现前小后大的
非最优花费大量在队列前扩展,尽量让其直接将最优解排在队列前扩展,少扩展其他分支。 那么这个题的评估函数怎么求呢,因为我们是求到t的k短路,那么我们就把所有点到t的对短路当成评估函数,也就是以t为原点,求一遍最短路,刚好这个值可以反应未来变化的趋势和相对大小关系
注:该题当s==t时,0并不是第一条路
#include<iostream>
#include<cstdio>
#include<queue>
#include<string.h>
using namespace std; int n,m;
typedef pair<int,int>p;
struct E
{
int x,y,val;
int next;
E(int x=,int y=,int val=,int next=-):x(x),y(y),val(val),next(next){}
}edge[];
int head[];
int cnt;
E t_edge[];
int t_head[];
int t_cnt;
int f[];
int ans[];
void add(int x,int y,int val)
{
edge[++cnt] = E(x,y,val,head[x]);
head[x] = cnt;
t_edge[++t_cnt] = E(y,x,val,t_head[y]);
t_head[y] = t_cnt;
} void get_f(int t)
{
memset(f,0x3f,sizeof(f));
priority_queue<p,vector<p>,greater<p> >que;
while(!que.empty())que.pop();
que.push(p(,t));
while(!que.empty())
{
p tmp = que.top();
que.pop();
int now = tmp.second;
int cost = tmp.first;
if(f[now] != 0x3f3f3f3f)continue;
f[now] = cost;
for(int i=t_head[now];i!=-;i=t_edge[i].next)
{
if(f[now] + t_edge[i].val < f[t_edge[i].y])
{ que.push(p(f[now] + t_edge[i].val,t_edge[i].y));
}
}
}
} int cal(int s,int t,int k)
{
priority_queue<p,vector<p>,greater<p> >que;
while(!que.empty())que.pop();
que.push(p(f[s],s));
memset(ans,0x3f,sizeof(ans));
int cnt = ;
if(s == t)k++;
if(f[s] == 0x3f3f3f3f)return -;
while(!que.empty())
{
p tmp = que.top();
que.pop();
int now = tmp.second;
int cost = tmp.first - f[now];
if(now == t)
{
cnt++;
if(cnt == k)return cost;
}
for(int i=head[now];i!=-;i=edge[i].next)
{
que.push(p(cost+edge[i].val+f[edge[i].y],edge[i].y));
}
}
return -;
} int main()
{
scanf("%d%d",&n,&m);
memset(head,-,sizeof(head));
memset(t_head,-,sizeof(t_head));
for(int i=;i<=m;i++)
{
int u,v,k;
scanf("%d%d%d",&u,&v,&k);
add(u,v,k);
}
int s,t,k;
scanf("%d%d%d",&s,&t,&k);
get_f(t);
printf("%d\n",cal(s,t,k));
}
Remmarguts' Date POJ - 2449 (A*搜索|k短路)的更多相关文章
- POJ 2449 Dijstra + A* K短路
这题一开始的思路应该是直接从源点进行BFS搜索K短路. 但这样的复杂度在点数和K的值增大后将会变得很大. 而A*算法则构造一个h(x),在进行BFS时,每次都抛出最小的h(x)从而使汇点的出队速度加快 ...
- POJ 2449 求第K短路
第一道第K短路的题目 QAQ 拿裸的DIJKSTRA + 不断扩展的A* 给2000MS过了 题意:大意是 有N个station 要求从s点到t点 的第k短路 (不过我看题意说的好像是从t到s 可能是 ...
- POJ:2449-Remmarguts' Date(单源第K短路)
Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 33081 Accepted: 8993 Des ...
- POJ——T 2449 Remmarguts' Date
http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 30754 ...
- 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短路模板)
Remmarguts' Date http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Total Subm ...
- 【POJ】2449 Remmarguts' Date(k短路)
http://poj.org/problem?id=2449 不会.. 百度学习.. 恩. k短路不难理解的. 结合了a_star的思想.每动一次进行一次估价,然后找最小的(此时的最短路)然后累计到k ...
- poj 2449 Remmarguts' Date K短路+A*
题目链接:http://poj.org/problem?id=2449 "Good man never makes girls wait or breaks an appointment!& ...
- POJ 2449 - Remmarguts' Date - [第k短路模板题][优先队列BFS]
题目链接:http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Description "Good m ...
随机推荐
- 数据库技术丛书:SQL Server 2016 从入门到实战(视频教学版) PDF
1:书籍下载方式: SQL Server2016从入门到实战 PDF 下载 链接:https://pan.baidu.com/s/1sWZjdud4RosPyg8sUBaqsQ 密码:8z7w 学习 ...
- Selenium WebDriver中鼠标事件
鼠标点击操作 鼠标点击事件有以下几种类型: 清单 1. 鼠标左键点击 Actions action = new Actions(driver);action.click();// 鼠标左键在当 ...
- 页面初始化的js函数要放在最最最最最最最前边!否则没效果
简单说一下这个情况,html的页面的各部分都是动态渲染的,所以头部的某些个样式调用函数需要在页面初始化的时候被加载,这个我也是知道的,结果后边代码敲着敲着,就把这个事儿给忘了,然后启动项目的时候,页面 ...
- python基础之迭代器与生成器
一.什么是迭代器: 迭代是Python最强大的功能之一,是访问集合元素的一种方式. 迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束. 迭代器是一个可以记住遍历的位置的对象. 迭代器的 ...
- 《剑指offer》二叉树中和为某一值的路径
本题来自<剑指offer> 反转链表 题目: 思路: C++ Code: Python Code: 总结:
- eclipse创建动态maven项目
需求表均同springmvc案例 此处只是使用maven 注意,以下所有需要建立在你的eclipse等已经集成配置好了maven了,说白了就是新建项目的时候已经可以找到maven了 没有的话需要安装m ...
- 论文阅读笔记二-ImageNet Classification with Deep Convolutional Neural Networks
分类的数据大小:1.2million 张,包括1000个类别. 网络结构:60million个参数,650,000个神经元.网络由5层卷积层,其中由最大值池化层和三个1000输出的(与图片的类别数相同 ...
- k-近邻算法-手写识别系统
手写数字是32x32的黑白图像.为了能使用KNN分类器,我们需要把32x32的二进制图像转换为1x1024 1. 将图像转化为向量 from numpy import * # 导入科学计算包numpy ...
- Python深度学习案例2--新闻分类(多分类问题)
本节构建一个网络,将路透社新闻划分为46个互斥的主题,也就是46分类 案例2:新闻分类(多分类问题) 1. 加载数据集 from keras.datasets import reuters (trai ...
- requests之json系列(一)
以post方式获取接口指定的相关信息 #! /usr/bin/env python # coding=utf-8 import json import urllib import requests i ...