poj2485
稠密图的prim最小生成树中的最长边,加一个标记变量即可
#include <iostream>
#include <stdio.h>
using namespace std;
#pragma optimize( "/Ox", on )
#pragma warning(disable:4996)
#define maxn 505
#define typec int
#define inf 0x3f3f3f3f
int map[maxn][maxn];
int vis[maxn]; typec lowc[maxn];
typec prim(typec cost[][maxn],int n)
{
int i, j, p;
typec minc, res = 0;
memset(vis, 0, sizeof(vis));
vis[0] = 1;
for (i = 1; i < n; i++) lowc[i] = cost[0][i];
for (i = 1; i < n; i++)
{
minc = inf; p = -1;
for (j = 0; j<n; j++)
if (0 == vis[j] && minc > lowc[j])
{
minc = lowc[j]; p = j;
}
if (res < minc) res = minc;//here is the max edge in minimum spanning tree
vis[p] = 1;
if (inf == minc) return -1;
//res += minc; vis[p] = 1;
for (j = 0; j<n; j++)
if (0 == vis[j] && lowc[j] > cost[p][j])
lowc[j] = cost[p][j];
}
return res;
}
int main()
{
ios::sync_with_stdio(false);
int t;
while(cin >> t)
while (t--)
{
int n;
cin >> n;
//memset(map, inf, sizeof(map));
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
scanf("%d", &map[i][j]);
}
}
cout << prim(map, n) << endl;
}
return 0;
}
补充:软件开发 , C++ ,