POJ2449 Remmarguts' Date A*算法
题意是让求从st的ed第k短路。。。
考虑A*算法:先把终点到每个点最短路跑出来(注意要建反图),当做估价函数h(u),然后跑A*
每次取出总代价最小的,即g(u)+h(u)最小的进行扩展,注意如果u被取出的次数c[u]>k,就不再进行扩展了。
当ed被取出且c[ed]==k,那么答案就是此时的g(ed)
#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
#define R register int
#define mp make_pair
const int Inf=0x3f3f3f3f,M=,N=;
using namespace std;
inline int g() {
R ret=; register char ch; while(!isdigit(ch=getchar())) ;
do ret=ret*+(ch^); while(isdigit(ch=getchar())); return ret;
}
int n,m,cnt,ccnt,st,ed,k;
int vr[M],w[M],nxt[M],fir[N];
int vv[M],ww[M],nn[M],ff[N],d[N],c[N];
bool vis[N];
inline void add(int u,int v,int ww) {vr[++cnt]=v,w[cnt]=ww,nxt[cnt]=fir[u],fir[u]=cnt;}
inline void add1(int u,int v,int w) {vv[++ccnt]=v,ww[ccnt]=w,nn[ccnt]=ff[u],ff[u]=ccnt;}
inline void dijk() { memset(d,0x3f,sizeof(d));
priority_queue<pair<int,int> > q; q.push(mp(,ed)); d[ed]=;
while(q.size()) {
R u=q.top().second; q.pop(); if(vis[u]) continue;
for(R i=ff[u];i;i=nn[i]) { R v=vv[i];
if(d[v]>d[u]+ww[i]) d[v]=d[u]+ww[i],q.push(mp(-d[v],v));
}
}
}
struct node{ int u,g,h; node() {}
node(int uu,int gg,int hh) {u=uu,g=gg,h=hh;}
bool operator <(const node& y) const {return g+h>y.g+y.h;}
};
inline int Astar() { if(d[st]==Inf) return -;
priority_queue<node> q; q.push(node(st,,d[st]));
while(q.size()) {
register node crt=q.top(); q.pop(); R u=crt.u; ++c[u];
if(c[u]>k) continue; if(u==ed&&c[u]==k) return crt.g;
for(R i=fir[u];i;i=nxt[i]) q.push(node(vr[i],crt.g+w[i],d[vr[i]]));
} return -;
}
signed main() {
n=g(),m=g();
for(R i=,u,v,w;i<=m;++i) u=g(),v=g(),w=g(),add(u,v,w),add1(v,u,w);
st=g(),ed=g(),k=g(); st==ed?++k:k; dijk(); printf("%d\n",Astar());
}
2019.04.27
POJ2449 Remmarguts' Date A*算法的更多相关文章
- [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/ ...
- [poj2449]Remmarguts' Date(K短路模板题,A*算法)
解题关键:k短路模板题,A*算法解决. #include<cstdio> #include<cstring> #include<algorithm> #includ ...
- POJ2449 Remmarguts' Date
"Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. ...
- POJ2449 Remmarguts' Date 第K短路
POJ2449 比较裸的K短路问题 K短路听起来高大上 实际思路并不复杂 首先对终点t到其他所有点求最短路 即为dist[] 然后由起点s 根据当前走过的距离+dist[]进行A*搜索 第k次到达t即 ...
- poj2449 Remmarguts' Date K短路 A*
K短路裸题. #include <algorithm> #include <iostream> #include <cstring> #include <cs ...
- 图论(A*算法,K短路) :POJ 2449 Remmarguts' Date
Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 25216 Accepted: 6882 ...
- poj 2449 Remmarguts' Date(第K短路问题 Dijkstra+A*)
http://poj.org/problem?id=2449 Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Subm ...
- poj 2449 Remmarguts' Date 第k短路 (最短路变形)
Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 33606 Accepted: 9116 ...
随机推荐
- opennebula kvm 创建虚拟机错误
Thu Jul :: : Error executing image transfer script: Error copying localhost.localdomain:/app/openneb ...
- lucene 第二天
Lucene/Solr 第二天 1. 课程计划 Lucene的Field Lucene的索引库维护 lucene的查询 a) Query子对象 b) QueryParser Lucene相关度排序 ...
- Glade编程
一.简介 如果有一种软件能将图形界面的设计及时地展现于开发人员的面前,而且在设计完后能直接看到界面的外观效果,这样就使程序员的主要精力集中于应用程序核心功能的开发上,这就是所谓的可视化编程思想. ...
- (转)不定义JQuery插件,不要说会JQuery
原文地址:http://www.cnblogs.com/xcj26/p/3345556.html 一:导言 有些WEB开发者,会引用一个JQuery类库,然后在网页上写一写$("#" ...
- LibreOJ 6283 数列分块入门 7(区间加区间乘区间求和)
题解:这道题要打一个乘标记一个加标记,两个标记的优先级是乘法高,所以在乘的时候要将加标记同时乘上一个c,当然,对于每个非完整块一定要记得暴力重构整个块,把加标记和乘标记都初始化. 代码如下: #inc ...
- 简单工厂(Simple Factory)模式
工厂模式专门负责将大量有共同接口的类实例化.工厂模式可以动态决定将哪一个类实例化,不必事先知道每次要实例化哪一个类.工厂模式有以下几种形态: 简单工厂(Simple Factory)模式 工厂方法(F ...
- datatables表格行内编辑的实现
Datatables是一款jquery表格插件,它是一个高度灵活的工具,灵活就意味着很多功能需要自己去实现,比如说行内编辑功能. Datatables自己是没有行内编辑功能的,最简单的是通过modal ...
- GraphQL 优势之处
一次查询,搞定需求 举个例子,Book对象有bookTypeId,那我想看对应的bookTypeName,bookType对应的summary咋办? 如果你用RESTful Api ,免不了要定制接口 ...
- 安卓开发时访问google方法
启动浏览器后15秒左右,浏览器的右上角就会出现图标 启用防火墙功能(右上角墙形图标),这时候程序就会去寻找网上代理,从而达到访问GOOGLE的效果,提示如果不访问google网站,可再点击一下关闭防火 ...
- SpringMVC+Hibernate 项目开发之三 (创建SpringMVC项目)
引用(很全面了):http://blog.csdn.net/dhx20022889/article/details/38041039 我只想说默认创建的项目使用的Spring版本可能不是你想要的,可以 ...