bfs打印最短路径
2018-02-06 05:33
375 查看
The Flight PlanAttempted by: 511/Accuracy: 75%/Maximum Score: 20/ 2 VotesTag(s):
Algorithms, BFS, BreadthFirst Search, Easy, Graphs
PROBLEMEDITORIALMY SUBMISSIONSANALYTICSYou are given flights route map of a country consisting of NN citiesand MM undirectedflight routes. Each city has an airport and each airport can work as layover. The airport will be in two states, Loading and Running. In loading state, luggage is loaded into the planes. In the running state, planes will leave the airport for the next city.All the airports will switch their states from Loading to Running and vice versa after every TT minutes.You can cross a city if its airport state is running. Initially, all the airports are in running state. At an airport, if its state is loading, you have to wait for it to switch its state to running. The time taken to travel through any flight route is CC minutes.Find the lexicographically smallest path which will take the minimum amount of time (in minutes) required to move from city XX tocity YY.It is guaranteed that the given flight route map will be connected. Graph won't contain multiple edges and self loops. A self loop is an edge that connects a vertex to itself.Input Format:The first line contains 44 spaceseparated integers, NN, MM, TT and CC.Next MM linescontains two space separated integers each, UU and VV denotingthat there is a bidirectional road between city UU andcity VV.Next line contains two space separated integers, XX and YY.Output Format:In the first line print an integer KK,denoting the number of city you need to go through to reach city YY fromthe city XX.In next line, print KKspaceseparated integers denoting the path which will take the minimum amount of time (in minutes) required by to move from city XX tocity YY.There can be multiple paths. Print the lexicographically smallest one.Constraints:1≤N≤1031≤N≤103N−1≤M≤N∗(N−1)2N−1≤M≤N∗(N−1)21≤C≤1031≤C≤1031≤T≤1031≤T≤1031≤U,V,X,Y≤N1≤U,V,X,Y≤NSAMPLE INPUT5 5 3 5 1 2 1 3 2 4 1 4 2 5 1 5SAMPLE OUTPUT
3 1 2 5ExplanationFastest path will be 1−>2−>51−>2−>5.You can reach city 22 in 55 minutes.After 33 minutesthe airport in city 22 willchange its state to Loading. So in city 22,you have to wait for 11 minutefor the airport to change its state. So total time will be 55 minutes(from city 11 tocity 22)+ 11 minute(waiting time at city 22)+ 55 minutes(from city 22 tocity 55)= 1111 minutes.Time Limit:1.0 sec(s) for each input file.Memory Limit:256MBSource Limit:1024KBMarking Scheme:Marksare awarded when all the testcases pass.Allowed Languages:C,C++, C++14, Clojure, C#, D, Erlang, F#, Go, Groovy, Haskell, Java, Java 8, JavaScript(Rhino), JavaScript(Node.js), Julia, Kotlin, Lisp, Lisp (SBCL), Lua, Objective-C, OCaml, Octave, Pascal, Perl, PHP, Python, Python 3, R(RScript), Racket, Ruby, Rust, Scala,Swift, Visual Basic
#include <bits/stdc++.h> using namespace std; vector<int>adj[1001]; int vis[1001]; int parent[1001]; int main() { int n,m,c,k; cin>>n>>m>>c>>k; int a,b; for(int i=1;i<=m;i++) { cin>>a>>b; adj[a].push_back(b); adj[b].push_back(a); } for(int i=1;i<=n;i++) { sort(adj[i].begin(),adj[i].end()); } int x,y; cin>>x>>y; queue<int>q; q.push(x); vis[x]=true; for(int i=0;i<=1000;i++) parent[i]=-1; int flag=0; while(!q.empty()) { int p=q.front(); q.pop(); if(flag) break; for(int i=0;i<adj[p].size();i++) { if(!vis[adj[p][i]]) { q.push(adj[p][i]); parent[adj[p][i]]=p; vis[adj[p][i]]=1; if(adj[p][i]==y) { flag=1; break; } } } } vector<int>path; int tmp=y; while(tmp!=x) { path.push_back(tmp); tmp=parent[tmp]; } path.push_back(x); int sz=path.size(); cout<<sz<<endl; for(int i=sz-1;i>=0;i--) { cout<<path[i]<<" "; } return 0; }
相关文章推荐
- bfs寻找最短路径并且打印路径和方向
- bfs 记录和打印最短路径
- BFS解迷宫问题,并打印最短路径
- bfs走迷宫记录最短路径(求大神指…
- 迷宫的最短路径(bfs)
- Dijkstra算法求单源最短路径(二)(BFS的改版)
- Treasure Hunting (hdu 3468 二分匹配+bfs最短路径)
- HDU 1495 非常可乐(bfs)|POJ 3414 Pots(bfs+打印路径)
- HDU 1372<记忆BFS找最短路径>
- 经典图算法Java代码实践:BFS,DFS以及几种最短路径算法
- BFS广度搜索(最短路径问题)
- Codeforces Round #287 (Div. 2) E bfs+记录最短路径(邻接表的使用
- (广度优先搜索第一课)迷宫的最短路径 - BFS
- bfs_迷宫求最短路径
- Floyed多源节点--最短路径C++实现与实现打印路径
- 迷宫的最短路径(BFS的简单应用)
- SPFA 最短路径打印方法
- 最少步数 (bfs最短路径)
- BFS(最短路+路径打印) POJ 3984 迷宫问题
- 图之 打印从源点到任何一个顶点最短路径所经过的顶点