uva 10740 - Not the Best(第k短路)
来源:互联网 发布:练字软件免费下载 编辑:程序博客网 时间:2024/06/04 23:12
Problem A
Not the Best
Input: standard input
Output: standard output
Time Limit: 1 second
Abul is not the best student in his class; neither is he the best player in his team. Not that he is bad; he is really good, but unfortunately not the best.
Last semester our “not quite the best” Abul took a course on algorithms. In one of the assignments he was required to find the shortest path from a given vertex x to another vertex y in a weighted directed graph. As you have probably already guessed, he rarely managed to find the shortest path; instead he always ended up finding the kth (2 £ k £ 10) shortest path from x to y. If he was fortunate enough and the shortest k paths from x to y had the same length, he was given credit for his solution.
For example, for the graph above, Abul was asked to find the shortest path from vertex 5 to vertex 2. The shortest 7 paths from vertex 5 to vertex 2 are listed below in non-decreasing order of length. For this graph Abul was able to find the 5thshortest path which could be either 5 ® 4 ® 3 ® 2 ® 5 ® 1 ® 2 or 5 ® 1 ® 2 ® 5 ® 4 ® 3 ® 2, each with length 15.
Path
Length
5 ® 1 ® 2
5
5 ® 4 ® 3 ® 2
6
5 ® 1 ® 2 ® 5 ® 1 ® 2
14
5 ® 4 ® 3 ® 2 ® 5 ® 1 ® 2
15
5 ® 1 ® 2 ® 5 ® 4 ® 3 ® 2
15
5 ® 4 ® 3 ® 2 ® 5 ® 4 ® 3 ® 2
16
5 ® 1 ® 2 ® 5 ® 1 ® 2 ® 5 ® 1 ® 2
23
Given a description of the graph, source vertex x, target vertex y, and the value of k, you need to find out the length of the path Abul computed. You may assume that there exists at least one path from x to y in the given graph.
Input
The input may contain multiple test cases.
The first line of each test case contains two integers n (2 £ n £ 100) and m (1 £ m £ 1000) giving respectively the number of vertices, and the number of edges in the graph. Each vertex in the graph is identified by a unique integer in [1, n]. The second line of the test case contains the values of x, y and k (1 £ x, y £ 100, x ¹ y, 2 £ k £ 10). Each of the next m lines contains three integers u, v and l (1 £ u, v £ 100, 0 £ l £ 10000) specifying a directed edge of length l from vertex u to vertex v.
The input terminates with two zeros for n and m.
Output
For each test case in the input output a line containing an integer giving the length of the kth shortest path in the graph. If the graph does not have at least k paths from x to y, output a –1 instead.
Sample Input Output for Sample Input
3 3
1 3 4
1 3 3
1 2 4
2 3 5
5 6
5 2 5
1 2 2
2 5 4
3 2 3
4 3 1
5 1 3
5 4 2
2 2
1 2 3
1 2 5
2 2 2
0 0
-1
15
9
Problem setter: Rezaul Alam Chowdhury, University of Texas at Austin
Special thanks: Md. Kamruzzaman, EPS
求两点间的第k短路。因为点数比较少,所以直接暴力做的,利用每个点最多k次出队。正规做法好像是A*。
#include<cstdio>#include<algorithm>#include<vector>#include<cstring>#include<stack>#include<iostream>#include<queue>using namespace std;const int maxn = 100 + 5;typedef pair<int, int> P;int n, m;int S, E, k;struct Edge{ int to, dis; Edge(int to, int dis){ this -> to = to; this -> dis = dis; }};vector<Edge> G[maxn];int vis[maxn];int ans;void bfs(){ priority_queue<P> q; memset(vis, 0, sizeof vis); q.push(P(0, S)); while(!q.empty()){ P p = q.top(); q.pop(); int dist = -p.first; int pos = p.second; if(vis[pos]>k) continue; //cout << pos << ' ' << dist << endl; vis[pos]++; if(pos==E && vis[pos]==k){ ans = dist; return; } for(int i = 0;i < G[pos].size();i++){ int to = G[pos][i].to; int der = G[pos][i].dis; q.push(P(-(der+dist), to)); } }}int main(){ while(scanf("%d%d", &n, &m)){ if(n == 0 && m == 0) break; scanf("%d%d%d", &S, &E, &k); for(int i = 0;i < maxn;i++) G[i].clear(); while(m--){ int u, v, l; scanf("%d%d%d", &u, &v, &l); G[u].push_back(Edge(v, l)); } ans = -1; bfs(); printf("%d\n", ans); } return 0;}
- uva 10740 - Not the Best(第k短路)
- (intermediate) 最短路(第k短路) UVA 10740 - Not the Best
- uva 10740 Not the Best (最短路 A*算法)
- uva_10740_Not the best(第k短路)
- [第2短路变形]LightOJ 1099 - Not the Best
- uva 10740 Not the Best 两点之间的K短路径
- poj 2449Remmarguts' Date uvaoj 10740 not the best dijkstra或spfa或bellman-ford k短路 A*
- LightOj 1099 Not the Best(次短路)
- lightoj 1099 - Not the Best(次短路)
- lightoj 1099 - Not the Best (次短路)
- LightOJ - 1099 Not the Best(次短路)
- 10740 - Not the Best
- 【最短路】LightOJ 1099 - Not the Best 第二短路
- LightOJ 1099 - Not the Best(Dijkstra次短路)
- POJ 3463 Sightseeing (第k短路)
- poj2449 (第k条最短路)
- A* && 第k短路详解 (详尽)
- 第K短路(A*算法)
- 1589-Xiangqi【模拟】
- Ubuntu 12.10 安装 jdk-7u10-linux-x64.tar.gz
- Ubuntu下安装JDK图文解析
- C++设计一个简单的壳(2)
- js判断当前窗口是否有父窗口
- uva 10740 - Not the Best(第k短路)
- Android平台下OpenGL初步
- 状态机在线上录单系统的实际应用例子
- 第三方库 EGOCache
- obj-c编程10:Foundation库中类的使用(6)[线程和操作队列]
- POJ 3723 Conscription(最大生成树)
- oracle查询
- 割点、割边和双连通分量
- Investment_多重背包!!!