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 ...
随机推荐
- Linux服务器有什么优势?
为您的企业选择服务器时,您可以选择几种不同的选项.虽然许多公司使用基于Windows的服务器,但选择Linux服务器可能是您最好的选择.为什么Linux服务器比其他服务器更好?以下是使用Linux服务 ...
- 洛谷3119 [USACO15JAN]草鉴定Grass Cownoisseur
原题链接 显然一个强连通分量里所有草场都可以走到,所以先用\(tarjan\)找强连通并缩点. 对于缩点后的\(DAG\),先复制一张新图出来,然后对于原图中的每条边的终点向新图中该边对应的那条边的起 ...
- Latex基本用法
空格 需要使用 \qquad,\quad,\,应该是占位符和变量之间需要有{}相隔. $$ C_{1} \qquad {C_2} $$ $$ C_{1} \quad {C_2} $$ $$ C_{1} ...
- Spring MVC 注解类型
Spring 2.5 引入了注解 基于注解的控制器的优势 1. 一个控制器类可以处理多个动作,而一个实现了 Controller 接口的控制器只能处理一个动作 2. 基于注解的控制器的请求映射不需要存 ...
- [Python] Python教程
http://www.runoob.com/python/python-tutorial.html
- Spring Environment(二)源码分析
Spring Environment(二)源码分析 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) Spring Envi ...
- ApplicationContext(四)BeanFactory 功能扩展
ApplicationContext(四)BeanFactory 功能扩展 上节我们提到容器刷新的第二步初始化 BeanFactory 工厂并解析配制文件,但此时 BeanFactory 的功能还很简 ...
- MySQL中varchar与char区别
MySQL中varchar与char区别(转) MySQL中varchar最大长度是多少? 一. varchar存储规则: 4.0版本以下,varchar(20),指的是20字节,如果存放UTF8汉字 ...
- Squares of a Sorted Array LT977
Given an array of integers A sorted in non-decreasing order, return an array of the squares of each ...
- sqlserver 添加服务器链接 跨服务器访问数据库
转载地址1:https://www.cnblogs.com/wanshutao/p/4137994.html //创建服务器链接 转载地址2:https://www.cnblogs.com/xulel ...