首页 > 代码库 > PTA 07-图4 哈利·波特的考试 (25分)

PTA 07-图4 哈利·波特的考试 (25分)

/* 1.用floyd 求多源最短路径~    2.找出节点k到其他节点的最短路径的最大值 MAX[k].   3 在找出MAX数组中最小的即为所求~~ 如果最小值为INF~~~ 说明图不连通! 输出0 */#include "iostream"using namespace std;#define INF 101int map[101][101];void floyd(int v) {	int i, j, k;	for(k=1;k<=v;k++)		for(i=1;i<=v;i++)			for (j = 1; j <= v; j++) {				if (map[i][k] + map[k][j] < map[i][j])					map[i][j] = map[i][k] + map[k][j];			}}int main() {	int v, e;	cin >> v >> e;	for (int i = 1; i <= v; i++)		for (int j = 1; j <= v; j++) {			if (i == j)				map[i][j] = 0;			else				map[i][j] = INF;		}	while (e--) {		int a, b, c;		cin >> a >> b >> c;		map[a][b] = map[b][a] = c;	}	floyd(v);	int MIN = INF;	int MAX = 0;	int index;	for (int i = 1; i <= v; i++) {		MAX = 0;		for (int j = 1; j <= v; j++) {			if (map[i][j] >= MAX)				MAX = map[i][j];		}		if (MAX < MIN)		{			MIN = MAX;			index = i;		}	}	if (MIN == INF) {		cout << "0";		return 0;	}	cout << index << " " << MIN << endl;	return 0;}

  

PTA 07-图4 哈利·波特的考试 (25分)