最小生成树的水提,纪念一下我的第一到图论题
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define INF 1000000
using namespace std;
int judge[105];
int cost[105][105];
int mincost[105];
int main(){
int n,ans;
while(scanf("%d",&n)!=EOF){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
scanf("%d",&cost[i][j]);
}
}
for(int i=1;i<=n;i++){
mincost[i]=INF;
judge[i]=0;
}
mincost[1]=0;
ans=0;
while(1){
int x=-1;
for(int i=1;i<=n;i++){
if(judge[i]==0&&(x==-1||mincost[i]<mincost[x])) x=i;
}
if(x==-1) break;
judge[x]=1;
ans+=mincost[x];
for(int i=1;i<=n;i++){
mincost[i]=min(mincost[i],cost[x][i]);
}
}
printf("%d\n",ans);
}
return 0;
}