Huawei internal staff assessment--subway station best Route Recommendation

Source: Internet
Author: User
Tags unique id

Background overview

? The city's subway network consists of several lines, each line has a number of stations, the line itself has no intersection? When the lines cross or overlap, the shared stations can be transferred to each other at these stations? Each line is a two-way drive? There are two lines on the line: I-line and O-–i-line. Passengers can only take the subway to the other end at the end point, and there are two directions at the non-end. (Line Line 8) –o-shaped line all stations form a ring, there is no end point, passengers in either station have two directions to choose from. (Line Line 4)

Functional Requirements

Select a station in the subway network as the starting point, choose another station as the end point, midway can transfer, demand output

1) Shortest route length: The minimum number of stations to pass from the start to the end (the station count does not contain the starting point, including the end)

2) Shortest route: All routes that meet the shortest route length requirements (there may be multiple routes, each route from the starting point to the finish order output all sites passing through, including the start and end points)

3) Best route: The shortest route with the least number of transfers (there may be multiple routes)

Enter a description

Each site in the Metro network is identified with a unique ID, and the ID is a 32-bit positive integer;

Enter one line at a time, represented as an array of site IDs;

– for example {2, 3, 6, 9, 10}, which indicates that this is an I-shaped line, 2 and 10 are both ends, the array element order is consistent with the order of the elements from one end to the other, and-for example, {1, 3, 6, 7, 4, 1}, the first and last sites, indicating that this The sequence of elements in the array and from site 1 in a direction around a circle through the site in the same order;

The same site appears in both lines (3, 6 above), indicating that two lines can be transferred to each other at these sites.

Example

Subway information in a city,

line1:{1,2,3,4,5};

line2:{1,10,9,7,6};

line3:{5,7,8};

line4:{11,5};

From starting point 1 to Terminal 11

1) Shortest route length is 5

2) The shortest route has 2, respectively {1, 2, 3, 4,5,11} and {1,10,9,7,5,11}

3) The best route has 1: {1, 2, 3, 4,5,11} (Transfer once)

 

This problem is very practical, similar to the Baidu map to check the bus, the shortest distance and the least number of transfers, such as the category of graph theory, find the shortest path length is not difficult, but to preserve it must also be deep search, in the Huawei OJ is the most difficult kind of classification (challenge), the general Huawei Recruitment machine test only three questions (beginner , high-level each), will not test such a difficult problem. As for the minimum transfer plan has not been done, the code exists here, continue to improve the back, welcome replies to Exchange ~


#include <iostream>//#include <string>//#include <algorithm>//#include <cmath> #include < vector> #include <queue>//#include <stack>//#include <iomanip>using namespace std; #define MAX 535int flag[max];vector<int>station_reach[max];static vector<int>myfront[max];/********************* Function Description: Add a metro line function prototype: void addline (unsigned int lineno, unsigned int stationnum, unsigned int *pstationarray); Input parameters: Lin ENo metro line number; Stationnum the number of stations in the Metro line, guaranteed by the caller to be no less than 2; Pstationarray all site information for the metro line, Pstationarray points to the storage empty Will be released outside the function, please request the storage space; output parameter: No return value: No ******************************************************************************* /void addline (unsigned int lineno, unsigned int stationnum, unsigned int * Pstationarray) {/* here implements the function */int i;for (i=1;i<stationnum;i++) {Station_reach[pstationarray[i]].push_back (Pstationarray[i-1]); Station_reach[pstationarray[i-1]].push_back (Pstationarray[i]);} return;} /************************************************************************************************************** Function Description: Calculates the shortest route length function prototype from starting point to Terminal: int calcminpathlen (unsigned int srcstation, unsigned int desstation); input parameter: SRCS Tation starting point; desstation terminal; output parameter: No return value: starting point to terminal shortest route Length-1: Any error conditions (including route does not exist, site does not exist, start and end overlap, etc.) * * * * * *************************************************************************************************************** **/int calcminpathlen (unsigned int srcstation, unsigned int desstation) {/* Here implement function */if (srcstation==desstation) return   -1;int I,j,newsta;int minlen=1,count=0;queue<int>qsta; /*for (i=1;i<11;i++) {cout<<i<< "station" << ":"; for (J=0;j<station_reach[i].size (); j + +) cout< <station_reach[i][j]<< ""; Cout<<endl;} Cout<<endl;*/memset (flag,0,sizeof (flag)); for (I=0;i<station_reach[srcstatIon].size (); i++) {if (station_reach[srcstation][i]==desstation) return Minlen;qsta.push (station_reach[srcstation][ I]);//count++;} Flag[srcstation]=1;int ss=qsta.size (); while (!qsta.empty ()) {minlen++;count=qsta.size (), while (count--) {newsta= Qsta.front (); Flag[newsta]=1;for (I=0;i<station_reach[newsta].size (); i++) {if (station_reach[newsta][i]== Desstation) return minlen;if (flag[station_reach[newsta][i]]==0) Qsta.push (Station_reach[newsta][i]);} Qsta.pop ();}}    if (Qsta.empty ()) return-1; Return 0;};/ Function Description: Output from starting point to terminal                              The shortest route function prototype: int searchminpathes (unsigned int srcstation, unsigned int desstation,                              Unsigned int* ppathnum, unsigned int* ppathlen, unsigned int **pppathes); input parameters: Srcstation starting point; Desstation terminal; Output Param Ppathnum most          Number of short-circuit lines; PpatHlen shortest route length; Pppathes Store the memory address of the shortest route, memory space in this function request, caller release, memory space storage format see PPT requirements; return value: 0: Success-1: Any error conditions (including route not present, site not present, start and end overlap, etc.) *  /void DFS (unsigned int srcstation, unsigned int desstation, unsigned int minlen, unsigned int *ppathlen, unsign Ed int* ppathnum, unsigned int **ppathes) {int i,j;if (minlen==1) {if (myfront[desstation][0]==srcstation) {(*ppathnum) + + ;pP athes[(*ppathnum) -1]= (unsigned int *) malloc ((*ppathlen+1) *sizeof (unsigned int)) memset (ppathes[(*ppathnum)-1], 0, (*ppathlen+1) *sizeof (unsigned int));pP athes[(*ppathnum) -1][minlen-1]=srcstation;ppathes[(*ppathnum) -1][minlen ]=desstation;//cout<<desstation<< "<<minlen<<" "&LT;&LT;*PPATHNUM&LT;&LT;ENDL;} return;} For (I=0;i<myfront[desstation].size (); i++) {j=myfront[desstation][i];//cout<<j<< "" << Minlen-1<<endl;dfs (Srcstation,j,minlen-1,ppathlen,ppathnum, ppathes);pP athes[(*ppathnum) -1][minlen]=desstation;if (*ppathnum>1 && ppathes[(*ppathnum) -2][minlen]= =0) ppathes[(*ppathnum) -2][minlen]=desstation;}} int searchminpathes (unsigned int srcstation, unsigned int desstation, unsigned int* Ppathnum, unsigned int* ppathlen, unsigned int **pppathes) {/* here implements the function */if (SRCST ation==desstation) return-1;int i,j,newsta,findflag=0;int minlen=1,count=0;int pathnum=0;int sum=0;queue<int>       Qsta; for (i=0;i<max;i++) myfront[i].clear (), memset (flag,0,sizeof (flag)), for (I=0;i<station_reach[srcstation]. Size (); i++) {myfront[station_reach[srcstation][i]].push_back (srcstation); if (station_reach[srcstation][i]== desstation) {*ppathnum=1;*ppathlen=1;*pppathes= (unsigned int *) malloc (2*sizeof (unsigned int));(*pppathes) [0]= srcstation; (*pppathes) [1]=desstation;return 0;} Qsta.push (Station_reach[srcstation][i]);//count++;} Flag[srcstation]=1;findflag=0;while (!qsta.empty () &Amp;&!findflag) {minlen++;count=qsta.size (); while (count--) {Newsta=qsta.front (); Flag[newsta]=1;for (i=0;i< Station_reach[newsta].size (); i++) {if (flag[station_reach[newsta][i]]==0) for (j=0;j<myfront[station_reach[ Newsta][i]].size (); j + +) if (Myfront[station_reach[newsta][i]][j]==newsta) break;if (j==myfront[station_reach[ Newsta][i]].size ()) Myfront[station_reach[newsta][i]].push_back (Newsta); if (station_reach[newsta][i]==desstation ) {findflag=1;break;/**pppathes= (unsigned int *) malloc ((minlen+1) *sizeof (unsigned int));(*pppathes) [minlen]= Desstation;for (j=minlen-1,k=desstation;j>=0;j--) (*pppathes) [j]=front[(*pppathes) [j+1]];p athnum++;*/}if (flag [station_reach[newsta][i]]==0) Qsta.push (Station_reach[newsta][i]);} Qsta.pop ();}}   if (Qsta.empty ()) return-1;/*for (i=1;i<12;i++) {cout<<i<< "station:";   For (J=0;j<myfront[i].size (); j + +) cout<<myfront[i][j]<< "";   cout<<endl; } */if (Findflag) {*ppathlen=minlen;*ppathnum=0;dfs (srcstation,desstation,minlen,ppathlen,ppathnum,pppathes);} else return-1;//cout<<minlen<< "<<DesStation<<" "<<sum<<endl;return 0;} /************************************************************************************************* function Description: Output from starting point to terminal of the optimal route function original                               Type: int searchbestpathes (unsigned int srcstation, unsigned int desstation, unsigned int *ppathnum, unsigned int* ppathlen, uns         igned int** pppathes); input parameters: Srcstation starting point; Desstation terminal; Output Param ppathnum the optimal route bar number;         Ppathlen shortest route length; Pppathes stores the memory address of the shortest route, memory format, memory space in the function of the request, the caller is released; return value: 0: Success-1: Any error conditions (including the route does not exist, the site does not exist, the beginning and end overlap, etc.) **************** /int Searchbestpathes (             unsigned int srcstation, unsigned int desstation, unsigned int *ppathnum,        Unsigned int* ppathlen, unsigned int** pppathes) {/* Implement function here */return 0;}  /************************************************************************************************* function Description: Clears all line information function prototypes: void Clear (); input parameter: No output parameter: No return value: No ************************************************************************************* /void Clear () {/* here implements the function *///vector<int>station_reach[max];for (int i=0;i<max;i++) Station_    Reach[i].clear (); return;}; int main () {unsigned int line1[] = {1,2,3,4,5};unsigned int line2[] = {1,10,9,7,6};unsigned int line3[] = {5,7,8};unsigned    int line4[] = {11,5};    int ret,i;unsigned int Ppathnum,ppathlen,*pppathes[70]={null}; AddLine (1,5,&line1[0]); AddLine (2,5,&line2[0]); AddLine (3,3,&line3[0]);    AddLine (4,2,&line4[0]); Cout<<calcminpathlen (1, one) <<endl;    Searchminpathes (1,11,&ppathnum,&ppathlen,pppathes); cout<<ppathlen<< "" <<ppathnum<<endl;for (ret=0; Ret<pPathnum; ret++) {for (i=0;i<=ppathlen;i++) cout<<pppathes[ret][i]<< ""; Cout<<endl;} Clear (); if (Searchminpathes (1,11,&ppathnum,&ppathlen,pppathes) ==-1) cout<< "NO" <<endl;cout <<calcminpathlen (1, one) <<endl;return 0;}


Huawei internal staff assessment--subway station best Route Recommendation

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.