poj2449(k短路&A_star模板)
题目链接:http://poj.org/problem?id=2449
题意:给出一个有向图,求s到t的第k短路;
思路:k短路模板题,可以用A_star模板过;
单源点最短路径+高级搜索A*;
A*算法结合了启发式方法和形式化方法;
启发式方法通过充分利用图给出的信息来动态地做出决定而使搜索次数大大降低;
形式化方法不利用图给出的信息,而仅通过数学的形式分析;
算法通过一个估价函数f(h)来估计图中的当前点p到终点的距离,并由此决定它的搜索方向;
当这条路径失败时,它会尝试其他路径;
对于A*,估价函数=当前值+当前位置到终点的距离,即f(p)=g(p)+h(p),每次扩展估价函数值最小的一个;
对于K短路算法来说,g(p)为当前从s到p所走的路径的长度;h(p)为点p到t的最短路的长度;
f(p)的意义为从s按照当前路径走到p后再走到终点t一共至少要走多远;
为了加速计算,h(p)需要在A*搜索之前进行预处理, 因为本题是有向图,所以将原图的所有边反向,再从终点t做一次单源点最短路径就能得到每个点的h(p)了;
算法步骤:
(1),将有向图的所有边反向,以原终点t为源点,求解t到所有点的最短距离;
(2),新建一个优先队列,将源点s加入到队列中;
(3),从优先级队列中弹出f(p)最小的点p,如果点p就是t,则计算t出队的次数;
如果当前为t的第k次出队,则当前路径的长度就是s到t的第k短路的长度,算法结束;
否则遍历与p相连的所有的边,将扩展出的到p的邻接点信息加入到优先级队列;
---------------------------------
以上这段话摘自其它博客,感觉写的不错...
代码:
#include<iostream>
#include<queue>
#include <string.h>
#include<algorithm>
#define MAXN 1010
using namespace std; const int inf=0x3f3f3f3f;
int dist[MAXN]; //**dist[i]记录此时源点到i的最短距离
bool vis[MAXN]; //***标记点是否在队列中
int cnt[MAXN]; //***cnt[i]记录i节点入队次数,判断是否存在负权环
vector<pair<int, int> >mp[MAXN*], mp2[MAXN*];//mp储存原图,mp2存储反向图
struct node{
int point;
int g, f;
friend bool operator<(node a, node b){
return a.f==b.f?a.g>b.g:a.f>b.f;
}
}; int A_star(int s, int e, int n, int k){
priority_queue<node> q;
int cnt=;//***当前为第cnt短路
if(s==e){//**本题题要求一定要经过其他点,即若终点和起点相同的话即为求第k+1短路
k++;
}
if(dist[s]==inf){//***终点点不可达
return -;
}
node node1;
node1.point=s;
node1.g=;
node1.f=dist[s]+node1.g;
q.push(node1); while(!q.empty()){
node cc=q.top(); //***将估价值最小的节点出队
q.pop();
if(cc.point==e){//**求出第cnt短路
cnt++;
}
if(cnt==k){//***已求出第k短路
return cc.g;
}
for(int i=; i<mp[cc.point].size(); i++){//**遍历当前节点的所有邻节点
node node2;
node2.point=mp[cc.point][i].first;
node2.g=cc.g+mp[cc.point][i].second;
node2.f=node2.g+dist[node2.point];
q.push(node2);
}
}
return -;
} bool spfa(int n, int s){ //**注意这里的n的实参是e,求出反向图中所有节点到n的最短距离,即所有节点到e的最短距离,即求出 h(x)
memset(dist, 0x3f, sizeof(dist));
queue<int> q;
while(!q.empty()){
q.pop();
}
q.push(s);
dist[s]=;
cnt[s]+=;
vis[s]=true;
while(!q.empty()){
int u=q.front();
q.pop();
vis[u]=false;
for(int i=; i<mp2[u].size(); i++){
int point=mp2[u][i].first;
if(dist[point]>dist[u]+mp2[u][i].second){ //**松驰操作
dist[point]=dist[u]+mp2[u][i].second;
if(!vis[point]){ //***若此点不在队列中则将其入队
vis[point]=true;
q.push(point);
cnt[point]++;
if(cnt[point]>n){ //***判断是否存在负权环
return false;
}
}
}
}
}
return true;
} int main(void){
ios::sync_with_stdio(false), cin.tie(), cout.tie();
int n, m;
int x, y, z;
cin >> n >> m;
while(m--){
cin >> x >> y >> z;
mp[x].push_back(make_pair(y, z));
mp2[y].push_back(make_pair(x, z)); //**建立反向图
}
int s, e, k;
cin >> s >> e >> k;
spfa(n, e);
int res=A_star(s, e, n, k);
cout << res << endl;
return ;
}
poj2449(k短路&A_star模板)的更多相关文章
- POJ2449 K短路模板
#include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> ...
- POJ2449:K短路
Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 26355 Accepted: 7170 ...
- A_star poj2449 k短路
赛后填坑系列QAQ 贴代码呀 #include<iostream> #include<algorithm> #include<cstdio> #include< ...
- ACM-ICPC 2018 沈阳赛区网络预赛-D:Made In Heaven(K短路+A*模板)
Made In Heaven One day in the jail, F·F invites Jolyne Kujo (JOJO in brief) to play tennis with her. ...
- poj 2449 Remmarguts' Date K短路+A*
题目链接:http://poj.org/problem?id=2449 "Good man never makes girls wait or breaks an appointment!& ...
- upc组队赛15 Made In Heaven【第K短路 A*】
Made In Heaven 题目描述 One day in the jail, F·F invites Jolyne Kujo (JOJO in brief) to play tennis with ...
- 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 Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 33606 Accepted: 9116 ...
- A* K短路
注:\(A*\) 求解K短路效率极其低下,时间复杂度\(O(nklog\ n)\),空间视题目而定,因为本质是爆搜,可求解数据范围较小的题目. 我们使用\(A*\)求解k短路: 首先需要预处理出估价函 ...
随机推荐
- HTML5/CSS3鼠标滑过图片滤镜动画效果
在线演示 本地下载
- zabbix监控系统性能采集指标
监控项目 详细内容 ...
- JavaScript(3)
var a=90; switch(a){ case "890": window.alert("ok"); break; case 90: window.aler ...
- 安装与设置hexo
普通用户(非全局)安装nodejs和npm wget -qO- https://raw.github.com/creationix/nvm/master/install.sh | sh nvm ins ...
- linux下MySQL5.6安装记录
MySQL下载地址: ftp://mirror.switch.ch/mirror/mysql/Downloads/MySQL-5.6/http://mirrors.sohu.com/mysql/ ...
- rand()与srand()
1.不用srand()的话 两次运行程序产生的随机数序列相同 2.用srand() 两次运行程序产生的随机数则不同 示例程序: #include<iostream> #include< ...
- HDU 2035 人见人爱A^B(二分求幂,快速求幂)
题意:求A的B次方的后三位数字 思路1:常规求幂,直接取余求解 代码: #include<iostream> #include<cstdio> using namespace ...
- linux进程学习笔记
学习了linux下的进程,觉得应该整理一下,忘得差不多了,顺便回顾一下. 学而时习之,不亦说乎~~ 进程笔记 ,什么是进程? The Single UNIX Specification, Versio ...
- 【运维程序】简单的命令控制器(支持定时命令执行、重复定时任务命令和进程管理,开发这个小程序主要是为了方便管理服务进程)【个人github项目】
一.前言: command-controller 一个运维程序,简单的命令控制器(支持定时命令执行和重复定时命令,开发这个程序主要是为了方便管理服务进程) 本来是要用python做的,但是之前做ffm ...
- ASM认证与口令文件
ASM认证 ORACLE ASM 实例没有数据字典,所以连接ASM 实例只能通过如下三种系统权限来进行连接: SYSASM,SYSDBA,SYSOPER. 可以通过如下三种模式来连接ASM 实例:1. ...