Subway transfer-Huawei 2014-dijkstra and Floyd-warshall algorithm to solve the problem

Source: Internet
Author: User
Tags time limit
Subway Transfer--The sample question of Huawei 2014 School recruit Machine --Method One: Dijkstra Shortest path algorithm

 

The original question is as follows:

Subway transfer

Describe:

Known 2 metro lines, where A is the loop, B for the east-west line, the lines are two-way. After the site names are as follows, the two lines cross the transfer point with T1, T2 said. Write a program, arbitrary input two site name, output by subway at least need to pass the number of stations (including the start and end of input, transfer site only calculate once).

Metro line A (loop) via station: A1 A2 A3 A4 A5 A6 A7 A8 A9 T1 a10a11 A12 A13 T2 A14 A15 A16 A17 A18

Metro line B (line) via station: B1 B2 B3 B4 B5 T1 B6 B7 B8 B9 b10t2 B11 B12 B13 B14 B15

Run time limit: Unlimited

Memory Limit: Unlimited

Input: Enter two different station names

Output: The minimum number of stations to go through, including the input of the starting point and end point, transfer site only calculate once

Sample input: A1 A3

Sample output: 3

 

try to solve this problem last week, at that time did not look at the part of the data structure diagram, thinking with a double linked list with a variety of conditions to resolve, found too complex, and then search on the Internet, here provides a map to solve the floyed algorithm, links are as follows:

http://blog.csdn.net/arcsinsin/article/details/11267755

In view of this , these days hurriedly went to see the next part of the figure, just looked at the Dijkstra single source point to all the shortest path algorithm, think can be transformed to solve the problem here, hence resolved as follows.

See here for the Dijkstra algorithm:

http://www.cnblogs.com/dolphin0520/archive/2011/08/26/2155202.html

 

#include <iostream> #include <string> #include <stack> using namespace std; #define SIZE_A #define SIZE_B #define N/A + 1 string a[] = {"A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "T
1 "," A10 "," A11 "," A12 "," A13 "," T2 "," A14 "," A15 "," A16 "," A17 "," A18 "," A1 "}; 
B[] = {"B1", "B2", "B3", "B4", "B5", "T1", "B6", "B7", "B8", "B9", "B10", "T2", "B11", "B12", "B13", "B14", "B15"}; Node[] = {"A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "T1", "A10", "A11", "A12", "A13", "T2", "A14", "A15", " 

A16 "," A17 "," A18 "," B1 "," B2 "," B3 "," B4 "," B5 "," B6 "," B7 "," B8 "," B9 "," B10 "," B11 "," B12 "," B13 "," B14 "," B15 "};
	struct Graph {int matrix[n][n];
	int n;
int e;

};
Graph G;
BOOL S[n];
int dist[n];

int path[n];

int length=0;

String Strbegin,strend;

void Dijkstra (int v0, int v1);
	int GetPos (String *array, String & str) {int n=0;
		if (str[0] = = ' B ') {n+=20;
	array+=20;
	if (str = = "T1") return 9;

	if (str = = "T2") return 14;
		while (*array!= str) {array + +;
n++;	} return N;
		} void Buildgraph () {///initialize matrix for (int i=0; i<n; i++) {for (int j=0; j<n; J + +) {g.matrix[i][j]=0;
		(int i=0; i< (size_a-1); i++) {int u = GetPos (Node,a[i]) based on A;

		int v = getpos (node,a[i+1]);
		G.matrix[u][v]=1;
	G.matrix[v][u]=1;
		///(B) Establish side information for (int i=0; i< (size_b-1); i++) {int u = GetPos (Node,b[i]);

		int v = getpos (node,b[i+1]);
		G.matrix[u][v]=1;
	G.matrix[v][u]=1;
	int u = getpos (Node,strbegin);

	int v = getpos (node,strend);

Dijkstra (U,V);
		} void Dijkstra (int v0, int v1) {//1, initialization for (int i=0; i<n; i++) {S[i] = false;
			if (G.matrix[v0][i]!= 0) {Dist[i] = G.matrix[v0][i];
		Path[i] = V0;
			else {dist[i] = Int_max;
		Path[i] =-1;
	}} s[v0]=true;
	dist[v0]=0;

	Path[v0]=v0;
		2, Cycle n-1 for (int i=0; i< (N-1); i++) {//2.1, Fetch dist[u] the smallest u int min=int_max;
		int u;
		for (int j=0; j<n; J + +) {if (s[j] = = False && Dist[j] < min) {		min = Dist[j];
			U = j;
		}} S[u] = true; 2.2. Update all U adjacency w for (int w=0; w<n; w++) {if (s[w] = = False && G.matrix[u][w]!= 0) {if (dist[u
					] + G.matrix[u][w] < Dist[w]) {dist[w] = Dist[u] + g.matrix[u][w];
				PATH[W] = u;
		()}}//3, Get results while (v1!= v0) {v1 = Path[v1];
	length++; 
	length + +;
	Stack<int> s;
	while (v1!= v0)//{//S.push (v1);
	V1 = Path[v1];
	}//s.push (V0);
	while (!s.empty ())//{//length++;
	if (1)//debug//{/int pos = S.top ();
	String str = Node[pos];
	STR + + "";
	cout<<str<< "";
	}//S.pop ();
		int main () {//while (0) {length = 0;
		cin>>strbegin>>strend;
		if (Strbegin = = strend) cout<< "1" <<endl;
			else {buildgraph ();
		cout<<length<<endl;
	System ("pause");
return 0; }


--method Two: Floyd-warshall vertex to shortest path algorithm

the algorithm can find the shortest path between each vertex pair, the writing is simple, the main core is a three-layer nested loop.


#include <iostream> #include <string> #include <stack> using namespace std; #define SIZE_A #define SIZE_B #define N #define INF 0XFFFFF//+ 1 string a[] = {"A1", "A2", "A3", "A4", "A5", "A
6 "," A7 "," A8 "," A9 "," T1 "," A10 "," A11 "," A12 "," A13 "," T2 "," A14 "," A15 "," A16 "," A17 "," A18 "," A1 "}; 
B[] = {"B1", "B2", "B3", "B4", "B5", "T1", "B6", "B7", "B8", "B9", "B10", "T2", "B11", "B12", "B13", "B14", "B15"}; Node[] = {"A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "T1", "A10", "A11", "A12", "A13", "T2", "A14", "A15", " 

A16 "," A17 "," A18 "," B1 "," B2 "," B3 "," B4 "," B5 "," B6 "," B7 "," B8 "," B9 "," B10 "," B11 "," B12 "," B13 "," B14 "," B15 "};

int matrix[n][n];
int dist[n][n];

int path[n][n];

int length=0;

String Strbegin,strend;

void Floyd_warshall ();
	int GetPos (String *array, String & str) {int n=0;
		if (str[0] = = ' B ') {n+=20;
	array+=20;
	if (str = = "T1") return 9;

	if (str = = "T2") return 14;
		while (*array!= str) {array + +;
	n++;
} return N;
} void Buildgraph () {	Initializes the matrix for (int i=0; i<n; i++) {for (int j=0; j<n; J + +) {matrix[i][j]=0;
		(int i=0; i< (size_a-1); i++) {int u = GetPos (Node,a[i]) based on A;

		int v = getpos (node,a[i+1]);
		Matrix[u][v]=1;
	Matrix[v][u]=1;
		///(B) Establish side information for (int i=0; i< (size_b-1); i++) {int u = GetPos (Node,b[i]);

		int v = getpos (node,b[i+1]);
		Matrix[u][v]=1;
	Matrix[v][u]=1;
	int u = getpos (Node,strbegin);

	int v = getpos (node,strend);

Floyd_warshall (); } void Floyd_warshall () {//1, initialization for (int i=0; i<n; i++) {for (int j=0; j<n; J + +) {if (I!= J &AMP;&A mp
				MATRIX[I][J] > 0) {dist[i][j] = matrix[i][j];
			PATH[I][J] = i;
				else {dist[i][j] = INF;
			PATH[I][J] =-1;
			}}//2, Floyd core three-layer loop for (int k=0; k<n; k++) {for (int i=0; i<n; i++) {for (int j=0; j<n; j +)
					{if (Dist[i][j] > Dist[i][k] + dist[k][j]) {dist[i][j] = Dist[i][k] + dist[k][j]; PATH[I][J] = Path[k][j];
	}}//3, output result int u = getpos (Node,strbegin);
	int v = getpos (node,strend);
		while (U!= v) {v = path[u][v];
	length++; 
length + +;
		int main () {//while (0) {length = 0;
		cin>>strbegin>>strend;
		if (Strbegin = = strend) cout<< "1" <<endl;
			else {buildgraph ();
		cout<<length<<endl;
	System ("pause");
return 0; }



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.