【HDU1102】Constructing Roads(MST基础题)

最小生成树水题。prim一次AC

 #include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <algorithm>
#include <numeric> #define typec int
using namespace std; const int V = ;
const int inf = 0xffff;
int vis[V]; typec lowc[V];
int Map[][]; 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 () {
//cout << inf <<endl;
int n;
while (cin >> n) {
for (int i = ; i < n; ++ i) {
for (int j = ; j < n; ++ j) {
scanf("%d", &Map[i][j]);
}
}
int ok_line; cin >> ok_line;
for (int i = ; i < ok_line; ++ i) {
int s, e;
scanf("%d%d", &s, &e);
Map[s - ][e - ] = Map[e - ][s - ] = ;
}
cout << prim(Map, n) << endl;
}
return ;
}
上一篇:PhalGo-介绍


下一篇:RabbitMQ(二):Java 操作队列