poj 2449 模板题  A*+spfa

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define mt(a,b) memset(a,b,sizeof(a))
using namespace std;
const int inf=0x3f3f3f3f;
class AStar { ///A*+spfa求第k短路
typedef int typec;///边权的类型
static const int ME=1e5+;///边的个数
static const int MV=1e3+;///点的个数
struct G {
struct E {
int v,next;
typec w;
} e[ME];
int le,head[MV];
void init(int n) {
le=;
for(int i=; i<=n; i++) head[i]=-;
}
void add(int u,int v,typec w) {
e[le].v=v;
e[le].w=w;
e[le].next=head[u];
head[u]=le++;
}
};
class Spfa { ///单源最短路o(k*ME)k~=2
G g;
int n,inque[MV],i,u,v;
typec dist[MV];
bool used[MV];
queue<int> q;
public:
void init(int tn) { ///传入点的个数
n=tn;
g.init(n);
}
void add(int u,int v,typec w) {
g.add(u,v,w);
}
bool solve(int s) { ///传入起点,存在负环返回false
for(i=; i<=n; i++) {
dist[i]=inf;
used[i]=true;
inque[i]=;
}
used[s]=false;
dist[s]=;
inque[s]++;
while(!q.empty()) q.pop();
q.push(s);
while(!q.empty()) {
u=q.front();
q.pop();
used[u]=true;
for(i=g.head[u]; ~i; i=g.e[i].next) {
v=g.e[i].v;
if(dist[v]>dist[u]+g.e[i].w) {
dist[v]=dist[u]+g.e[i].w;
if(used[v]) {
used[v]=false;
q.push(v);
inque[v]++;
if(inque[v]>n) return false;
}
}
}
}
return true;
}
typec getdist(int id) {
return dist[id];
}
} spfa;
struct Q {
int p;
typec g,h;
friend bool operator <(const Q &a,const Q &b) {
return a.g+a.h>b.g+b.h;
}
} now,pre;
priority_queue<Q> q;
int n,cnt[MV];
G g;
typec ans;
public:
void init(int tn) {
n=tn;
g.init(n);
spfa.init(n);
}
void add(int u,int v,typec w) {
g.add(u,v,w);
spfa.add(v,u,w);
}
bool solve(int s,int t,int k) {
if(s==t) k++;
spfa.solve(t);
while (!q.empty()) q.pop();
for(int i=; i<=n; i++) cnt[i]=;
now.p=s;
now.g=;
now.h=;
q.push(now);
while(!q.empty()) {
pre=q.top();
q.pop();
int u=pre.p;
cnt[u]++;
if(cnt[u]==k&&u==t) {
ans=pre.h+pre.g;
return true;
}
if(cnt[u]>k) continue;
for(int i=g.head[u]; ~i; i=g.e[i].next) {
now.h=pre.h+g.e[i].w;
int v=g.e[i].v;
now.g=spfa.getdist(v);
now.p=v;
q.push(now);
}
}
return false;
}
typec getans() {
return ans;
}
} gg;
int main() {
int n,m,u,v,w,s,t,k;
while(~scanf("%d%d",&n,&m)) {
gg.init(n);
while(m--) {
scanf("%d%d%d",&u,&v,&w);
gg.add(u,v,w);
}
scanf("%d%d%d",&s,&t,&k);
if(!gg.solve(s,t,k)) {
puts("-1");
} else {
printf("%d\n",gg.getans());
}
}
return ;
}

第k短路的更多相关文章

  1. POJ 2449 Remmarguts' Date --K短路

    题意就是要求第K短的路的长度(S->T). 对于K短路,朴素想法是bfs,使用优先队列从源点s进行bfs,当第K次遍历到T的时候,就是K短路的长度. 但是这种方法效率太低,会扩展出很多状态,所以 ...

  2. POJ 2449Remmarguts' Date K短路模板 SPFA+A*

    K短路模板,A*+SPFA求K短路.A*中h的求法为在反图中做SPFA,求出到T点的最短路,极为估价函数h(这里不再是估价,而是准确值),然后跑A*,从S点开始(此时为最短路),然后把与S点能达到的点 ...

  3. BZOJ-1975 魔法猪学院 K短路 (A*+SPFA)

    1975: [Sdoi2010]魔法猪学院 Time Limit: 10 Sec Memory Limit: 64 MB Submit: 1323 Solved: 433 [Submit][Statu ...

  4. 【POJ】2449 Remmarguts' Date(k短路)

    http://poj.org/problem?id=2449 不会.. 百度学习.. 恩. k短路不难理解的. 结合了a_star的思想.每动一次进行一次估价,然后找最小的(此时的最短路)然后累计到k ...

  5. poj 2449 Remmarguts' Date K短路+A*

    题目链接:http://poj.org/problem?id=2449 "Good man never makes girls wait or breaks an appointment!& ...

  6. poj 2449(A*求第K短路)

    题目链接:http://poj.org/problem?id=2449 思路:我们可以定义g[x]为源点到当前点的距离,h[x]为当前点到目标节点的最短距离,显然有h[x]<=h*[x](h*[ ...

  7. K短路

    K短路 用dijsktra+A*启发式搜索当点v第K次出堆的时候,这时候求得的路径是k短路.A*算法有一个启发式函数f(p)=g(p)+h(p), 即评估函数=当前值+当前位置到终点的最短距离g(p) ...

  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. bzoj 1975 [Sdoi2010]魔法猪学院(k短路)

    题目描述 iPig在假期来到了传说中的魔法猪学院,开始为期两个月的魔法猪训练.经过了一周理论知识和一周基本魔法的学习之后,iPig对猪世界的世界本原有了很多的了解:众所周知,世界是由元素构成的:元素与 ...

随机推荐

  1. php异常处理示例

    php异常处理使用示例,代码说明了普通错误和致命错误捕获及处理的方法.  代码如下: <?php //禁止错误输出 error_reporting(0); //设置错误处理器 set_error ...

  2. 一,U盘安装 CentOS 6.5 minimal

    U盘安装盘: CentOS-6.5的版本有四个,分别是: 1.CentOS-6.5-i386-netinstall.iso 通过网络安装的,需要联网 2.CentOS-6.5-i386-minimal ...

  3. FireFox Prevent this page from creating addtional dialogs 火狐浏览器 设置 阻止此页面创建更多对话框

    FireFox英文版本老弹出“Prevent this page from creating addtional dialogs”的确认框 FireFox english version alert ...

  4. Ruby使用gets的错误:gets得到的有'\n',需要使用chomp去掉

    gets方法得到的字符串包含一个“\n”回车符,所以我们需要继续使用chomp方法把"\n"回车符去掉

  5. [.NET] 打造防“狼”神器 :任务栏篇

    @微微一笑:本文标题纯属自娱自乐. 隐藏任务栏效果图: 对比 说起来惭愧,上面这个隐藏任务栏功能,只是完成了我一半的预想.本想是可以选择性的隐藏任务栏上的某个TaskBarButton,但是Win7+ ...

  6. sublimeLinter-jshint 配置

    这几天知道sublime3有可以对javascript进行语法检查的文件,折腾了一上午,搞定了. 记录一下步骤: 1.先安装nodejs. 2.npm install jshint -g 3.通过su ...

  7. Collection、Iterator、Set、HashSet

    Collection接口的基本方法 boolean add(Object o) 向集合当中加入一个对象 void clear() 删除集合当中的所有对象 boolean isEmpty() 判断集合是 ...

  8. adb 安装失败

    打开Terminal终端:Ctrl + Alt + T 按顺序执行以下三条命令:                   sudo add-apt-repository ppa:nilarrimogard ...

  9. IOS内存管理「4」- ARC 和垃圾回收机制的基本概念

  10. [转]WinExec、ShellExecute和CreateProcess及返回值判断方式

    [转]WinExec.ShellExecute和CreateProcess及返回值判断方式 http://www.cnblogs.com/ziwuge/archive/2012/03/12/23924 ...