【HDU1233】还是畅通工程(MST基础题)

无坑,裸题。直接敲就恩那个AC。

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <numeric> #define typec int
using namespace std; const int V = ;
const int inf = 0xffff;
int vis[V]; typec lowc[V], Map[V][V]; typec prim (typec cost[][V], int n) {
int i, j, p;
typec minc, res = ;
memset(vis, , sizeof(vis));
vis[] = ;
for (int i = ; i < n; ++ i) lowc[i] = cost[][i];
for (int i = ; i < n; ++ i) {
minc = inf; p = -;
for (j = ; j < n; ++ j) {
if ( == vis[j] && minc > lowc[j]) {
minc = lowc[j]; p = j;
}
}
if (inf == minc) return -;
res += minc; vis[p] = ;
for (j = ; j < n; ++ j) {
if ( == vis[j] && lowc[j] > cost[p][j])
lowc[j] = cost[p][j];
}
}
return res;
} int main () {
int n;
while (~scanf("%d", &n) && n) {
for (int i = ; i < V; ++ i) {
for (int j = ; j < V; ++ j) {
if (i == j) Map[i][j] = ;
else Map[i][j] = inf;
}
}
for (int i = ; i < n * (n - ) / ; ++i) {
int x, y, c; scanf("%d%d%d", &x, &y, &c);
if (x == y) continue;
Map[x - ][y - ] = Map[y - ][x - ] = min(Map[x - ][y - ], c);
}
cout << prim(Map, n) << endl;
}
return ;
}
上一篇:try-catch中的finally块


下一篇:windows phone 之笔势