poj 图相关之2485Highways
简单的Prime算法
Accepted 540K 375MS
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<math.h>
#include<algorithm>
#include<memory.h>
#include<vector>
#include<queue>
#include<map>
#define MAXNUM 505
#define INF 66000;
using namespace std;
int N, W[MAXNUM][MAXNUM];
bool mark[MAXNUM];
int Prime()
{
int i, j, k, tmp, t, ans;
vector<int> record;
memset(mark, 0, sizeof(mark));
mark[0] = 1;
record.push_back(0);
ans = 0;
for (i = 1; i < N; i++)
{
tmp = INF;
for (j = 0; j < record.size(); j++)
{
for (k = 0; k < N; k++)
{
if (!mark[k] && W[record[j]][k] < tmp)
{
tmp = W[record[j]][k];
t = k;
}
}
}
mark[t] = 1;
record.push_back(t);
ans = max(ans, tmp);
}
return ans;
}
int main()
{
//freopen("1.txt", "r", stdin);
int i, j, t, Cases;
scanf("%d", &Cases);
while (Cases > 0)
{
scanf("%d", &N);
for (i = 0; i < N; i++)
for (j = 0; j < N; j++)
scanf("%d", &W[i][j]);
printf("%d\n", Prime());
Cases--;
}
}