XYNU 2177: dijkstra

Posted by prexep on Mon, 07 Oct 2019 10:55:03 +0200

2177: Xiao Ming who loves traveling

Time limit: 1 Sec memory limit: 128 MB
Submitted: 89 Solutions: 39
The status of your question: Completed
[Submission] [Status] [Discussion Edition]
Title Description
Xiao Ming wants to travel, but there are too many roads on the traffic map. It's not good to have too many roads. Every time we have to go from one town to another, there are many road schemes to choose from, and some of them are much shorter than others. This makes Xiao Ming very troubled.

Now, knowing the starting point and the end point, please calculate the shortest distance you need to walk from the starting point to the finish line.
input
This topic contains multiple sets of data, please process to the end of the file.
The first row of each data set contains two positive integers N and M (0 < N < 200, 0 < M < 1000), representing the number of existing towns and the number of roads built. Towns are numbered 0-N-1, respectively.
Next is the road information of Line M. Each row has three integers A, B, X (0 <= A, B < N, A!= B, 0 < X < 10 000), indicating that there is a two-way road between town A and town B with length X.
Then the next line has two integers S, T (0<=S, T<N), representing the starting point and the ending point respectively.
output
For each group of data, please output the shortest distance Xiaoming needs to walk in one line. If there is no route from S to T, output - 1.
sample input
3 3
0 1 1
0 2 3
1 2 1
0 2
3 1
0 1 1
1 2
sample output
2
-1
The problem is complicated.

#include<iostream>
using namespace std;
int N,M,T,S,a,b,c,ans;
int start[1010];
int end[1010];
#include<memory.h>
#define inf 0x3f3f3f3f
#define max_size 1010

int map[max_size][max_size];

/* dijkstra The core code, parameter n, is the total number of nodes. start and end represent the starting and ending points respectively, returning the shortest path from the beginning to the end point, and returning to -1 when the starting point and terminal point are not connected. */ 
int dijkstra( int start, int end, int n ) {
	int dis[max_size], minn, next;    //dis represents the distance from each node to the starting point, inf means unreachable 
	bool flag[max_size];    //flag[i]==true indicates that the shortest distance from I to the starting point has been determined. 
	memset( dis, 0x3f, sizeof( dis ) );    // !!! memset function must be used carefully, understand its operation mechanism!!! 
	memset( flag, false, sizeof( flag ) );
	dis[start] = 0;    //The starting point to its own distance is 0.
	/*Think about it.
	Why do the following for statements execute conditions? Flag[end] & index! = -1
	Are both conditions necessary?
	Which one should be removed if one is to be removed?
	*/ 
	for( int index = start; !flag[end] && index != -1; index = next ) {
		flag[index] = true;
		minn = inf;
		next = -1;
		for( int i = 0; i <= n; i ++ ) {
			if( dis[i] > dis[index] + map[index][i] ) {
				dis[i] = dis[index] + map[index][i];
			}
			if( dis[i] < minn && !flag[i] ) {
				minn = dis[i];
				next = i;
			}
		}
	}
	if( dis[end] == inf ) {
		return -1;
	}
	return dis[end];
}
int main(){
	while(cin>>N>>M){
		memset(map,inf,sizeof(map));
		for(int i=0;i<M;i++){
			cin>>a>>b>>c;
			if(a==b) map[a][b] = 0;
			map[a][b] = min(map[a][b],c);
			map[b][a] = min(map[b][a],c);
//			map[a][b] = map[b][a] =  c;
		}
		cin>>S>>T;
		ans = dijkstra(S,T,N);
		cout<<ans<<endl;
	}
	return 0;
}