POJ 2449 Remmarguts' Date (第k短路 A*搜索算法模板)
来源:互联网 发布:2017年流行网络语 编辑:程序博客网 时间:2024/06/05 09:44
Remmarguts' Date
Time Limit: 4000MS Memory Limit: 65536KTotal Submissions: 22412 Accepted: 6085
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.
"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).
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 52 1 41 2 2
Sample Output
14
Source
POJ Monthly,Zeyuan Zhu
题目链接:http://poj.org/problem?id=2449
题目大意:n个点,m条路,给出起点s和终点t,求从s到t的第k短路的长度
题目分析:A*算法模板题
题目链接:http://poj.org/problem?id=2449
题目大意:n个点,m条路,给出起点s和终点t,求从s到t的第k短路的长度
题目分析:A*算法模板题
#include <cstdio>#include <cstring>#include <queue>#include <vector>using namespace std;int const INF = 0xfffffff;int const MAX1 = 1005;int const MAX2 = 1e5 + 5;int dis[MAX1], head[MAX1], head2[MAX1];int n, m, cnt;bool vis[MAX1];struct node{ int v, w; int next;}e[MAX2], e2[MAX2];struct node1{ int v, g, f; bool operator < (const node1 &r) const { if(r.f == f) return r.g < g; return r.f < f; }};void Add(int u, int v, int w){ e[cnt].v = v; e[cnt].w = w; e[cnt].next = head[u]; head[u] = cnt; e2[cnt].v = u; e2[cnt].w = w; e2[cnt].next = head2[v]; head2[v] = cnt++;}bool spfa(int s){ memset(vis, false, sizeof(vis)); dis[s] = 0; queue <int> q; q.push(s); while(!q.empty()) { int cur = q.front(); q.pop(); vis[cur] = false; for(int i = head2[cur]; i != -1; i = e2[i].next) { if(dis[e2[i].v] > dis[cur] + e2[i].w) { dis[e2[i].v] = dis[cur] + e2[i].w; if(!vis[e2[i].v]) { vis[e2[i].v] = true; q.push(e2[i].v); } } } }}int A_star(int s, int t, int k){ if(s == t) k++; if(dis[s] == INF) return -1; priority_queue <node1> q; int num = 0; node1 tmp, to; tmp.v = s; tmp.g = 0; tmp.f = tmp.g + dis[tmp.v]; q.push(tmp); while(!q.empty()) { tmp = q.top(); q.pop(); if(tmp.v == t) num++; if(num == k) return tmp.g; for(int i = head[tmp.v]; i != -1; i = e[i].next) { to.v = e[i].v; to.g = tmp.g + e[i].w; to.f = to.g + dis[to.v]; q.push(to); } } return -1;}int main(){ scanf("%d %d", &n, &m); cnt = 0; memset(head, -1, sizeof(head)); memset(head2, -1, sizeof(head2)); for(int i = 0; i < MAX1; i++) dis[i] = INF; while(m--) { int u, v, w; scanf("%d %d %d", &u, &v, &w); Add(u, v, w); } int s, t, k; scanf("%d %d %d", &s, &t, &k); spfa(t); printf("%d\n", A_star(s, t, k));}
0 0
- POJ 2449 Remmarguts' Date (第k短路 A*搜索算法模板)
- POJ 2449 Remmarguts' Date 第k短路 A*搜索
- 【POJ 2449】Remmarguts's Date (A*搜索第k短路)
- 搜索 ( 第K短路, A*搜索)——Remmarguts' Date ( POJ 2449 )
- POJ 2449 Remmarguts' Date (A*搜索求K短路)
- (poj 2449 Remmarguts' Date)<A*-K短路模板>
- POJ 2449 Remmarguts' Date 第K短路 A* + SPFA
- poj 2449 Remmarguts' Date(第K短路 A*)
- poj 2449 Remmarguts' Date(A*求第K短路)
- poj 2449 Remmarguts’ Date第k短路 dij+A*
- POJ--2449--Remmarguts' Date【dijkstra_heap+A*】第K短路
- POJ 2449 Remmarguts' Date ( Dijkstra + A* 求解第K短路 )
- POJ 2449 Remmarguts' Date 第K短路 A*
- POJ 2449 Remmarguts' Date(A* - 第K短路)
- poj 2449 Remmarguts' Date(第K短路 A*)
- poj 2449 Remmarguts' Date A*+spfa求第k短路
- POJ 2449 Remmarguts' Date (A* 第k短路)
- POJ 2449 Remmarguts' Date (求第K短路,A* + Dijkstra)
- 蒄莇袄芆芇螅袃羅腿蚁袂肈
- 芄薃袂羂蒃蒃螄羇艿蚅螇肃
- 莀袇艿芀蝿袆罿薆蚅袅肁莈
- 莁芇蝿羈芁蚂袅肇膀蚅羁袃
- 羆莈葿袈羅蒁蚅螄肅膀蒇蚀
- POJ 2449 Remmarguts' Date (第k短路 A*搜索算法模板)
- 肂膂螃肆肅芈蒀羆肇莄蚈螅
- 莇蒈袆肄葿蚃螂肃腿蒆蚈肂
- hdu 5066 Harry And Physical Teacher(物理题 弹性碰撞)
- 膇芆虿肀芃蒅薈羃艿蚄螀羅
- 肈肄节螇袁肀芁袀螄荿芀蕿
- 肈蒁蒀肅芇芀螅虿羃膀螇螄
- 袇羄葿蚄螃肃薂衿肁肂芁蚂
- 薀羁膅莆袂袄膀薅蚂袇芆芁