题意就是要求第K短的路的长度(S->T)。

对于K短路,朴素想法是bfs,使用优先队列从源点s进行bfs,当第K次遍历到T的时候,就是K短路的长度。

但是这种方法效率太低,会扩展出很多状态,所以考虑用启发式搜索A*算法。

估价函数 = 当前值 + 当前位置到终点的距离,即F(p) = G(p) + H(p)。

G(p): 当前从S到p所走的路径距离

H(p): 当前点p到终点T的最短路径距离   ---可以先将整个图边方向取反然后以T为源点求个最短路,用SPFA提速

F(p): 从S按照当前路径走到p然后走到T一共至少走多远

所以我们结合SPFA+A*可以解决。

注意:当S==T时,需要计算第K+1短路,因为从S->T这条长度为0的路径不能算在内。

还有,SPFA处判了一下负环。SPFA算法中,如果某个点出队次数大于n,说明此处存在负环。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <functional>
#define Mod 1000000007
using namespace std;
#define N 1007 struct node
{
int v;
int g,f; //f = g+h
bool operator < (const node &a)const
{
if(a.f == f)
return a.g < g;
return a.f < f;
}
}; struct Edge
{
int v,w,next;
}G[*N],G2[*N]; int head[*N],head2[*N];
int vis[N],dis[N];
int out[N];
int n,m,K,S,T,tot,tot2; void addedge(Edge *G,int& tot,int *head,int u,int v,int w)
{
G[tot].v = v;
G[tot].w = w;
G[tot].next = head[u];
head[u] = tot++;
} int SPFA(int s,int head[N],Edge G[N],int dis[N])
{
int i;
queue<int> que;
for(i=;i<=n;i++)
dis[i] = Mod;
memset(vis,,sizeof(vis));
memset(out,,sizeof(out));
que.push(s);
vis[s] = ;
dis[s] = ;
while(!que.empty())
{
int now = que.front();
que.pop();
vis[now] = ;
out[now]++;
if(out[now] > n)
return ;
for(int k=head[now];k!=-;k=G[k].next)
{
if(dis[G[k].v] > dis[now] + G[k].w)
{
dis[G[k].v] = dis[now] + G[k].w;
if(!vis[G[k].v])
{
vis[G[k].v] = ;
que.push(G[k].v);
}
}
}
}
return ;
} int A_Star(int head[N],Edge G[N],int dis[N])
{
node tmp,now;
int cnt = ;
priority_queue<node> que;
if(S == T)
K++;
if(dis[S] == Mod)
return -;
tmp.v = S;
tmp.g = ;
tmp.f = tmp.g+dis[S];
que.push(tmp);
while(!que.empty())
{
tmp = que.top();
que.pop();
if(tmp.v == T)
cnt++;
if(cnt == K)
return tmp.g;
for(int i=head[tmp.v];i!=-;i=G[i].next)
{
now.v = G[i].v;
now.g = tmp.g + G[i].w;
now.f = now.g + dis[now.v];
que.push(now);
}
}
return -;
} int main()
{
int i,j,u,v,w;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(head,-,sizeof(head));
memset(head2,-,sizeof(head2));
tot = tot2 = ;
for(i=;i<m;i++)
{
scanf("%d%d%d",&u,&v,&w);
addedge(G,tot,head,u,v,w); //原图
addedge(G2,tot2,head2,v,u,w); //反图
}
scanf("%d%d%d",&S,&T,&K);
if(SPFA(T,head2,G2,dis))
{
int k_len = A_Star(head,G,dis);
printf("%d\n",k_len);
}
else
puts("-1");
}
return ;
}

POJ 2449 Remmarguts' Date --K短路的更多相关文章

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

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

  2. POJ 2449 Remmarguts' Date (K短路 A*算法)

    题目链接 Description "Good man never makes girls wait or breaks an appointment!" said the mand ...

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

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

  4. poj 2449 Remmarguts' Date (k短路模板)

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

  5. POJ 2449 - Remmarguts' Date - [第k短路模板题][优先队列BFS]

    题目链接:http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Description "Good m ...

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

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

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

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

  8. poj 2449 Remmarguts' Date(K短路,A*算法)

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u013081425/article/details/26729375 http://poj.org/ ...

  9. K短路模板POJ 2449 Remmarguts' Date

      Time Limit: 4000MS   Memory Limit: 65536K Total Submissions:32863   Accepted: 8953 Description &qu ...

随机推荐

  1. python面向对象(一),Day6

    connfigparser模块 xml模块 shutil模块以及压缩解压 subprocess模块 面向对象(上) 类和对象 onfigParser 用于对特定的配置进行操作,当前模块的名称在 pyt ...

  2. Verilog学习笔记设计和验证篇(三)...............同步有限状态机的指导原则

    因为大多数的FPGA内部的触发器数目相当多,又加上独热码状态机(one hot code machine)的译码逻辑最为简单,所以在FPGA实现状态机时,往往采用独热码状态机(即每个状态只有一个寄存器 ...

  3. 搭建spring的开发环境

    在eclipse中新建web项目,集成spring开发环境,把集成spring的过程描述如下, 1.从spring官网下载spring的jar包,我这里是spring4.1,下载的文件中包含了源码及文 ...

  4. Hibernate中的一级缓存、二级缓存和懒加载(转)

    1.为什么使用缓存 hibernate使用缓存减少对数据库的访问次数,从而提升hibernate的执行效率.hibernate中有两种类型的缓存:一级缓存和二级缓存. 2.一级缓存 Hibenate中 ...

  5. .Net开发人员有趣的Podcast

            如果你是一个.Net开发人员,那么一定不要错过这些Podcasts,它们可是即可以了解IT业态,又可以锻炼英文听力.有采访很多开源人员,涉及项目等等.先尽力听他们说什么,然后再看Tra ...

  6. Android 手机号码格式验证

    package com.app.android01 ; import android.app.Activity; import android.os.Bundle; import android.te ...

  7. JavaWeb开发必过关-Servlet学习(一)

    一.什么是Servlet servlet其实是一个小程序,它是运行在服务器上的,一个servlet就是一个Java类,可以通过"请求-响应"编程模型来访问这个驻留在服务器内存的Se ...

  8. 更轻量的 View Controllers

    iew controllers 通常是 iOS 项目中最大的文件,并且它们包含了许多不必要的代码.所以 View controllers 中的代码几乎总是复用率最低的.我们将会看到给 view con ...

  9. CoreAnimation-08-CATransition

    概述 简介 CATransition又称转场动画,是CAAnimation的子类,可以直接使用 转场动画主要用于为图层提供移入/移出屏幕的动画效果 转场动画常见的应用是UINavigationCont ...

  10. android 进程/线程管理(四)续----消息机制的思考(自定义消息机制)

    继续分析handler 和looper 先看看handler的 public void dispatchMessage(Message msg) { if (msg.callback != null) ...