1. 程式人生 > >娛樂中心

娛樂中心

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int INF = 0x3f3f3f3f;

int max_length[7], sum_length[7] = { 0 };
int main(void)
{
	int book[10], i, j, k, n = 5, m = 10, t1, t2, t3, u, v, max_my, start, temp, *p, res_1, res_2;
	int e[6][6] =
	{
		{ 0,0,0,0,0,0 },
	{ 0,0,13,INF,4,INF },
	{ 0,13,0,15,INF,5 },
	{ 0,INF,INF,0,12,INF },
	{ 0,4,INF,12,0,INF },
	{ 0,INF,INF,6,3,0 }
	};
	for (k = 1; k <= n; k++)
		for (i = 1; i <= n; i++)
			for (j = 1; j <= n; j++)
				if (e[i][j]>e[i][k] + e[k][j])
					e[i][j] = e[i][k] + e[k][j];
	for (i = 1; i <= n; i++)
	{
		max_my = -INF;
		for (j = 1; j <= n; j++)
		{
			sum_length[i] += e[i][j];
			sum_length[i] += e[j][i];
			temp = e[i][j] + e[j][i];
			if (temp>max_my);
			{
				max_my = temp;
				max_length[i] = max_my;
			}
		}
	}
	p = max_element(max_length, max_length + 7);
	temp = 0;
	for (i = 1; i <= n; i++)
	{
		if (max_length[i] == *p)
		{
			temp++;
			(temp == 1) ? res_1 = i : res_2 = i;
		}
	}
	if (temp == 1)
	{
		cout << "娛樂中心應建在頂點" << res_1 << "處" << endl;
	}
	if (temp == 2)
	{
		cout << "娛樂中心應建在頂點" << (sum_length[res_1]<sum_length[res_2] ? res_1 : res_2) << "處" << endl;
	}
	return 0;
}