POJ 2449 A*+spfa
来源:互联网 发布:c语言小游戏代码txt 编辑:程序博客网 时间:2024/06/10 05:19
题目:
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.
题目大意:在一个图中,找到s点到t点的第k短路,输出其路劲长度,如果没有这条路,则输出-1.
算法思路:
先用spfa算法逆向求出终点t到其他所有点的最短距离。然后再用A*算法,创建一个优先队列,只要终点入队k次,则已近找到了这条路劲。
当然,还得注意几点:(1)起点终点相同时,k要加1;(2)当终点到达不了起点时,则无路劲,输出-1;(3)当k值过大时,也无路径,输出-1.
代码:
//存储方式类似于邻接表#include <cstdio>#include <algorithm>#include <queue>#include <vector>#include <cstring>#define MAXN 10005 //边数#define inf 1 << 25 // inf等于2的25次方using namespace std;int dis[MAXN];//记录终点到各节点的最短距离struct node{ int v, dis;};struct edge{ int v, w; friend bool operator < (edge a, edge b) { return (a.w + dis[a.v]) > (b.w + dis[b.v]); }};vector <node> map[MAXN];//正向边vector <node> remap[MAXN];//反向边int n, m; //n是节点数,m是边数。int s, t, k; //s是起始点,t是结束点,k是第k小。void init()//清空以前的边的数据{ int i; for (i = 0; i < MAXN; ++i) map[i].clear(); for (i = 0; i < MAXN; ++i) remap[i].clear();}void spfa(int s)//计算出终点到各点的最短距离{ int i; bool used[MAXN];//标记数组 memset(used, 0, sizeof(used)); for (i = 0; i < MAXN; ++i) dis[i] = inf; dis[s] = 0; used[s] = true; queue <int> q; q.push(s); while (!q.empty()) { int u = q.front(); q.pop(); used[u] = false; for (i = 0; i < remap[u].size(); ++i) { node p = remap[u][i]; if (dis[p.v] > dis[u] + p.dis) { dis[p.v] = dis[u] + p.dis; if (!used[p.v]) { used[p.v] = true; q.push(p.v); } } } }}int a_star(){ if (s == t) k++; //注意,起始和结束一样,k要+1; if (dis[s] == inf)//如果终点到不了起点,说明无解 return -1; int i, x, len, cnt[MAXN];//cnt数组用来记录节点入对的次数 edge n1, n2; priority_queue <edge> q;//用优先队列存放边(第一个最小) memset(cnt, 0, sizeof(cnt)); n1.v = s; n1.w = 0; q.push(n1); while (!q.empty()) { n2 = q.top(); q.pop(); x = n2.v; len = n2.w; cnt[x]++; if (cnt[t] == k)//终点入队次数等于k,则找到正确答案 return len; if (cnt[x] > k)//这个节点入队次数大于k,跳过,因为每个节点最多入队k次 continue; for (i = 0; i < map[n2.v].size(); ++i)//采用BFS处理所有与n2.v相连得节点 { n1.v = map[n2.v][i].v; n1.w = len + map[n2.v][i].dis; q.push(n1); } } return -1;//如果搜完所有路劲都找不到答案,返回-1}int main(){ int i; node p; while (scanf("%d%d", &n, &m) != EOF) { init(); int a, b, l; for (i = 1; i <= m; ++i) { scanf("%d%d%d", &a, &b, &l); p.v = b; p.dis = l; map[a].push_back(p); p.v = a; remap[b].push_back(p); } scanf("%d%d%d", &s, &t, &k); spfa(t); int ans = a_star(); printf("%d\n", ans); } return 0;}
- POJ 2449 A*+SPFA
- POJ 2449 A*+spfa
- POJ 2449 第K短路 SPFA+A*
- POJ 2449第K短路(A*+spfa)
- POJ 2449 Remmarguts' Date【SPFA】【A*】
- poj 2449 第k短路 A* + spfa
- POJ 2449-Remmarguts' Date(A*+spfa)
- k短路 SPFA+A*算法 poj 2449
- k短路 SPFA+A*算法 poj 2449
- POJ 2449 Remmarguts' Date(A* + spfa)
- poj Remmarguts' Date A*+ spfa
- poj 2449( k 短路 spfa+A*来求解)
- 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短路
- 提高PHP编程效率 引入缓存机制提升性能
- OpenGL之抗锯齿 以及 线宽的设置
- 二十一、自定义注解实例
- Oracle 常用SQL系统查询语句
- 用微博控制你的电脑
- POJ 2449 A*+spfa
- matlab
- Qt学习之路(41): QTableWidget
- ARM的堆栈学习笔记
- Android -- 网络管理分析
- java new Date 获取时间 与系统不一致
- 免费获取百度网盘空间300M
- 有时候还逃课的视频转换
- cmake 学习