【HDU1879】继续畅通工程(MST基础题)

时间:2022-05-06 15:54:23

真心大水题。。。不多说。

 #include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <algorithm>
#include <numeric> #define typec int
using namespace std; const typec inf = 0xffff;
const int V = ;
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 (i = ; i < n; ++ i) lowc[i] = cost[][i];
for (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)) {
if (!n) break;
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, w, ok;
scanf("%d%d%d%d", &x, &y, &w, &ok);
if (!ok) {
Map[x - ][y - ] = Map[y - ][x - ] = min(Map[x][y], w);
} else {
Map[x - ][y - ] = Map[y - ][x - ] = ;
}
} cout << prim(Map, n) << endl;
}
return ;
}