BFS Traverse all Shortest routes

Source: Internet
Author: User

K-caravansTime limit:2000MS Memory Limit:65536KB 64bit IO Format:%i64d &%i64 U SubmitStatusPracticeURAL 2034

Description

Student Ilya often skips he classes at the university. His friends criticize him for this, but they don ' t know the Ilya spends this time not watching TV serials or listening to Music. He creates a computer game of his dreams. The game ' s world is a forest. There is elves, wooden houses, and a villain. And one can Rob Caravans there! Though there is a caravan in the game so far, Ilya have hard time trying to implement the process of robbing. The game world can be represented as several settlements connected by roads. It is a possible to get from any settlement-to-any and roads (possibly, passing through other settlements on the). The settlements is numbered by integers from 1 to N. All the roads is two-way and has the same length equal to 1. It is not a allowed to move outside the roads. The caravan goes from settlement sTo settlement FFollowing one of the Shortest Routes. Settlement RIs the villain ' Den. A Band of robbers from settlement Rhas received a assignment to rob the caravan. In the evening they would have a exact plan of the the route that the caravan would take the next morning. During the night they'll be a able to move to all settlement on the route, even to settlement sOr F. They would lay an ambush there and rob the caravan in the morning. Of course, among all such settlements the robbers would choose the one closest to settlement R. The robbers has a lot of time until the evening. They don ' t know the caravan ' s route yet, but they want to know the maximum distance they would have a to go In the worst caseTo the settlement where they would rob the caravan. Help Ilya calculate this distance, and it could happen that he'll attend his classes again!

Input

The first line contains integers Nand m(3≤ N≤10 5; 2≤ m≤10 5), which is the number of settlements in the game world and the number of roads between them. Each of the following mLines describes a road. The description contains integers aand b, which is the numbers of the settlements connected by the road. It is guaranteed so each road connects the different settlements and there is at the most one road between any and Settlemen Ts. It is also guaranteed, the road network is connected. Given pairwise different integers s, F, and R, which is the numbers of the settlements described above.

Output

In the-line output the required distance.

Sample Input

input Output
7 71 22 42 53 44 65 66 71 7 3
2
Notesin the sample the caravan may follow either the route 1-2-4-6-7 or the route 1-2-5-6-7. In the first case the robbers lay a ambush in settlement 4, which was at distance 1 from the villain's Den. In the second case the robbers lay a ambush in settlement 2 or settlement 6, which is at distance 2 from the villain ' s D En. The second variant is worse for the robbers, and it should be chosen as the answer. Key: Use the heap to record the path; First BFS search: Use the array dist to record all points to the bandit stronghold distance; The essence lies in the second BFS traversal, clever use of the nature of the pair, just about every line can read and compare, not much to see the code naturally understand! (0x11,int_max means infinity) The BFS here can be used without an array to record whether or not to traverse this.
#include <bits/stdc++.h>using namespaceStd;typedef pair<int,int>Node;#defineX First#defineY SecondConst intN =111111; Vector<int>Nx[n];intDist[n], N, M; Node Answer[n];voidBFS (Const intx) {queue<int>Q; memset (Dist,0x11,sizeofDist); DIST[X]=0;    Q.push (x);  while(!Q.empty ()) {        intU =Q.front ();        Q.pop ();  for(inti =0; I < nx[u].size (); ++i) {intv =Nx[u][i]; if(Dist[v] > Dist[u] +1) {Dist[v]= Dist[u] +1;            Q.push (v); }        }    }}intBFS (intXinty) {queue<int>Q;  for(inti =0; i < N; ++i) {answer[i].x=Int_max; Answer[i].y=0; } answer[x].x=0; Answer[x].y= -Dist[x];    Q.push (x);  while(!Q.empty ()) {        intU =Q.front ();        Q.pop ();  for(inti =0; I < nx[u].size (); ++i) {intv =Nx[u][i]; if(Answer[v] > Node (answer[u].x +1, Max (ANSWER[U].Y,-Dist[v]))) {Answer[v]= Node (answer[u].x +1, Max (ANSWER[U].Y,-Dist[v]));            Q.push (v); }        }    }    //cout << answer[y].x << ' << answer[y].y << Endl;    return-answer[y].y;}intRun () {intx, y, Z; intA, B;  while(Cin >> N >>m) { for(inti =1; I <= N; ++i) {nx[i].clear (); }         for(inti =0; I < m; ++i) {cin>> a >>b;            Nx[a].push_back (b);        Nx[b].push_back (a); } CIN>> x >> y >>Z;        BFS (z); cout<< BFS (x, y) <<Endl; }    return 0;}intMain () {Ios::sync_with_stdio (0); returnRun ();}
View Code

BFS Traverse all Shortest routes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.