题意是让求从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*算法的更多相关文章

  1. [poj2449]Remmarguts' Date(spfa+A*)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Remmarguts' Date Time Limit: 4000MS   Mem ...

  2. poj2449 Remmarguts' Date【A*算法】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4303855.html   ---by 墨染之樱花 [题目链接]:http://poj.org/ ...

  3. [poj2449]Remmarguts' Date(K短路模板题,A*算法)

    解题关键:k短路模板题,A*算法解决. #include<cstdio> #include<cstring> #include<algorithm> #includ ...

  4. POJ2449 Remmarguts' Date

    "Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. ...

  5. POJ2449 Remmarguts' Date 第K短路

    POJ2449 比较裸的K短路问题 K短路听起来高大上 实际思路并不复杂 首先对终点t到其他所有点求最短路 即为dist[] 然后由起点s 根据当前走过的距离+dist[]进行A*搜索 第k次到达t即 ...

  6. poj2449 Remmarguts' Date K短路 A*

    K短路裸题. #include <algorithm> #include <iostream> #include <cstring> #include <cs ...

  7. 图论(A*算法,K短路) :POJ 2449 Remmarguts' Date

    Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 25216   Accepted: 6882 ...

  8. poj 2449 Remmarguts' Date(第K短路问题 Dijkstra+A*)

    http://poj.org/problem?id=2449 Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Subm ...

  9. poj 2449 Remmarguts' Date 第k短路 (最短路变形)

    Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 33606   Accepted: 9116 ...

随机推荐

  1. id 和 instancetype 方法的区别

    首先明确 id 和 instancetype 都是万能指针,都能指向一个对象:(instancetype == id == 万能指针 == 指向一个对象) 主要区别亮点: 1. id 在编译时候不能判 ...

  2. Python学习笔记_Python向Excel写入数据

    实验环境 1.OS:Win 10 64位 2.Python 3.7 3.如果没有安装xlwt库,则安装:pip install xlwt 下面是从网上找到的一段代码,网上这段代码,看首行注释行,是在L ...

  3. 类型或命名空间名称“Interop”在类或命名空间“Microsoft.Office”中不存在(是否缺少程序集引用?)

    准备用C#编写Web程序,生成Excel报表,在使用下面语句时报错. using Microsoft.Office.Interop.Excel; 报错信息:类型或命名空间名称“Interop”在类或命 ...

  4. 2.Hive的几种常见的数据导入方式

    好久没写Hive的那些事了,今天开始写点吧.今天的话题是总结Hive的几种常见的数据导入方式,我总结为四种:(1).从本地文件系统中导入数据到Hive表:(2).从HDFS上导入数据到Hive表:(3 ...

  5. Web API集成Azure AD认证

    1.声明的介绍 基于角色的授权管理,适用于角色变化不大,并且用户权限不会频繁更改的场景. 在更复杂的环境下,仅仅通过给用户分配角色并不能有效地控制用户访问权限. 基于声明的授权有许多好处,它使认证和授 ...

  6. MVC5应用程序生命周期lifecycle

  7. 软件工程:Java实现WC.exe基本功能

    项目相关要求 GitHub地址:https://github.com/3216004716/WC 实现一个统计程序,它能正确统计程序文件中的字符数.单词数.行数,以及还具备其他扩展功能,并能够快速地处 ...

  8. Android自动提示控件:AutoCompleteTextView和MultiAutoCompleteTextView

    在输入框中输入我们想要输入的信息就会出现其他与其相关的提示信息,这种效果在Android中是用AutoCompleteTextView实现的. 一.AutoCompleteTextView:单一匹配 ...

  9. java中公用类型Car必须在它自己的文件中定义

    熟悉java的过程中发现了一些小问题,定义的类Car老是提示必须在它自己的文件中定义.自己想了想试试把Car继承的类Vehicle中的public换到Car类中,结果发现输出问题很大.它只显示了一个输 ...

  10. [other] 代码量代码复杂度统计-lizard

    [other] 代码量代码复杂度统计-lizard lizard的可以用来统计下面的一些数据 不包含代码注释的代码行数 CCN 代码的复杂度,也就是分支复杂度 token的个数(关键字,标示符,常量, ...