poj1258
跟上题就改了两行就出来了。这题标准最小生成树,即,支撑树上的总权值。
#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;
//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)
{
//memset(map, inf, sizeof(map));
for (int i = 0; i < t; i++)
{
for (int j = 0; j < t; j++)
{
scanf("%d", &map[i][j]);
}
}
cout << prim(map, t) << endl;
}
return 0;
}
补充:软件开发 , C++ ,