poj 2449 k短路+A*算法
http://poj.org/problem?id=2449
K短路的定义:
1.如果起点终点相同,那么0并不是最短路,而是要出去一圈回来之后才是最短路,那么第K短路也是一样。
2.每个顶点和每条边都可以使用多次。(可以存在环和来回走)
给定起终点,求K短路的长度
然后求K短路使用A*算法,其估价函数f(n) = g(n)+h(n),h(n)是终点到结点n的最短路径,g(n)是起点到结点n的实际代价, 这样选择显然能满足A*估价函数的要求,g(n)>=g'(n),
h(n)<=h'(n)。
h(n)可以对原图的逆图进行SPFA得出。
终止条件是,如果终点第K次出队的话,那么表明已经找到第K短路。
注意处理起点终点不连通的情况。
#pragma comment(linker, "/STACK:36777216")
#pragma GCC optimize ("O2")
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define clr0(x) memset(x,0,sizeof(x))
#define clr1(x) memset(x,-1,sizeof(x))
#define eps 1e-9
const double pi = acos(-1.0);
typedef long long LL;
typedef unsigned long long ULL;
const int modo = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int inf = 0x3fffffff;
const int maxn = 1005,maxm = 100005;
struct edge{
int v,w,next;
edge(){};
edge(int vv,int ww,int nnext):v(vv),w(ww),next(nnext){};
}e[maxm<<1];
struct node{
int f,g,v;
node(){};
node(int ff,int gg,int vv):f(ff),g(gg),v(vv){};
bool operator < (const node &a) const{
return a.f < f;
}
};
int head[maxn],tail[maxn],dist[maxn],inq[maxn];
int n,m,s,t,k,ecnt;
void init()
{
clr1(head),clr1(tail);
ecnt = 0;
fill(dist,dist+maxn,inf);
clr0(inq);
}
void add(int u,int v,int w)
{
e[ecnt] = edge(v,w,head[u]);
head[u] = ecnt++;
e[ecnt] = edge(u,w,tail[v]);
tail[v] = ecnt++;
}
void spfa(int src)
{
queue<int> q;
q.push(src);dist[src] = 0,inq[src] = 1;
while(!q.empty()){
int cur = q.front();
q.pop();inq[cur] = 0;
for(int i = tail[cur];i != -1;i = e[i].next){
int nxt = e[i].v;
if(dist[nxt] > dist[cur] + e[i].w){
dist[nxt] = dist[cur] + e[i].w;
if(!inq[nxt])
inq[nxt] = 1,q.push(nxt);
}
}
} }
int Astar(int src,int dst){
priority_queue<node> q;
if(dist[src] == inf)
return -1;
clr0(inq);
q.push(node(dist[src],0,src));
while(!q.empty()){
node cur = q.top();
q.pop();
inq[cur.v]++;
if(inq[dst] == k)
return cur.f;
if(inq[cur.v] > k)
continue;
for(int i = head[cur.v];i != -1;i = e[i].next){
node nxt(dist[e[i].v] + e[i].w + cur.g,e[i].w + cur.g,e[i].v);
q.push(nxt);
}
}
return -1;
}
int main(){
int u,v,w;
while(~RD2(n,m)){
init();
while(m--){
RD3(u,v,w);u--,v--;
add(u,v,w);
}
RD3(s,t,k);s--,t--;
spfa(t);
if(s == t)//如果起点终点相同,0不是第1短路径。。
k++;
printf("%d\n",Astar(s,t));
}
return 0;
}
poj 2449 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短路 A*算法)
题目链接 Description "Good man never makes girls wait or breaks an appointment!" said the mand ...
- POJ 2449 Remmarguts' Date(第k短路のA*算法)
Description "Good man never makes girls wait or breaks an appointment!" said the mandarin ...
- UESTC - 1987 童心未泯的帆宝和乐爷 (第k短路 A*算法+SPFA算法 模板)
传送门: http://www.qscoj.cn/#/problem/show/1987 童心未泯的帆宝和乐爷 Edit Time Limit: 10000 MS Memory Limit: ...
- 【k短路&A*算法】BZOJ1975: [Sdoi2010]魔法猪学院
Description 找出1~k短路的长度. Solution k短路的求解要用到A*算法 A*算法的启发式函数f(n)=g(n)+h(n) g(n)是状态空间中搜索到n所花的实际代价 h(n) ...
- K短路 (A*算法) [Usaco2008 Mar]牛跑步&[Sdoi2010]魔法猪学院
A*属于搜索的一种,启发式搜索,即:每次搜索时加一个估价函数 这个算法可以用来解决K短路问题,常用的估价函数是:已经走过的距离+期望上最短的距离 通常和Dijkstra一起解决K短路 BZOJ1598 ...
- poj 2449 Remmarguts' Date 求第k短路 Astar算法
=.=好菜 #include <iostream> #include <cstdio> #include <string.h> #include <cstri ...
- poj 3013 最短路SPFA算法
POJ_3013_最短路 Big Christmas Tree Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 23630 ...
随机推荐
- (O)JS高阶函数应用——函数节流
在一些函数需被频繁调用的场景,如:window.onresize.mousemove.scroll滚动事件.上传进度等等,操作频繁导致性能消耗过高,而造成浏览器卡顿现象,我们可以通过函数节流的方式解决 ...
- git报“commiter email "root@localhost.localdomain"does not match your user account”
首先检查账户邮箱配置是否正确,检查方法: git config --list 发现邮箱及帐号配置正确,但是git push时仍然报如题错误: 原因:git执行add.commit 时已经记录下了做了该 ...
- You have more than one version of ‘org.apache.commons.logging.Log’ visible, which is not allowed问题解决
https://zeroturnaround.com/forums/topic/jrebel-reports-more-than-one-version-of-org-apache-commons-l ...
- Luogu 2577[ZJOI2005]午餐 - 动态规划
Solution 啊... 我太菜了唔 不看题解是不可能的, 这辈子都不可能的. 首先一个队伍中排队轮到某个人的时间是递增的, 又要加上吃饭时间, 所以只能使吃饭时间递减, 才能满足最优,于是以吃饭时 ...
- asp.net web 服务器端全局定时执行任务
web网站里面,需要每隔1分钟,执行一个任务,并且一直保持这个定时执行状态,可以用如下一个方法: 1,Global.asax里面的 Application_Start ,发生在第一次请求网站的时 ...
- 品味性能之道<十一>:JAVA中switch和if性能比较
通常而言大家普遍的认知里switch case的效率高于if else.根据我的理解而言switch的查找类似于二叉树,if则是线性查找.按照此逻辑推理对于对比条件数目大于3时switch更优,并且对 ...
- Using The jQuery Migrate Plugin
jQuery( html [, ownerDocument ] )Returns: jQuery Description: Creates DOM elements on the fly from t ...
- Mysql遇到的坑
2018-04-09 这个虽然跟粗心有关,但是Mysql没报错是哪般? select sum(play_count) from tb_user_login where user_id = 61 and ...
- 【Linux】开机自动启动脚本
Linux下(以RedHat为范本)添加开机开机自动启动脚本有两种方式; 本例系统:Linux(CentOS 7.2) 方法一 使用 /etc/rc.d/rc.local,自动启动脚本 #!/bin/ ...
- 20155312 2016-2017-2《Java程序设计》课程总结
20155312 2016-2017-2<Java程序设计>课程总结 每周作业链接汇总 预备作业1:你期望的师生关系是什么? 预备作业2:做中学learning by doing个人感想 ...