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 < ...
随机推荐
- html form <label>标签基础语法结构与使用案例教程(转载)
在表单布局中会遇到label标签的使用,label没有任何样式效果,有触发对应表单控件功能.比如我们点击单选按钮或多选框前文字对应选项就能被选中,这个就是对文字加了<label>标签实现. ...
- django 序列化json问题
from django.core import serializers @login_required def ajax_get_data(request): json_data = serializ ...
- Codeforces 556A Case of the Zeros and Ones(消除01)
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description Andr ...
- linux内核移植 I
根据tx2440的文档, 目标也比较简单, 先编译成功, 再烧录, 根文件系统, busybox 这些. A. 准备 1. 解压tar, 修改根Makefile ARCH ?= arm CROSS_C ...
- codevs 1198 国王游戏
传送门 题目描述 Description 恰逢 H 国国庆,国王邀请 n 位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右手上面分别写下一个整数,国王自己也在左.右手上各写一个整数.然后,让这 n位 ...
- poj 2031Building a Space Station
http://poj.org/problem?id=2031 #include<cstdio> #include<cstring> #include<cmath> ...
- Ganglia + Nagios 初步实践
参考文档: http://www.bubuko.com/infodetail-715636.html http://www.linuxidc.com/Linux/2014-01/95804p2.htm ...
- C51指针类型和存储区的关系详解
一.存储类型与存储区关系 data ---> 可寻址片内ram bdata ---> 可位寻址的片内ram idata ---> 可寻址片内ram ...
- android Base64加密解密
// 加密传入的数据是byte类型的,并非使用decode方法将原始数据转二进制,String类型的数据 使用 str.getBytes()即可 String str = "Hello!&q ...
- BZOJ1528: [POI2005]sam-Toy Cars
1528: [POI2005]sam-Toy Cars Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 282 Solved: 129[Submit][S ...