bfs打印最短路径

 The Flight Plan
Attempted by:  511
/
Accuracy:  75%
/
Maximum Score:  20
/
 
2 Votes
Tag(s):
 

Algorithms, BFS, Breadth First Search, Easy, Graphs

PROBLEM
EDITORIAL
MY SUBMISSIONS
ANALYTICS

You are given flights route map of a country consisting of NN cities and MM undirected flight 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 to city 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 space separated integers, NNMMTT and CC. Next MM lines contains two space separated integers each, UU and VV denoting that there is a bidirectional road between city UU and city 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 from the city XX. In next line, print KKspace separated integers denoting the path which will take the minimum amount of time (in minutes) required by to move from city XX to city YY. There can be multiple paths. Print the lexicographically smallest one.

Constraints:
1N1031≤N≤103
N1MN(N1)2N−1≤M≤N∗(N−1)2
1C1031≤C≤103
1T1031≤T≤103
1U,V,X,YN1≤U,V,X,Y≤N

SAMPLE INPUT
 
5 5 3 5
1 2
1 3
2 4
1 4
2 5
1 5
SAMPLE OUTPUT
 
3
1 2 5 
Explanation

Fastest path will be 1>2>51−>2−>5. You can reach city 22 in 55 minutes. After 33 minutes the airport in city 22 will change its state to Loading. So in city 22, you have to wait for 11 minute for the airport to change its state. So total time will be 55 minutes (from city 11 to city 22) + 11 minute (waiting time at city 22) + 55 minutes (from city 22 to city 55) = 1111 minutes.

Time Limit: 1.0 sec(s) for each input file.
Memory Limit: 256 MB
Source Limit: 1024 KB
Marking Scheme: Marks are 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;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值