POJ2449
#include<stdio.h>
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
#define inf 99999999
#define N 1100
typedef struct nnn
{
int F,G,s;
friend bool operator<(nnn a,nnn b)
{
return a.F>b.F;
}
}PATH;
typedef struct nn
{
int v,w;
}node;
vector<node>map[N],tmap[N];
int H[N];
void SPFA(int s)
{
queue<int>q;
int inq[N]={0};
q.push(s); inq[s]=1; H[s]=0;
while(!q.empty())
{
s=q.front(); q.pop(); inq[s]=0;
int m=tmap[s].size();
for(int i=0;i<m;i++)
{
int j=tmap[s][i].v;
if(H[j]>tmap[s][i].w+H[s])
{
H[j]=tmap[s][i].w+H[s];
if(!inq[j])
inq[j]=1,q.push(j);
}
}
}
}
int Astar(int st,int end,int K)
{
priority_queue<PATH>q;
PATH p,tp;
int k[N]={0};
SPFA(end);
if(H[st]==inf)return -1;
p.s=st; p.G=0; p.F=H[st];
q.push(p);
while(!q.empty())
{
p=q.top(); q.pop();
k[p.s]++;
if(k[p.s]>K)continue;//每个点最多走K次,超过K条路不必走
if(p.s==end&&k[end]==K) return p.F;
int m=map[p.s].size();
for(int i=0;i<m;i++)
{
int j=map[p.s][i].v;
if(H[j]!=inf)//表明当前点不能通向终点,就不用加入队列
{
tp.G=p.G+map[p.s][i].w;
tp.F=H[j]+tp.G;
tp.s=j;
q.push(tp);
}
}
}
return -1;
}
int main()
{
int n,m,S,T,K,a,b,t;
node p;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
map[i].clear(); tmap[i].clear(); H[i]=inf;
} while(m--)
{
scanf("%d%d%d",&a,&b,&t);
p.v=b; p.w=t; map[a].push_back(p);
p.v=a; tmap[b].push_back(p);//反建一个地图求H
}
scanf("%d%d%d",&S,&T,&K);
if(S==T)K++;
printf("%d\n",Astar(S,T,K));
}
POJ2449
图论模板题,题意是给你起点A和终点B,求A到B的第K条最短路。
输入M,N(M个点,N条边)
输入S,E,T(S起始点,E终点,T代表第T条最短路)
输出第T条最短路的长度,如果没有,则输出-1.
思路:由单源最短路径可求得各点到E的距离,利用估价函数=当前长短的实际代价(起点到点K)+估计代价(点K到终点)
可得Astar优先队列保存节点求出最终结果,纯模板题。
另外题目用的Astar算法即是启发式搜索,类似与穷举,但在穷举的途中进行大量优化(因为前期已经做了单源路径最短化处理),减少不必要的搜索,利用起点,中间点,终点之间的估价关系进行求解。
算法与实现上的模板没用,有点复杂。
POJ2449的更多相关文章
- POJ2449 Remmarguts' Date 第K短路
POJ2449 比较裸的K短路问题 K短路听起来高大上 实际思路并不复杂 首先对终点t到其他所有点求最短路 即为dist[] 然后由起点s 根据当前走过的距离+dist[]进行A*搜索 第k次到达t即 ...
- A*模板(求K短路)(POJ2449)
A*是bfs的优化,IDA*是dfs的优化 A*算法: 为启发式算法中很重要的一种,被广泛应用在最优路径求解和一些策略设计的问题中.而A*算法最为核心的部分,就在于它的一个估值函数的设计上: f(n) ...
- 【poj2449】 Remmarguts' Date
http://poj.org/problem?id=2449 (题目链接) 题意 求有向图K短路. Solution A*.g(x)为当前节点到起点的步数,h(x)为当前节点到终点的最短距离(也就是估 ...
- POJ2449 (k短路)
#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> # ...
- poj3255,poj2449
这里介绍怎么求k短路 A*搜索 估价函数f[i]=g[i]+h[i]; 在这里g[i]表示到达点i当前路径长,h[i]表示点i到达终点的最短距离 在搜索中,每次都取队列估价函数值最小的点,然后把它所能 ...
- A_star poj2449 k短路
赛后填坑系列QAQ 贴代码呀 #include<iostream> #include<algorithm> #include<cstdio> #include< ...
- [poj2449]Remmarguts' Date(spfa+A*)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Remmarguts' Date Time Limit: 4000MS Mem ...
- poj2449 Remmarguts' Date【A*算法】
转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4303855.html ---by 墨染之樱花 [题目链接]:http://poj.org/ ...
- k短路模板 POJ2449
采用A*算法的k短路模板 #include <iostream> #include <cstdio> #include <cstring> #include < ...
随机推荐
- 关于-webkit-tap-highlight-color的一些事儿
这个属性只用于iOS (iPhone和iPad).当你点击一个链接或者通过Javascript定义的可点击元素的时候,它就会出现一个半透明的灰色背景.要重设这个表现,你可以设置-webkit-tap- ...
- mvc 的 OutputCache
有人问.有没有类似asp.net web form 一样的那种标头 缓存 [OutputCache(Duration=1000)]public async Task<ActionResult&g ...
- [转]Windows平台下安装Hadoop
1.安装JDK1.6或更高版本 官网下载JDK,安装时注意,最好不要安装到带有空格的路径名下,例如:Programe Files,否则在配置Hadoop的配置文件时会找不到JDK(按相关说法,配置文件 ...
- 手工走一次OPENSTACK安装,掉一层皮啊
掉皮也是值得的,对OS的了解慢慢加深. 最近加入CS的Q群也学到不少.
- windows环境下VS2013编译openSSL
openssl版本:1.0.2h 编译器:MSVC (VS2013) 需要准备工具:perl. windows环境的perl下载请戳这里:http://www.activestate.com/acti ...
- 【HDOJ】1348 Wall
计算几何-凸包模板题目,Graham算法解. /* 1348 */ #include <iostream> #include <cstdio> #include <cst ...
- OI记忆口诀
splay_rotate: inline void rotate(splay_node *x){ splay_node *y,*z;int d1,d2; d1=get_parent(x,y);//三个 ...
- WCF默认实例的解读
一:图片 IService1.cs是定义的接口,包含对Service.方法和方法用的类的声明的声明 Service1.cs是对接口的实现,包含实现的方法 代码注释: using System; usi ...
- POJ_3356——最短编辑距离,动态规划
Description Let x and y be two strings over some finite alphabet A. We would like to transform x int ...
- linux下安装mysql-community后起不来
wget http://repo.mysql.com/mysql-community-release-el6-5.noarch.rpmrpm -ivh http://repo.mysql.com/my ...