标签:style blog color io os for sp div log
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #define MAX 102 void read(); using namespace std; int map[MAX][MAX],best[MAX],n; bool visit[MAX]; int main() { //freopen("in.txt","r",stdin); while(scanf("%d",&n)!=EOF) { memset(visit,false,sizeof(visit)); read(); int ans=0,k; for(int i=1;i<=n;i++) { best[i]=map[1][i]; } visit[1]=true; while(1) { int min=1 << 30; k=0; for(int i=1;i<=n;i++) { if(!visit[i] && best[i]<min) { min=best[i]; k=i; } } if(k==0)break; ans+=min; visit[k]=true; for(int i=1;i<=n;i++) { if(!visit[i] &&map[k][i]<best[i]) { best[i]=map[k][i]; } } } cout << ans << endl; } return 0; } void read() { for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { scanf("%d",&map[i][j]);//输入邻接矩阵 } } }
标签:style blog color io os for sp div log
原文地址:http://www.cnblogs.com/balfish/p/4015155.html