题意就是要求第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. php学习笔记:利用gd库生成图片,并实现随机验证码

    说明:一些基本的代码我都进行了注释,这里实现的验证码位数.需要用的字符串都可以再设置.有我的注释,大家应该很容易能看得懂. 基本思路: 1.用mt_rand()随机生成数字确定需要获取的字符串,对字符 ...

  2. 常见的几种RuntimeException

    一般面试中java Exception(runtimeException )是必会被问到的问题 常见的异常列出四五种,是基本要求.更多的....需要注意积累了   常见的几种如下:   NullPoi ...

  3. weblogic 12c下jxls导出excel报错Could not initialize class org.apache.poi.xssf.usermodel.XSSFVMLDrawing

    周一,开发反馈weblogic 12c下jxls导出excel报错,公司环境和UAT环境均报错,看日志如下: 2016-06-08 09:16:55,825 ERROR org.jxls.util.T ...

  4. 挖掘机技术哪家强(c++实现)

    描述:为了用事实说明挖掘机技术到底哪家强,组织一场挖掘机技能大赛.现请你根据比赛结果统计出技术最强的那个学校. 输入:输入在第1行给出不超过105的正整数N,即参赛人数.随后N行,每行给出一位参赛者的 ...

  5. ASP.NET控件绑定数据源

    DataList/GridView/Repeater DataSet表示数据集,其中包含表,约束和表之间的关系.与现有数据源的交互通过DataAdapter来控制. 源代码示例: SqlDataAda ...

  6. 独立博客开张!有关读书、GTD和IT方面的内容将发布在新网站上

    2015年自己建个独立博客http://www.shenlongbin.com,以后与读书.GTD和IT技术有关的主题都放在个人博客中,2015年计划基本制定,请移步到这里. 感谢博客园提供了如此优秀 ...

  7. 浅谈ClickableSpan , 实现TextView文本某一部分文字的点击响应

    超文本:http://www.baidu.com 这么一个效果:一行文本当中 前面显示黑色颜色的“超文本:”,后面显示红色颜色的“http://www.baidu.com” 并且要求红色字体的部分可以 ...

  8. HDFS简单入门

    本文地址:http://www.cnblogs.com/archimedes/p/hadoop-simple.html,转载请注明源地址. 欢迎关注我的个人博客:www.wuyudong.com, 更 ...

  9. 手把手搭建自己的android环境

    最近想学习安卓,不过国内实在被墙的厉害,真是"万里安装只被墙".安装的过程中也出现了几个问题.所以记录下来,免得自己下次再次安装的时候又来重蹈覆辙. 以下的问题也是按照出现的顺序排 ...

  10. NSFileManager文件操作的十个小功能

    NSFileManager文件操作的十个小功能 NSFileManager是一个单列类,也是一个文件管理器.可以通过NSFileManager创建文件夹.创建文件.写文件.读文件内容等等基本功能. 下 ...