POJ 2485 Highways
解题思路:Prim算法求解最小生成树,
#include<iostream>
using namespace std;
#define MaxNum 1e8
int main()
{
int m, n, min, start, tempS, MinMax;
int edge[505][505];
int dist[505];
int path[505];
bool visited[505];
scanf("%d", &m);
while (m--)
{
scanf("%d", &n);
dist[0] = 0;
start = 0;
path[0] = 0;
fill(&dist[1], &dist[n], MaxNum);
fill(&path[1], &path[n], -1);
memset(visited, 0, sizeof(bool) * 505);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
scanf("%d", &edge[i][j]);
while (start != -1)
{
visited[start] = true;
tempS = -1;
min = MaxNum;
for (int i = 0; i < n; i++)
{
if (!visited[i])
{
if (dist[i] > edge[start][i])
{
dist[i] = edge[start][i];
path[i] = start;
}
if (min > dist[i])
{
min = dist[i];
tempS = i;
}
}
}
start = tempS;
}
MinMax = 0;
for (int i = 0; i < n; i++)
if (MinMax < edge[path[i]][i])
MinMax = edge[path[i]][i];
printf("%d\n", MinMax);
}
return 0;
}