POJ 2449 Remmarguts' Date【SPFA】【A*】
来源:互联网 发布:淘宝抓绒衣 编辑:程序博客网 时间:2024/05/29 14:23
Remmarguts' Date
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 21978Accepted: 5982
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 21 2 5
2 1 4
1 2 2
Sample Output
14Source
POJ Monthly,Zeyuan Zhu
题目大意:公主要求王子通过第k短的路径去找她。给出了N个点,M条单向边的图。也给出了
起点s(王子所在的点)、终点t(公主所在的点)和k。问:K短路是多少。
思路:第一次做K短路的题目。用的A*+SPFA来做的。下边简单说下这个算法。
使用链式前向星存储图。安装下边步骤来做。
(1)将有向图的所有边正向、反向分别存入两个不同的边集(Edges,Edges1)中。用反向边集,
以所求终点t为源点,利用SPFA或Dijkstra求解出所有点到t的最短路径,用Dist[i]数组来表示点i
到点t的最短距离。
(2)建立一个优先队列,将源点s加入到队列中。
(3)从优先队列中取出最小的点p,如果点p == t,则计算t出队的次数。如果当前路径长度就是s
到t的第k短路长度,算法结束。否则遍历与p相连的所有的边,将扩展出的到p的邻接点信息加入
到优先队列中取。
注意:当s == t的时候,需要计算第k+1短路。因为s到t这条距离为0的路不能算是这k短路里边,
当s == t的时候,只需要将k = k+1后再求第k短路就可以了。这也是这道题的关键点之一。
#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<queue>using namespace std;const int MAXN = 1100;const int MAXM = 110000;const int INF = 0xffffff0;struct EdgeNode{ int to; int w; int next;}Edges[MAXM],Edges1[MAXM];int Head[MAXN],Head1[MAXN];struct Node{ int to; int g,f; bool operator < (const Node &r) const { if(r.f == f) return r.g < g; return r.f < f; }};int vis[MAXN],Dist[MAXN];int A_Star(int start,int end,int N,int k){ Node e,ne; int Cnt = 0; priority_queue<Node> que; if(start == end) k++; if(Dist[start] == INF) return -1; e.to = start; e.g = 0; e.f = e.g + Dist[e.to]; que.push(e); while( !que.empty() ) { e = que.top(); que.pop(); if(e.to == end) Cnt++; if(Cnt == k) return e.g; for(int i = Head[e.to]; i != -1; i = Edges[i].next) { ne.to = Edges[i].to; ne.g = e.g + Edges[i].w; ne.f = ne.g + Dist[ne.to]; que.push(ne); } } return -1;}void SPFA(int s,int N){ for(int i = 0; i <= N; ++i) Dist[i] = INF; memset(vis,0,sizeof(vis)); vis[s] = 1; Dist[s] = 0; queue<int> Q; Q.push(s); while( !Q.empty() ) { int u = Q.front(); Q.pop(); vis[u] = 0; for(int i = Head1[u]; i != -1; i = Edges1[i].next) { int temp = Dist[u] + Edges1[i].w; if(temp < Dist[Edges1[i].to]) { Dist[Edges1[i].to] = temp; if(!vis[Edges1[i].to]) { vis[Edges1[i].to] = 1; Q.push(Edges1[i].to); } } } }}int main(){ int N,M,u,v,w,s,t,k; while(~scanf("%d%d",&N,&M)) { memset(Edges,0,sizeof(Edges)); memset(Edges1,0,sizeof(Edges1)); memset(Head,-1,sizeof(Head)); memset(Head1,-1,sizeof(Head1)); for(int i = 0; i < M; ++i) { scanf("%d%d%d",&u,&v,&w); Edges[i].to = v; Edges[i].w = w; Edges[i].next = Head[u]; Head[u] = i; Edges1[i].to = u; Edges1[i].w = w; Edges1[i].next = Head1[v]; Head1[v] = i; } scanf("%d%d%d",&s,&t,&k); SPFA(t,N); int kthlenth = A_Star(s,t,N,k); printf("%d\n",kthlenth); } return 0;}
0 0
- POJ 2449 Remmarguts' Date【SPFA】【A*】
- POJ 2449-Remmarguts' Date(A*+spfa)
- POJ 2449 Remmarguts' Date(A* + spfa)
- poj Remmarguts' Date A*+ spfa
- POJ 2449 Remmarguts' Date 第K短路 A* + SPFA
- poj 2449 Remmarguts' Date--k短路--spfa+A*
- poj 2449 Remmarguts' Date(K短路 Spfa+A*)
- POJ 2449 Remmarguts' Date(A*+SPFA)K短路问题
- POJ 题目2449 Remmarguts' Date(K短路,A*+spfa)
- poj 2449 Remmarguts' Date A*+spfa求第k短路
- POJ——2449Remmarguts' Date(A*+SPFA)
- POj 2449 Remmarguts' Date K短路 A*+SPFA
- POJ Remmarguts' Date A*搜索+spfa
- poj-2449-Remmarguts' Date-A
- POJ 2449 Remmarguts' Date A* -
- Remmarguts' Date poj 2449 A*
- poj 2449 Remmarguts' Date 【SPFA+Astar】【经典】
- poj2449 Remmarguts' Date (spfa+A*)
- Hexagon DSP的所有寄存器
- Android代码混淆
- 嵌入式C语言之---模块化编程
- 《深入理解计算机系统》第二章习题2_66
- 通过Id遍历标签
- POJ 2449 Remmarguts' Date【SPFA】【A*】
- Mysql:操作笔记
- java中接口的概念及使用(补充final修饰符的使用)
- 牛刀小试 - 详细总结Java-IO流的使用
- C++应用程序性能优化学习笔记:C++语言特性的性能分析
- A20启动流程分析
- 中国移动灵犀云语音识别及合成SDK(iOS)使用指南
- 统计代码行数脚本
- 准备GMAT极好的网站