继续畅通工程(kruskal prim)
#include <cstdio>
#define Max 0x3f3f3f3f
int N, Map[105][105], sum;
void prim()
{
int k, Min, dis[105], vis[105];
for(int i = 1; i <= N; i++) {
dis[i] = Map[1][i];
vis[i] = 0;
}
vis[1] = 1;
for(int i = 1; i < N; i++) {
Min = Max;
for(int j = 1; j <= N; j++)
if(!vis[j] && dis[j] < Min) {
k = j;
Min = dis[j];
}
vis[k] = 1;
sum += Min;
for(int j = 1; j <= N; j++) {
if(!vis[j] && dis[j] > Map[k][j])
dis[j] = Map[k][j];
}
}
}
int main()
{
int Start, End, Cost, Dis;
while(scanf("%d", &N) && N) {
for(int i = 1; i <= N; i++)
for(int j = 1; j <= N; j++)
Map[i][j] = Map[j][i] = Max;
for(int i = 1; i <= N * (N - 1) / 2; i ++) {
scanf("%d %d %d %d", &Start, &End, &Cost, &Dis);
if(!Dis) {Map[Start][End] = Map[End][Start] = Cost;}
else {Map[Start][End] = Map[End][Start] = 0;}
}
sum = 0;
prim();
printf("%d\n", sum);
}
return 0;
}