[poj2449]Remmarguts' Date(K短路模板题,A*算法)
解题关键:k短路模板题,A*算法解决。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<cmath>
#include<queue>
using namespace std;
typedef long long ll;
const int N=1e3+;
const int M=1e5+;
const int inf=1e9;
struct edge{
int v,w,nxt;
}e1[M],e2[M];
struct node{
int id;///当前节点编号
int f;//f表示经过当前节点的最短路,f=g+h
int g;//g表示S->当前节点的最短路
node(int id=,int f=,int g=):id(id),f(f),g(g){}
bool operator<(const node &a)const{
if(f==a.f) return g>a.g;
return f>a.f;
}
};
bool vis[N];
int tot,head1[N],head2[N],n,m,K;
int dis[N];//dis[i]表示当前点i到终点T的最短路径
void add_edge(int u,int v,int w){
e1[tot].v=v;e1[tot].w=w;e1[tot].nxt=head1[u];head1[u]=tot;
e2[tot].v=u;e2[tot].w=w;e2[tot].nxt=head2[v];head2[v]=tot;
tot++;
}//两个图
void spfa(int S){//更新每个点->n点的最短距离
queue<int>q;
fill(dis,dis+n+,inf);
dis[S]=;
vis[S]=;
q.push(S);
while(!q.empty()){
int x=q.front();q.pop();
vis[x]=;
for(int i=head2[x];~i;i=e2[i].nxt){
int v=e2[i].v,w=e2[i].w;
if(dis[v]>dis[x]+w){
dis[v]=dis[x]+w;
if(!vis[v]){
vis[v]=;
q.push(v);
}
}
}
}
}
int A_Star(int S,int T){
if(S==T) K++;//坑,题目要求必须走,s==t路程可能为0,所以K要加1
priority_queue<node>q;
q.push(node(S,,));
int cnt=;
while(!q.empty()){
node h=q.top();q.pop();
if(h.id==T){
if(++cnt==K){
return h.f;
}
}
for(int i=head1[h.id];~i;i=e1[i].nxt){
q.push(node(e1[i].v,h.g+e1[i].w+dis[e1[i].v],h.g+e1[i].w));//最短路更新k短路
}
}
return -;
}
int main(){
memset(head1,-,sizeof head1);
memset(head2,-,sizeof head2);
scanf("%d%d",&n,&m);
for(int i=,x,y,z;i<=m;i++){
scanf("%d%d%d",&x,&y,&z);
add_edge(x,y,z);
}
int S,T;
scanf("%d%d%d",&S,&T,&K);
spfa(T);//预处理,反向遍历
int ans=A_Star(S,T);
printf("%d\n",ans);
return ;
}
[poj2449]Remmarguts' Date(K短路模板题,A*算法)的更多相关文章
- poj2449 Remmarguts' Date K短路 A*
K短路裸题. #include <algorithm> #include <iostream> #include <cstring> #include <cs ...
- POJ 2449Remmarguts' Date K短路模板 SPFA+A*
K短路模板,A*+SPFA求K短路.A*中h的求法为在反图中做SPFA,求出到T点的最短路,极为估价函数h(这里不再是估价,而是准确值),然后跑A*,从S点开始(此时为最短路),然后把与S点能达到的点 ...
- 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短路模板题][优先队列BFS]
题目链接:http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Description "Good m ...
- poj 2449 Remmarguts' Date K短路+A*
题目链接:http://poj.org/problem?id=2449 "Good man never makes girls wait or breaks an appointment!& ...
- POJ 2449 Remmarguts' Date --K短路
题意就是要求第K短的路的长度(S->T). 对于K短路,朴素想法是bfs,使用优先队列从源点s进行bfs,当第K次遍历到T的时候,就是K短路的长度. 但是这种方法效率太低,会扩展出很多状态,所以 ...
- K短路模板POJ 2449 Remmarguts' Date
Time Limit: 4000MS Memory Limit: 65536K Total Submissions:32863 Accepted: 8953 Description &qu ...
- k短路模板 POJ2449
采用A*算法的k短路模板 #include <iostream> #include <cstdio> #include <cstring> #include < ...
- HDU 2544 最短路(模板题——Floyd算法)
题目: 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你 ...
随机推荐
- poj3252 数位dp
这题不是用10进制储存的,要转化成2进制再计算 dp[i][j][k] i是位数,j是1的个数,k是0的个数 #include<map> #include<set> #in ...
- VMware设置NAT网络及 CentOS 7IP配置
1.打开VMware,选择 编辑, 虚拟网络编辑器 2.默认情况下,VMware8为我们NAT所使用的网卡,选中VMnet8 3.此处设置我们的IP地址,这个随便指定,我这里设置成192.168.2 ...
- uva11806(容斥原理)
11806 - Cheerleaders Time limit: 2.000 seconds In most professional sporting events, cheerleaders pl ...
- 解决Opencv高低版本不兼容问题
目前OpenCV版本已更新到2.4...由此出现了一系列问题,解决如下: 1.cxcore.h等头文件找不到: 法一.将opencv1.0中的各种.h或者.lib文件拷到opencv2.3.1对应in ...
- mendeley 参考文献管理工具
本文由Suzzz原创,发布于http://www.cnblogs.com/Suzzz/p/4044144.html,转载请保留此声明 目录 介绍 功能 运行截图 安装方法 创建 Desktop Ent ...
- js中call apply方法的使用介绍
js call call 方法 请参阅 应用于:Function 对象 要求 版本 5.5 调用一个对象的一个方法,以另一个对象替换当前对象. call([thisObj[,arg1[, arg2[, ...
- 在Linux上利用core dump和GDB调试segfault
时常会遇到段错误(segfault),调试非常费劲,除了单元测试和基本测试外,有些时候是在在线环境下,没有基本开发和测试工具,这就需要调试的技能.以前介绍过使用strace进行系统调试和追踪<l ...
- unix下网络编程之I/O复用(一)
什么是I/O复用? What we need is the capability to tell the kernel that we want to be notified if one or mo ...
- Spring Boot 集成Swagger2生成RESTful API文档
Swagger2可以在写代码的同时生成对应的RESTful API文档,方便开发人员参考,另外Swagger2也提供了强大的页面测试功能来调试每个RESTful API. 使用Spring Boot可 ...
- spark 算子分析
别的不说先上官网: action 这些算子中需要注意: 1.reduce 和 reduceByKey 虽说都有reduce,但是一个是action级别,一个是transformation级别,速度上会 ...