POJ 2449 Remmarguts' Date --K短路
题意就是要求第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短路的更多相关文章
- poj 2449 Remmarguts' Date K短路+A*
题目链接:http://poj.org/problem?id=2449 "Good man never makes girls wait or breaks an appointment!& ...
- POJ 2449 Remmarguts' Date (K短路 A*算法)
题目链接 Description "Good man never makes girls wait or breaks an appointment!" said the mand ...
- 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 http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Total Subm ...
- POJ 2449 - Remmarguts' Date - [第k短路模板题][优先队列BFS]
题目链接:http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Description "Good m ...
- 图论(A*算法,K短路) :POJ 2449 Remmarguts' Date
Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 25216 Accepted: 6882 ...
- poj 2449 Remmarguts' Date 第k短路 (最短路变形)
Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 33606 Accepted: 9116 ...
- poj 2449 Remmarguts' Date(K短路,A*算法)
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u013081425/article/details/26729375 http://poj.org/ ...
- K短路模板POJ 2449 Remmarguts' Date
Time Limit: 4000MS Memory Limit: 65536K Total Submissions:32863 Accepted: 8953 Description &qu ...
随机推荐
- 【iOS】Quartz2D练习-动态改变属性值
一.通过slider控制圆的缩放 1.实现过程 新建一个项目,新建一个继承自UIview的类,并和storyboard中自定义的view进行关联.代码示例:SLViewController.m文件 # ...
- java File.mkdirs和mkdir区别
File f = new File("e://xxx//yyy"); System.out.println(f.mkdirs());//生成所有目录,一般来说,这个方法稳健性更好, ...
- 调用存储过程从EntityFramework
Prerequisites The prerequisite for running these examples are the following sample tables with test ...
- java多线程系列6-阻塞队列
这篇文章将使用经典的生产者消费者的例子来进一步巩固java多线程通信,介绍使用阻塞队列来简化程序 下面是一个经典的生产者消费者的例子: 假设使用缓冲区存储整数,缓冲区的大小是受限制的.缓冲区提供wri ...
- 【读书笔记】iOS网络-运行循环
运行循环是由类NSRunLoop表示的,有些线程可以让操作系统唤醒睡眠的线程以管理到来的事件,而运行循环则是这些线程的基本组件.运行循环是这样一种循环,可以在一个周期内调度任务并处理到来的事件.iOS ...
- 基础学习day10--异常、包
一.异常 1.1.异常定义 异常:--不正常,程序在运行时出现不正常情况 异常由来:其实也是现实生活中一个具体的事物,马可以通过JAVA的类的形式表现描述,并封装成类. Java对不正常情况描述后的, ...
- 基础学习day05---面向对象一类,对象、封装
一.面向对象 1.1.面向对象的概念 很经典的一句话----------万物皆对象 定义:面向对象一相对面向过程而言的 面向对象和面向过程都是一种思想 面向过程-- ...
- IOS UICollectionView基础+UICollectionViewFlowLayout基础
UICollectionView在众多控件中也算是比较常用的了,像淘宝在浏览宝贝时采用的就是UICollectionView,对于UICollectionView->UICollectionVi ...
- [android] 手机卫士设置向导页面
设置向导页面,通过SharedPreferences来判断是否已经设置过了,跳转到不同的页面 自定义样式 在res/values/styles.xml中 添加节点<style name=””&g ...
- javascript之工厂方式定义对象
每一个函数对象都有一个length属性,表示该函数期望接收的参数个数. <html> <head> <script type="text/javascript& ...