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 ...
随机推荐
- C语言以字符形式读写文件
一.字符读取函数 fgetc (一).函数介绍 fgetc 是 file get char 的缩写,意思是从指定的文件中读取一个字符.函数原型为: int fgetc(FILE* fp) fp 为文件 ...
- 微信小程序 功能函数picker-view的弹出模态
<view class="list"> <form bindsubmit="formSubmit"> <view class=&q ...
- Python动态规划求解最长递增子序列(LIS)
原始代码错误,移步博客查看O(N^2)及优化的O(N*logN)的实现:每天一道编程题--最长递增子序列
- ZooKeeper-基础介绍
What is ZooKeeper? ZooKeeper为分布式应用设计的高性能(使用在大的分布式系统).高可用(防止单点失败).严格地有序访问(客户端可以实现复杂的同步原语)的协同服务. ZooKe ...
- bootstrap栅格系统错位问题
在项目中div可以设置属性class=“col-size-x” //size取值为xs,sm,md,lg:x取值为1-12 可以让此div占据本行的 x/12 .col-xs- 超小屏幕 手机 (&l ...
- Ifter Party LightOJ - 1014(水题)
题意:有C个人,给P个食物,每人吃Q个,剩L个.然后给你P和L(Q>L),让你求Q的可能情况,如果有多种可能,从小到大输出:如果不存在,输出impossible 就是求写出公式 遍历c求P-L的 ...
- 怎么解决Xing欲
怎么解决Xing欲 来源:微信号 王路在隐身 这是知乎上的一道问题.原题叫<和尚怎么解决性欲>. 本来由出家人回答更合适,但估计出家人一般不太愿意回答. 我看了几十个答案,几乎都是在调侃出 ...
- python中文档IO的读写
首先明确一点,现代计算机的操作系统都不允许程序直接操作磁盘的,所以读取文件和写入文件都是有操作系统提供的提供的接口完成的,每一种编程语言都会把操作系统提供的低级C接口封装起来方便使用,Python也不 ...
- [洛谷P4705]玩游戏
题目大意:对于每个$k\in[1,t]$,求:$$\dfrac{\sum\limits_{i=1}^n\sum\limits_{j=1}^m(a_i+b_j)^k}{nm}$$$n,m,t\leqsl ...
- 记一个鼠标略过时候的css动画
.circle{ width: 15px; height: 15px; background: rgba(0,0,0,0.7); border-radius: 50%; display: table- ...