POJ2499:Remmarguts' Date(第K短路 SPFA+A*)
来源:互联网 发布:山西省大数据产业协会 编辑:程序博客网 时间:2024/05/17 20:28
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
这道题是给出了一个图,然后给出起点s与终点t与k,要求s到t的第k短路是多少
首先这肯定是一个最短路的问题,我们可以用SPFA算出t到其他所有点的最短路径,然后使用A*去迭代搜索,一开始,把s到t的最短路先压入队列中,那么就变成了以t为起点去遍历,在第k次回到t点的时候所走过的路径最短的值便是第k短路径
#include <stdio.h>#include <string.h>#include <queue>#include <algorithm>using namespace std;const int L = 100005;const int inf = 1<<30;struct node{ int now,g,f; bool operator <(const node a)const { if(a.f == f) return a.g < g; return a.f < f; }};struct edges{ int x,y,w,next;} e[L<<2],re[L<<2];//e是输入给出的定向图,re为其逆向图,用于求t到其他所有点的最短路径int head[1005],dis[1005],vis[1005],n,m,k,s,t,rehead[1005];void Init()//初始化{ memset(e,-1,sizeof(e)); memset(re,-1,sizeof(re)); for(int i = 0; i<=n; i++) { dis[i] = inf; vis[i] = 0; head[i] = -1; rehead[i] = -1; }}void AddEdges(int x,int y,int w,int k){ e[k].x = x,e[k].y = y,e[k].w = w,e[k].next = head[x],head[x] = k; re[k].x = y,re[k].y = x,re[k].w = w,re[k].next = rehead[y],rehead[y] = k;}int relax(int u,int v,int c){ if(dis[v]>dis[u]+c) { dis[v] = dis[u]+c; return 1; } return 0;}void SPFA(int src){ int i; dis[src] = 0; queue<int> Q; Q.push(src); while(!Q.empty()) { int u,v; u = Q.front(); Q.pop(); vis[u] = 0; for(i = rehead[u]; i!=-1; i = re[i].next) { v = re[i].y; if(relax(u,v,re[i].w) && !vis[v]) { Q.push(v); vis[v] = 1; } } }}int Astar(int src,int to){ priority_queue<node> Q; int i,cnt = 0; if(src == to) k++;//在起点与终点是同一点的情况下,k要+1 if(dis[src] == inf) return -1; node a,next; a.now = src; a.g = 0; a.f = dis[src]; Q.push(a); while(!Q.empty()) { a = Q.top(); Q.pop(); if(a.now == to) { cnt++; if(cnt == k) return a.g; } for(i = head[a.now]; i!=-1; i = e[i].next) { next = a; next.now = e[i].y; next.g = a.g+e[i].w; next.f = next.g+dis[next.now]; Q.push(next); } } return -1;}int main(){ int i,j,x,y,w; while(~scanf("%d%d",&n,&m)) { Init(); for(i = 0; i<m; i++) { scanf("%d%d%d",&x,&y,&w); AddEdges(x,y,w,i); } scanf("%d%d%d",&s,&t,&k); SPFA(t); printf("%d\n",Astar(s,t)); } return 0;}
0 0
- POJ2499:Remmarguts' Date(第K短路 SPFA+A*)
- poj2499 Remmarguts' Date 第K短路(spfa+A*)
- POJ 2449 Remmarguts' Date 第K短路 A* + SPFA
- poj 2449 Remmarguts' Date A*+spfa求第k短路
- 【A* + 第K短路】 poj2449 Remmarguts' Date
- poj2449 Remmarguts' Date 第K短路 A*
- poj 2449 Remmarguts' Date 求第k短路(SPFA+A*)
- POJ 2449 Remmarguts' Date(第k短路+spfa+A*搜索)
- poj 2449 Remmarguts' Date 第k短路 A*+spfa 解题报告
- POJ 2449 Remmarguts' Date A*+spfa求第k最短路
- POJ-2449 Remmarguts' Date(A*算法+SPFA求第K短路)
- poj 2449 Remmarguts' Date--k短路--spfa+A*
- poj 2449 Remmarguts' Date(K短路 Spfa+A*)
- POJ 2449 Remmarguts' Date(A*+SPFA)K短路问题
- poj2449 Remmarguts' Date --- k短路模板(SPFA+A*)
- POJ 题目2449 Remmarguts' Date(K短路,A*+spfa)
- POJ 2448(K短路,A*+SPFA) Remmarguts' Date
- POJ 2447 Remmarguts' Date【k短路 SPFA+A* 模板题】
- [LeetCode 9] Palindrome Number -- 回文数字
- 函数指针与回调函数
- 二进制快速排序
- 关于位域的字节内存储顺序、字节对齐、字节序以及符号
- Ubuntu13.04环境下载、编译Android源代码
- POJ2499:Remmarguts' Date(第K短路 SPFA+A*)
- 网站一打开就显示windows安全,输入用户名和密码
- 第三十讲 : 综合应用&存储过程
- java “动态数组”
- AT&T mnemonic conventions
- python3.3 django错误处理“sre_constants.error: nothing to repeat”
- ios动画系列 -- UIView的transform
- Attempt to refer to a unregistered pool by its alias 'xxx'
- 2014腾讯测试实习面试题