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 < ...
随机推荐
- TCP/IP-UDP
We read the world wrong but say that it deceives us. "我们看错了世界,却说世界欺骗了我们" 参考资料:TCP/IP入门经典 ( ...
- Linux(Debian)上安装Redis教程
-- 第一步下载文件到该目录 cd /usr/local/src wget http:.tar.gz 解压 tar xzf redis.tar.gz -- 第二步编译安装 make make all ...
- jQuery获取select、checkbox、radio的方法总结
在进行网页的前后台交互的时候,经常需要获取单选框(radio).复选框(checkbox)以及下拉列表(select/dropdownlist)选中的值. 总结了一下,方便以后复习查看: 1.获取选中 ...
- python的sorted相关
Python 字典排序 在python里,字典是内置的数据类型,是个无序的存储结构,每一个元素是key-value对: 有关key的解释: sorted(L,key=by_name)中的key即by_ ...
- Angular2案例rebirth开源
Angular2案例rebirth开源 在过去的几年时间里,Angular1.x显然是非常成功的.但由于最初的架构设计和Web标准的快速发展,逐渐的显现出它的滞后和不适应.这些问题包括性能瓶颈.滞后于 ...
- Solr4.8.0源码分析(3)之index的线程池管理
Solr4.8.0源码分析(3)之index的线程池管理 Solr建索引时候是有最大的线程数限制的,它由solrconfig.xml的<maxIndexingThreads>8</m ...
- 汇顶指纹传感器GF919深度解析
前言: 随着指纹识别技术的日益普遍,其在手机上的应用也得到了广泛关注.作为全球第一款Android正面按压指纹识别手机,魅族MX4 Pro所搭载的国产指纹识别系统可谓是赚足了眼球,这就是由汇顶科技提供 ...
- [Delphi]检查URL是否有效的函数
function CheckUrl(url: string): boolean; var hSession, hfile, hRequest: hInternet; dwindex, dwcodele ...
- 教你如何用Qt做透明的窗体,setMask, Opacity
// In this function, we can get the height and width of the current widgetvoid Widget::resizeEvent(Q ...
- POJ2479 Maximum sum(dp)
题目链接. 分析: 用 d1[i] 表示左向右从0到i的最大连续和,d2[i] 表示从右向左, 即从n-1到i 的最大连续和. ans = max(ans, d1[i]+d2[i+1]), i=0,1 ...