Agri-Net
pku 1258:http://acm.pku.edu.cn/JudgeOnline/problem?id=1258
最小生成树。prim或kruskal。由于prim比较好敲……幸好有做这题不然就忘了准备模板了囧
-
#include<iostream>
-
#define MAX 200000
-
using namespace std;
-
int n;
-
int edge[105][105];
-
-
int prim(){
-
int lowcost[105],mincost;
-
int i,j,k;
-
for(i=1;i<n;i++){
-
lowcost[i]=edge[0][i];
-
}
-
lowcost[0]=0;
-
int sum = 0;
-
for(i=1;i<n;i++){
-
mincost = MAX;
-
j=1;
-
k=1;
-
while(j<n){
-
if(lowcost[j]!=0&&lowcost[j]<mincost){
-
mincost = lowcost[j];
-
k = j;
-
}
-
j++;
-
}
-
sum += mincost;
-
lowcost[k] = 0;
-
for(j=1;j<n;j++){
-
if(edge[k][j]<lowcost[j]){
-
lowcost[j]=edge[k][j];
-
}
-
}
-
}
-
return sum;
-
}
-
-
int main(){
-
while(cin>>n){
-
for(int i=0;i<n;i++){
-
for(int j=0;j<n;j++){
-
cin>>edge[i][j];
-
}
-
}
-
cout<<prim()<<endl;
-
}
-
return 0;
-
}