POJ_2485 Highways
赤裸裸的最小生成树,直接prim算法解决
#include <stdio.h>
#define num 501
#define INF 100000000
int t, n, i, j, k, min, max, tmp, map[num][num], path[num];
void prim()
{
for (i = 2; i <= n; ++ i)
{
path[i] = map[1][i];
}
path[1] = -1;
max = 0;
for (i = 1; i < n; ++ i)
{
min = INF;
for (j = 1; j <= n; ++ j)
{
if (path[j] != -1 && path[j] < min)
{
min = path[j];
tmp = j;
}
}
max = min > max ? min : max;
path[tmp] = -1;
for (j = 1; j <= n; ++ j)
{
if (path[j] > map[tmp][j])
{
path[j] = map[tmp][j];
}
}
}
printf("%d\n", max);
}
int main()
{
scanf("%d", &t);
while (t --)
{
scanf("%d", &n);
for (i = 1; i <= n; ++ i)
{
for (j = 1; j <= n; ++ j)
{
scanf("%d", &map[i][j]);
}
}
prim();
}
return 0;
}
#define num 501
#define INF 100000000
int t, n, i, j, k, min, max, tmp, map[num][num], path[num];
void prim()
{
for (i = 2; i <= n; ++ i)
{
path[i] = map[1][i];
}
path[1] = -1;
max = 0;
for (i = 1; i < n; ++ i)
{
min = INF;
for (j = 1; j <= n; ++ j)
{
if (path[j] != -1 && path[j] < min)
{
min = path[j];
tmp = j;
}
}
max = min > max ? min : max;
path[tmp] = -1;
for (j = 1; j <= n; ++ j)
{
if (path[j] > map[tmp][j])
{
path[j] = map[tmp][j];
}
}
}
printf("%d\n", max);
}
int main()
{
scanf("%d", &t);
while (t --)
{
scanf("%d", &n);
for (i = 1; i <= n; ++ i)
{
for (j = 1; j <= n; ++ j)
{
scanf("%d", &map[i][j]);
}
}
prim();
}
return 0;
}