萌新求教 全re c++
查看原帖
萌新求教 全re c++
245152
X3B0A1楼主2020/6/11 20:05

rt ,本地的答案是正确的,谢谢

#include<iostream>
using namespace std;
const int MAX = 10+1;
int Adj[MAX][MAX];	//存储图 
int vertexNum;		//顶点数 
int edgeNum;		//边数 
int source;			//出发点 
int dis[MAX];		//最低按路径 
int Queue[MAX];	//已经求出最短路的点,将其值置true 
int main() {
	cin >> vertexNum >> edgeNum >> source;
	int inf = 99999999;
	for( int i = 1; i <= vertexNum; i++ ){
		for( int j = 1; j <= vertexNum; j++){
			if( i == j ){
				Adj[i][j] = 0;
			}else{
				Adj[i][j] = inf;
			}
		}
	}
	
	for( int i = 1; i <= vertexNum; i++ ){
		if( i == source ){
			dis[i] = 0;
		}else{
			dis[i] = inf;
		} 
	} 
	
	int u, v, w;	
	for( int i = 1; i <= edgeNum; i++ ){
		cin >> u >> v >> w;
		Adj[u][v] = w;
	}
	
	for( int i = 1; i <= vertexNum; i++ ){
		dis[i] = Adj[source][i];
	}
	
	int temp = vertexNum;
	while( temp-- ){
		int minDis = inf;
		int minP;
		for( int i = 1; i <= vertexNum; i++ ){
			if( Queue[i] == 0 && dis[i] < minDis ){
				minDis = dis[i];
				minP = i;
			}	
		}
		Queue[minP] = 1;
		for( int i = 1; i <= vertexNum; i++ ){
			if( Queue[i] == 0 && dis[i] > dis[minP] + Adj[minP][i] ){
				dis[i] = dis[minP] + Adj[minP][i];
			}
		}
	} 
	
	for( int i = 1; i <= vertexNum; i++ ){
		cout << dis[i] << " " ;
	}
	return 0;
} 
2020/6/11 20:05
加载中...