POJ 2449 Remmarguts' Date (K短路 A*算法)
Description
"Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. Softly touching his little ducks' head, he told them a story.
"Prince Remmarguts lives in his kingdom UDF – United Delta of Freedom. One day their neighboring country sent them Princess Uyuw on a diplomatic mission."
"Erenow, the princess sent Remmarguts a letter, informing him that she would come to the hall and hold commercial talks with UDF if and only if the prince go and meet her via the K-th shortest path. (in fact, Uyuw does not want to come at all)"
Being interested in the trade development and such a lovely girl, Prince Remmarguts really became enamored. He needs you - the prime minister's help!
DETAILS: UDF's capital consists of N stations. The hall is numbered S, while the station numbered T denotes prince' current place. M muddy directed sideways connect some of the stations. Remmarguts' path to welcome the princess might include the same station twice or more than twice, even it is the station with number S or T. Different paths with same length will be considered disparate.
Input
The first line contains two integer numbers N and M (1 <= N <= 1000, 0 <= M <= 100000). Stations are numbered from 1 to N. Each of the following M lines contains three integer numbers A, B and T (1 <= A, B <= N, 1 <= T <= 100). It shows that there is a directed sideway from A-th station to B-th station with time T.
The last line consists of three integer numbers S, T and K (1 <= S, T <= N, 1 <= K <= 1000).
Output
A single line consisting of a single integer number: the length (time required) to welcome Princess Uyuw using the K-th shortest path. If K-th shortest path does not exist, you should output "-1" (without quotes) instead.
Sample Input
2 2
1 2 5
2 1 4
1 2 2
Sample Output
14
分析:
对于A* ,它通过一个估价函数F(h)来估计图中的当前点p到终点的距离,并由此决定它的搜索方向,当这条路径失败时,它会尝试其他路径。
估价函数 = 当前值+当前位置到终点的距离,即 F(p)=g(p)+h(p),每次扩展估价函数值中最小的一个。对于k短路算法来说,g(p)为当前从s到p所走的路径的长度,h(p)为从点p到点 t 的最短路的长度,则F(p)的意义就是从s按照当前路径走到 p 后要走到终点 t 一共至少要走多远。也就是说我们每次的扩展都是有方向的扩展,这样就可以提高求解速度和降低扩展的状态数目。为了加速计算,h(p)需要在A*搜索之前进行预处理,只要将原图的所有边反向,再从终点 t 做一次单源最短路径就可以得到每个点的h(p)了。
具体步骤:
使用链式前向星来存储图,由于需要预处理所有点到终点T的最短路径,就需要将图G中所有的边反向得到图G',再从终点T做单元最短路径,所以实际上时两张图。
1.将有向图的所有边反向。以t(终点)为源点,求解点T到所有点的最短距离。其目的是要求所有点到终点T的最短距离,用数组dis[i]表示点i到点T的最短距离,其实dis[i]就是后面要用到的h(x)。这一步可以用Dijkstra算法或则SPFA算法。
2.新建一个优先队列,将源点S加入到队列中。
3.从优先队列中弹出f(p)最小的点p,如果点p就是t,则计算t出队的次数,如果当前为T的第k次出队,则当前路径的长度就是S到T的第k短路的长度,算法结束,否则遍历与p相连的所有的边,将扩展出的到p的邻接点信息加入到优先队列。
值得注意的是当S==T时需要计算k+1短路,因为S到T这条距离为0的路不能算在这k短路中,这时只需要将k加一后再求第k短路即可。
代码:dij
#include <stdio.h>
#include <string.h>
#include <queue>
#define M 100010
#define N 1005
const int inf = 0x3f3f3f3f;
using namespace std;
struct E //邻接表建边,to是下个结点,w 是权值 nxt 是下条边的位置
{
int to,w,nxt;
}edge[2*M];
struct data //g 表示起点到当前点的距离,h表终点到当前点的距离
{
int g,h;
int to;
bool operator < (data a) const //优先队列的排序(其实也不能这么讲) 使g+h小的在队首
{
return a.h + a.g < h + g;
}
};
int e,n,src,des,head[N],tail[N],dis[N];//head 是正向边,tail是逆向边 dis是des(终点)到各点的距离
void addedge (int cu,int cv,int cw)
{
edge[e].to = cv;
edge[e].w = cw;
edge[e].nxt = head[cu];
head[cu] = e ++;
edge[e].to = cu;
edge[e].w = cw;
edge[e].nxt = tail[cv];
tail[cv] = e ++;
}
void dij () //dijstra算法求des到各点的距离 用于估价函数h
{
int i,j,k;
int vis[N];
memset (vis,0,sizeof(vis));
memset (dis,0x3f,sizeof(dis));
dis[des] = 0;
for (i = 1;i <= n;i ++)
{
k = -1;
int min = inf;
for (j = 1;j <= n;j ++)
if (!vis[j]&&min > dis[j])
{
k = j;
min = dis[j];
}
// if (k == -1) //因为这里图肯定是连通的 可加可不加
// break;
vis[k] = 1;
for (int u = tail[k];u != -1;u = edge[u].nxt)
{
int v;
v = edge[u].to;
if (!vis[v]&&dis[v] > dis[k] + edge[u].w)
dis[v] = dis[k] + edge[u].w;
}
}
}
int Astar (int k) //A*算法求第k短路
{
int cnt[N];
data cur,nxt; //当前结点 下个结点
priority_queue <data> node;
memset (cnt,0,sizeof(cnt));
cur.to = src; //当前结点初始化 这就不用多说了
cur.g = 0;
cur.h = dis[src];
node.push (cur);
while (!node.empty())
{
cur = node.top ();
node.pop();
cnt[cur.to] ++;
if (cnt[cur.to] > k)//如果当前想拓展的点cnt>k就没必要拓展了
continue; //因为这个点已经是求到k+1短路了 从这个点继续往下搜肯定得到的是大于等于k+1短路的路径
if (cnt[des] == k) //找到第K短路 返回
return cur.g;
for (int u = head[cur.to];u != -1;u = edge[u].nxt) //相连的点入队列
{
int v = edge[u].to;
nxt.to = v;
nxt.g = cur.g + edge[u].w;
nxt.h = dis[v];
node.push (nxt);
}
}
return -1;
}
int main ()
{
int m,u,v,w,k;
while (~scanf ("%d%d",&n,&m))
{
e = 0;
memset (head,-1,sizeof (head));
memset (tail,-1,sizeof (tail));
while (m --)
{
scanf ("%d%d%d",&u,&v,&w);
addedge (u,v,w);
}
scanf ("%d%d%d",&src,&des,&k);
if (src == des) //起点和终点相同时,k要++
k ++;
dij ();
int ans = Astar (k);
printf ("%d\n",ans);
}
return 0;
}
spfa:
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#define MAXN 1005
#define MAXM 500005
#define INF 1000000000
using namespace std;
struct node
{
int v, w, next;
}edge[MAXM], revedge[MAXM];
struct A
{
int f, g, v;
bool operator <(const A a)const {
if(a.f == f) return a.g < g;
return a.f < f;
}
};
int e, vis[MAXN], d[MAXN], q[MAXM * 5];
int head[MAXN], revhead[MAXN];
int n, m, s, t, k;
void init()
{
e = 0;
memset(head, -1, sizeof(head));
memset(revhead, -1, sizeof(revhead));
}
void insert(int x, int y, int w)
{
edge[e].v = y;
edge[e].w = w;
edge[e].next = head[x];
head[x] = e;
revedge[e].v = x;
revedge[e].w = w;
revedge[e].next =revhead[y];
revhead[y] = e++;
}
void spfa(int src)
{
for(int i = 1; i <= n; i++) d[i] = INF;
memset(vis, 0, sizeof(vis));
vis[src] = 0;
int h = 0, t = 1;
q[0] = src;
d[src] = 0;
while(h < t)
{
int u = q[h++];
vis[u] = 0;
for(int i = revhead[u] ; i != -1; i = revedge[i].next)
{
int v = revedge[i].v;
int w = revedge[i].w;
if(d[v] > d[u] + w)
{
d[v] = d[u] + w;
if(!vis[v])
{
q[t++] = v;
vis[v] = 1;
}
}
}
}
}
int Astar(int src, int des)
{
int cnt = 0;
priority_queue<A>Q;
if(src == des) k++;
if(d[src] == INF) return -1;
A t, tt;
t.v = src, t.g = 0, t.f = t.g + d[src];
Q.push(t);
while(!Q.empty())
{
tt = Q.top();
Q.pop();
if(tt.v == des)
{
cnt++;
if(cnt == k) return tt.g;
}
for(int i = head[tt.v]; i != -1; i = edge[i].next)
{
t.v = edge[i].v;
t.g = tt.g + edge[i].w;
t.f = t.g + d[t.v];
Q.push(t);
}
}
return -1;
}
int main()
{
int x, y, w;
while(scanf("%d%d", &n, &m) != EOF)
{
init();
for(int i = 1; i <= m; i++)
{
scanf("%d%d%d", &x, &y, &w);
insert(x, y, w);
}
scanf("%d%d%d", &s, &t, &k);
spfa(t);
printf("%d\n", Astar(s, t));
}
return 0;
}
POJ 2449 Remmarguts' Date (K短路 A*算法)的更多相关文章
- 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短路
题意就是要求第K短的路的长度(S->T). 对于K短路,朴素想法是bfs,使用优先队列从源点s进行bfs,当第K次遍历到T的时候,就是K短路的长度. 但是这种方法效率太低,会扩展出很多状态,所以 ...
- POJ 2449 Remmarguts' Date ( 第 k 短路 && A*算法 )
题意 : 给出一个有向图.求起点 s 到终点 t 的第 k 短路.不存在则输出 -1 #include<stdio.h> #include<string.h> #include ...
- 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 求第K短路
第一道第K短路的题目 QAQ 拿裸的DIJKSTRA + 不断扩展的A* 给2000MS过了 题意:大意是 有N个station 要求从s点到t点 的第k短路 (不过我看题意说的好像是从t到s 可能是 ...
- poj 2449 Remmarguts' Date(K短路,A*算法)
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u013081425/article/details/26729375 http://poj.org/ ...
- 图论(A*算法,K短路) :POJ 2449 Remmarguts' Date
Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 25216 Accepted: 6882 ...
- POJ 2449 Remmarguts' Date(第k短路のA*算法)
Description "Good man never makes girls wait or breaks an appointment!" said the mandarin ...
- POJ 2449 - Remmarguts' Date - [第k短路模板题][优先队列BFS]
题目链接:http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Description "Good m ...
随机推荐
- 配置SSH Forward提升安全性
目标 MacBook ---(SSH)---> BastionServer ---(SSH)---> RemoteServer 说明 BastionServer.RemoteServe ...
- Nginx4大模块——proxy、headers、upstream、stream
一:ngx_http_proxy_module 反向代理( reverse proxy) 方式是指用代理服务器来接受 Internet 上的连接请求, 然后将请求转发给内部网络中的上游服务器, 并将从 ...
- 【刷题】BZOJ 3262 [HNOI2008]GT考试
Description 阿申准备报名参加GT考试,准考证号为N位数X1X2....Xn(0<=Xi<=9),他不希望准考证号上出现不吉利的数字. 他的不吉利数学A1A2...Am(0< ...
- BZOJ 3230 相似子串 | 后缀数组 二分 ST表
BZOJ 3230 相似子串 题面 题解 首先我们要知道询问的两个子串的位置. 先正常跑一遍后缀数组并求出height数组. 对于每一个后缀suffix(i),考虑以i开头的子串有多少是之前没有出现过 ...
- Web项目开发中用到的缓存技术
在WEB开发中用来应付高流量最有效的办法就是用缓存技术,能有效的提高服务器负载性能,用空间换取时间.缓存一般用来 存储频繁访问的数据 临时存储耗时的计算结果 内存缓存减少磁盘IO 使用缓存的2个主要原 ...
- Javascript/jQuery关于JSON或数组集合的几种循环方法
JavaScript遍历JSON或数组集合: /** * 根据json数据生成option树形控件 * 如果有children节点则自动生成树形数据 * @param {JSON} data * @p ...
- 【组合数学】【P5216】DLS采花
Description DLS 有 \(N\) 个花田,每个花田里有 \(a_i\) 朵花. DLS 喜欢稀奇古怪的花田,他希望重新排列花田,然后去采花. 但 DLS 采花又有一个癖好:他会从左往右采 ...
- [网络流]小M的作物
小\(M\)的作物(最小割) 做的第一道网络流,因为一个智障错误调了好久嘤嘤嘤 题目描述 小\(M\)在\(MC\)里开辟了两块巨大的耕地\(A\)和\(B\)(你可以认为容量是无穷),现在,小\(P ...
- Docker 及 nvidia-docker 使用
Docker 基本用法 1. 安装社区版docker-ce 及 nvidia-docker2 插件 通过官网介绍的软件源的方式安装. 如果要安装nvidia-docker,由于其需要与docker-c ...
- Python【datetime】模块
import datetimeprint("==============date类================")#创建一个date对象:datetime.date(year, ...