Beijing Subway Ride Route inquiry

Source: Internet
Author: User
Tags int size strcmp

"Problem description"

Write a program to achieve the Beijing Subway shortest Ride (station) line query, input as the starting station name and destination station name, output from the start station to the destination station of the shortest ride station transfer line. Note: 1. The Dijkstra algorithm is required to achieve 2. In the actual test, the data file was adjusted to make the input between the two stations only a shortest path.

"Input Form"

The file bgstations.txt is a data file (downloadable from course information at the course website), including the line and station information of the Beijing Subway. The format is as follows:

< subway line total number of bars >

< line 1> < Line 1 Station number >

< station name 1> < transfer status >

< station name 2> < transfer status >

...

< line 2> < Line 2 Station number >

< station name 1> < transfer status >

< station name 2> < transfer status >

...

Description: The first act of the document the number of total lines of the subway; the first number in the second line is a subway line number (for example, 1 is Line Line 1), the second number is the number of terminus of the subway line (such as line Line 1 has 23 stations), two are separated by a space; the third row two data is the subway station name and the transfer state (0 is the non transfer station, 1 is a transfer station, separated by a space between the two data, the same as the same, in turn, the line of other subway station information. After a line of information is the next subway line information, the same format. If a subway line for the loop, then the first station and the last station information is the same (such as Beijing subway Line Line 2, the first station information "Xizhimen 1", the last station information for "Xizhimen 1"). For example, the Bgstations.txt file (available from course information in the course website) can be read as follows:

12

1 23

Apple Orchard 0

Ancient city 0

Octagonal Amusement Park 0

Babaoshan 0

Yuquan Road 0

Five Pine 0

Wan Shou Road 0

Princess Grave 1

Military Museum 1

0 of the land of the clover

Nanrice Lou 0

Renaissance Gate 1

Xidan 1

...

2 19

Xizhimen 1

Jishuitan 0

Gulou Street 1

...

Xizhimen 1

...

The document shows that the current Beijing subway has 12 lines (excluding suburban routes), followed by each line of information.

Open the current directory of the file Bgstations.txt, read the subway line information, and from the standard input into the starting station and destination station name (both string, each one row).


"Output Form"

The travel information from the starting station to the destination station requires the least number of rides. Transfer Information format is as follows:

SSN-N1 (M1)-s1-n2 (m2)-...-ESN

Among them: SSN and ESN are the starting station name and the destination station name respectively, N is the subway line number, M is the number of the ride station.
"Sample Input"

West Tu Cheng

Beijing West railway station

"Sample Output"

West Tu-10 (1)-Know-Spring Road-13 (2)-Xizhimen-4 (2)-National Library-9 (4)-Beijing West railway station

"Sample description"

Open file Bgstations.txt, read the subway line information, and read the query from the standard input station called "West Tu", the purpose of the station called "Beijing West railway station." The results of the program running the least number of rides between the two stations for "West Tu station take line Line 10 1 station to know Spring Road station transfer Line 13 line 2 station to Xizhimen Station transfer Line 4 take 2 station to the National Library Station transfer Line 9 station to Beijing West station."

#include <stdio.h> #include <stdlib.h> #include <string.h> #define Maxstrlen #define INF 0x3f3f3f3f
	#define MAXSTATNUM typedef struct _EDGE {int adjseq;
	int weight;
	int lineseq;
struct _edge *next;

} Edge;
	typedef struct _STATION {char name[maxstrlen];
	int trans;
	int lineseq;
	int statseq;
Edge *link;

} station;
	typedef struct _LINE {int size;
	int seq;
	int isloop;
struct _station *list;

} line;
	typedef struct _RAILWAY {int size;
struct _station list[maxstatnum];

} Railway;
	void Insert_edge (station *head, int end, int lineseq) {Edge *rear = head->link;
	Edge *newedge = (Edge *) malloc (sizeof (edge));
		if (NewEdge = = NULL) {printf ("Allocation failure");
	Exit (1);
	} newedge->weight = 1;
	Newedge->lineseq = Lineseq;
	Newedge->adjseq = end;
	Newedge->next = NULL;
	if (rear = = NULL) {head->link = NewEdge; else {while (Rear->next!= NULL) rear = rear->next;//reach the rear Rear->next = NewEdge; Add the new Edge at the rear} void Input_station (railway *railway, line line, int seq_inline, int seq_inrail) {// Input a station in a processed line to a railway system, and in this case seq_inrail = = Railway->size strcpy (railway-&
	Gt;list[seq_inrail].name, Line.list[seq_inline].name);
	Railway->list[seq_inrail].trans = Line.list[seq_inline].trans;
	Railway->list[seq_inrail].lineseq = Line.seq;
	Railway->list[seq_inrail].statseq = Seq_inrail;
	Railway->list[seq_inrail].link = NULL;

	railway->size++; if (Seq_inline = = 0) {//The station if (line.isloop) {//The line is a loop, with the same station as start
			D End Insert_edge (& (Railway->list[seq_inrail]), LINE.LIST[1].STATSEQ, LINE.SEQ);
		Insert_edge (& (Railway->list[seq_inrail]), LINE.LIST[LINE.SIZE-2].STATSEQ, LINE.SEQ);  else {//the start station in a NON-LOOP has no previous station Insert_edge (& (Railway->list[seq_inrail)), Line.list[1].statseq, LiNE.SEQ); ' Else if ' (seq_inline = = line.size-1-Line.isloop) {//The last station if (line.isloop) {//The ' is a loo P, with the same station as start and End Insert_edge (& (Railway->list[seq_inrail)), Line.list[0].statseq, LINE.S
			EQ);
		Insert_edge (& (Railway->list[seq_inrail]), LINE.LIST[LINE.SIZE-3].STATSEQ, LINE.SEQ);  else {//the last station in a NON-LOOP has no following station Insert_edge (& (Railway->list[seq_inrail)),
		Line.list[line.size-2].statseq, LINE.SEQ);
		} else {Insert_edge (& (Railway->list[seq_inrail]), Line.list[seq_inline + 1].statseq, line.seq);
	Insert_edge (& (Railway->list[seq_inrail]), LINE.LIST[SEQ_INLINE-1].STATSEQ, LINE.SEQ); } void Update_station (railway *railway, line line, int seq_inline, int seq_inrail) {//update a station ' s edges in a RA Ilway system if (Seq_inline = = 0) {//the the station if (line.isloop) {//The-is-a loop, with the same stati On as start and END Insert_edge (& (Railway->list[seq_inrail]), LINE.LIST[1].STATSEQ, LINE.SEQ);
		Insert_edge (& (Railway->list[seq_inrail]), LINE.LIST[LINE.SIZE-2].STATSEQ, LINE.SEQ);  else {//the start station in a NON-LOOP has no previous station Insert_edge (& (Railway->list[seq_inrail)),
		Line.list[1].statseq, LINE.SEQ); ' Else if ' (seq_inline = = line.size-1-Line.isloop) {//The last station if (line.isloop) {//The ' is a loo P, with the same station as start and End Insert_edge (& (Railway->list[seq_inrail)), Line.list[0].statseq, LINE.S
			EQ);
		Insert_edge (& (Railway->list[seq_inrail]), LINE.LIST[LINE.SIZE-3].STATSEQ, LINE.SEQ);  else {//the last station in a NON-LOOP has no following station Insert_edge (& (Railway->list[seq_inrail)),
		Line.list[line.size-2].statseq, LINE.SEQ);
		} else {Insert_edge (& (Railway->list[seq_inrail]), Line.list[seq_inline + 1].statseq, line.seq); Insert_edge (& (Railway->list[seq_inrail]), LINE.LIST[SEQ_INLINE-1].STATSEQ, LINE.SEQ); {In_rail (railway Railway, Char name[]) {//Find the station and return it sequence number in Railsystems int ite
	R_rail;
			for (iter_rail = 0; iter_rail < railway.size; iter_rail++) {if (strcmp (railway.list[iter_rail].name, name) = = 0)
	return iter_rail; } return-1; Not found} void Input_line (railway *railway, line line) {//inputting a processed line into a railway system int seq
	_inline, Seq_inrail; if (railway->size = 0)//empty system {//Initialize a railway system for (seq_inline = 0; Seq_inline < line.si Ze seq_inline++) {//In a empty railway system, the inline seq equals the inrail seq Railway->list[seq_inline].link
			= NULL;
		Input_station (railway, line, Seq_inline, Seq_inline);
		} else {int seq_fromsize = railway->size; for (seq_inline = 0; seq_inline < Line.size-line.isloop; seq_inline++) {seq_inrail = In_rail (*railway, Line.lis T[seq_inline].name); if (Seq_inrail!=-1) {//This station already exists on the railway system Update_station (railway, line, Seq_inlin
			E, seq_inrail);
				else {//This are a new station Input_station (railway, line, Seq_inline, seq_fromsize);
			seq_fromsize++;  }} void Process_line (railway *railway, line *line) {//pre-process the STATSEQ of the stations to the
	Inputting of stations) int seq_inline, seq_inrail; if (railway->list = NULL) {//Empty railway system for (seq_inline = 0; seq_inline < line->size; seq_inline++
	) Line->list[seq_inline].statseq = Seq_inline;
		else {int seq_fromsize = railway->size; for (seq_inline = 0; seq_inline < line->size; seq_inline++) {seq_inrail = In_rail (*railway, Line->list[seq_
			Inline].name); if (Seq_inrail!=-1) {//This station already exists in the railway system LINE-&GT;LIST[SEQ_INLINE].STATSEQ = seq
			_inrail; else {//new station LINE-&GT;list[seq_inline].statseq = seq_fromsize;
			seq_fromsize++; }} void Dijkstra (railway *railway, Char startname[], char destname[], int path[]) {int start = In_rail (*railway
	, StartName);
	int dest = In_rail (*railway, destname); 
	int *dist = (int *) malloc (railway->size*sizeof (int)); Dist[] Stores the minimum distance from start to indexed station//0 means this station has been enclosed
	(Dist, INF, railway->size*sizeof (int));

	Dist[start] = 0;
	Edge *link = railway->list[start].link;
		while (link!= NULL) {Dist[link->adjseq] = link->weight;
		PATH[LINK-&GT;ADJSEQ] = start;
	link = link->next;
		while (dist[dest] = = inf) {int min = inf, Minstat = dest, iter; for (iter = 0, iter < railway->size; iter++) {if (Dist[iter]!= 0 && dist[iter] < min) {//not E
				Nclosed, and find the minimum min = Dist[iter];
			Minstat = iter;
		} link = railway->list[minstat].link; if (link = NULL) exit (1);  while (link!= NULL) {if (Dist[minstat] + link->weight < Dist[link->adjseq]) {DIST[LINK-&GT;ADJSEQ]
				= Dist[minstat] + link->weight;
			PATH[LINK-&GT;ADJSEQ] = Minstat;
		link = link->next;
	} Dist[minstat] = 0;

	Free (dist);
		/* Just for debugging while (dest!= start) {printf ("%s<-", railway->list[dest].name);
	Dest = Path[dest];
	printf ("%s\n", railway->list[start].name);
	*/} int Get_lineseq (railway railway, int stat1, int stat2) {Edge *link = Railway.list[stat1].link;
		while (link!= NULL) {if (Link->adjseq = = STAT2) return link->lineseq;
	link = link->next; } return-1; Not connected} void Depict_path (railway *railway, int start, int dest, int path[]) {int *track = (int *) malloc (rail
	way->size*sizeof (int));
	memset (track, INF, railway->size*sizeof (int));
	int iter = 0;
		while (dest!= start) {track[iter++] = dest;
	Dest = Path[dest];

	} Track[iter] = start; int i = 0, J= iter;
		while (I < j) {//reverse track[] int tmp;
		TMP = Track[j];
		TRACK[J] = Track[i];
		Track[i] = tmp;
	i++, j--; /*for (i = 0; I <= iter; i++) printf ("%s%d-", Railway->list[track[i]].name, Railway->list[track[i]].lines
	EQ); */int currstats = 1;
	printf ("%s-", railway->list[track[0]].name); for (i = 1; i < iter; i++) {if Get_lineseq (*railway, Railway->list[track[i-1]].statseq, railway->list[t RACK[I]].STATSEQ)!= get_lineseq (*railway, Railway->list[track[i + 1]].statseq, Railway->list[track[i]]. STATSEQ)) {//the previous station and the next one isn't on the same line:transfering printf ("%d (%d)-%s-", Get_li Neseq (*railway, Railway->list[track[i-1]].statseq, Railway->list[track[i]].statseq), Currstats, railway->
			List[track[i]].name);
		Currstats = 1;
		else {currstats++; printf ("%d (%d)-%s\n", Get_lineseq (*railway, Railway->list[track[i-1]].statseq, Railway->list[track[i]). Statseq), Currstats, railway->list[track[iter]].name);
	int main () {FILE *fin;
	Fin = fopen ("Bgstations.txt", "R");
	
	if (fin = = NULL) exit (1);
	int LineNum, LINESEQ, StatNum, trans;
	int I, J;
	Char Statname[maxstrlen];
	Railway *beijing = (Railway *) malloc (sizeof (railway));
	beijing->size = 0;
	Line *newline = (line *) malloc (sizeof);

	FSCANF (FIN, "%d\n", &linenum);
		for (i = 1; I <= linenum i++) {fscanf (Fin, "%d%d\n", &lineseq, &statnum);
		Newline->seq = Lineseq; 
		Newline->size = StatNum;
		Newline->isloop = 0;
		Newline->list = (Station *) malloc (statnum*sizeof (station));
			for (j = 0; J < StatNum; J + +) {fscanf (Fin, "%s%d\n", Statname, &trans);
			Newline->list[j].lineseq = i;
			strcpy (Newline->list[j].name,statname);
		Newline->list[j].trans = trans;
		} if (strcmp (newline->list[0].name, statname) = = 0) Newline->isloop = 1;
		Process_line (Beijing, newline);
		Input_line (Beijing, *newline); FreeNewline->list);
	FSCANF (FIN, "\ n");
	} char Startname[maxstrlen], Destname[maxstrlen];
	scanf ("%s", StartName);
	scanf ("%s", destname);
	int *path = (int *) malloc (beijing->size*sizeof (int));

	Dijkstra (Beijing, StartName, destname, Path);
	Depict_path (Beijing, In_rail (*beijing, StartName), In_rail (*beijing, Destname), Path); /* Just for debugging (i = 0 i < beijing->size; i++) {printf ("%s%d%d\n", Beijing->list[i].name, Beiji
		Ng->list[i].statseq, Beijing->list[i].trans);
		Edge *curr = beijing->list[i].link; while (Curr!= NULL) {printf ("%s%d%d\n", Beijing->list[curr->adjseq].name, Curr->adjseq, Beijing->lis
			T[curr->adjseq].trans);
		Curr = curr->next;
	}}*/free (newline);
	Free (path);
	Fclose (Fin);
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.