题目链接

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*算法)的更多相关文章

  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短路

    题意就是要求第K短的路的长度(S->T). 对于K短路,朴素想法是bfs,使用优先队列从源点s进行bfs,当第K次遍历到T的时候,就是K短路的长度. 但是这种方法效率太低,会扩展出很多状态,所以 ...

  3. POJ 2449 Remmarguts' Date ( 第 k 短路 && A*算法 )

    题意 : 给出一个有向图.求起点 s 到终点 t 的第 k 短路.不存在则输出 -1 #include<stdio.h> #include<string.h> #include ...

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

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

  5. POJ 2449 求第K短路

    第一道第K短路的题目 QAQ 拿裸的DIJKSTRA + 不断扩展的A* 给2000MS过了 题意:大意是 有N个station 要求从s点到t点 的第k短路 (不过我看题意说的好像是从t到s 可能是 ...

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

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

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

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

  8. POJ 2449 Remmarguts' Date(第k短路のA*算法)

    Description "Good man never makes girls wait or breaks an appointment!" said the mandarin ...

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

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

随机推荐

  1. Nginx和Apache区别

    Nginx VS  Apache 相同点: 1.都是HTTP服务器软件 2.功能上都采用模块化结构设计 3.都支持通用的语言接口,如PHP.Perl.Python等 4.支持正向.反向代理.虚拟主机. ...

  2. 安装iproute2

    yum install -y gcc bison flex #tar -xzf  iproute2-3.17.0.tar.gz #cd iproute2-3.17.0#sed -i '/^TARGET ...

  3. poj 1523 SPF(双连通分量割点模板)

    题目链接:http://poj.org/problem?id=1523 题意:给出无向图的若干条边,求割点以及各个删掉其中一个割点后将图分为几块. 题目分析:割点用tarjan算法求出来,对于每个割点 ...

  4. SQL中MAX()和MIN()函数的使用(比较字符串的大小)

    在SQL数据库中,最大/最小值函数—MAX()/MIN()是经常要用到的,下面就将为您分别介绍MAX()函数和MIN()函数的使用,供您参考,希望对您学习SQL数据库能有些帮助. 当需要了解一列中的最 ...

  5. background的使用

    先简单的介绍颜色表示法 一共有三种:单词,rgb表示法,十六进制表示法 rgb:红色 绿色 蓝色 三原色 光学显示器,每个像素都是有三原色的发光原件组成的,靠明亮度不同调成不同的颜色的 有逗号隔开,r ...

  6. Mysql innodb 间隙锁 (转)

    MySQL InnoDB支持三种行锁定方式: 行锁(Record Lock):锁直接加在索引记录上面. 间隙锁(Gap Lock):锁加在不存在的空闲空间,可以是两个索引记录之间,也可能是第一个索引记 ...

  7. python自动化之时间

    cxz##############################现在时间######################### import time time.time() ############# ...

  8. oracle +plsql装完省略号不能点

    1.如图 2.复制 TNS 服务名 3.复制到 登录框的 Database ,输入用户名密码,点OK..可以进去了,省略号变成可点击状态

  9. NOIP2018前的一些计划&记录(日更)

    先空着,等停课了再开始写. 诸位好,我是yyb.现在显然已经不再是高一的小蒟蒻了,已经升级为了高二的菜鸡了 现在已经不能再每天划划水切切题了,毕竟......已经高二了,所有的机会从高一的两倍全部除了 ...

  10. docker虚拟机动态扩展内存

    需求:将一台docker虚拟机的内存从6G扩展到8G. 关于资源限制,docker使用的是cgroup,这里就不细说原理了,只记录一下操作方法. 1.先找到容器ID # docker ps 2.进入对 ...