您的位置:首页 > 其它

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 INPUT 
5 5 3 5
1 2
1 3
2 4
1 4
2 5
1 5
SAMPLE OUTPUT 
3
1 2 5
ExplanationFastest 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;
}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: