题目链接: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模板)的更多相关文章

  1. POJ2449 K短路模板

    #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> ...

  2. POJ2449:K短路

    Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 26355   Accepted: 7170 ...

  3. A_star poj2449 k短路

    赛后填坑系列QAQ 贴代码呀 #include<iostream> #include<algorithm> #include<cstdio> #include< ...

  4. 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. ...

  5. poj 2449 Remmarguts' Date K短路+A*

    题目链接:http://poj.org/problem?id=2449 "Good man never makes girls wait or breaks an appointment!& ...

  6. 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 ...

  7. poj 2449 Remmarguts' Date(第K短路问题 Dijkstra+A*)

    http://poj.org/problem?id=2449 Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Subm ...

  8. poj 2449 Remmarguts' Date 第k短路 (最短路变形)

    Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 33606   Accepted: 9116 ...

  9. A* K短路

    注:\(A*\) 求解K短路效率极其低下,时间复杂度\(O(nklog\ n)\),空间视题目而定,因为本质是爆搜,可求解数据范围较小的题目. 我们使用\(A*\)求解k短路: 首先需要预处理出估价函 ...

随机推荐

  1. Linux入门基础(三)——系统命令

  2. python仪表盘

    1.在这里可以看到pyecharts中有定义好的各种图标类. 复制上面代码,会出现“ModuleNotFoundError: No module named 'pyecharts'”. pip ins ...

  3. High Performance Browser Networking

    Chapter 1. Primer on Latency and Bandwidth As a result, to improve performance of our applications, ...

  4. ubuntu 更新软件命令

    安装软件最好加权限(sudo) --default-timeout=100 设置超时时间100秒 install -U setuptools 表示更新安装setuptools sudo pip3 -- ...

  5. POJ - 1611 The Suspects 【并查集】

    题目链接 http://poj.org/problem?id=1611 题意 给出 n, m 有n个人 编号为 0 - n - 1 有m组人 他们之间是有关系的 编号为 0 的人是 有嫌疑的 然后和 ...

  6. Understanding JDBC Internals & Timeout Configuration

    原版:http://www.cubrid.org/blog/dev-platform/understanding-jdbc-internals-and-timeout-configuration 中文 ...

  7. Builder 模式初探

    Builder 模式是一步一步创建一个复杂对象的创建型模式,它允许用户在不知道内部构建细节的情况下,可以更精细的控制对象的构造流程.该模式是为了将构建复杂对象的过程和它的部件解耦,使得构建过程和部件的 ...

  8. leetcode 859. Buddy Strings

    Given two strings A and B of lowercase letters, return true if and only if we can swap two letters i ...

  9. Python网络编程--Echo服务

    Python网络编程--Echo服务 学习网络编程必须要练习的三个小项目就是Echo服务,Chat服务和Proxy服务.在接下来的几篇文章会详细介绍. 今天就来介绍Echo服务,Echo服务是最基本的 ...

  10. POJ 2409 Let it Bead:置换群 Polya定理

    题目链接:http://poj.org/problem?id=2409 题意: 有一串n个珠子穿起来的项链,你有k种颜色来给每一个珠子染色. 问你染色后有多少种不同的项链. 注:“不同”的概念是指无论 ...