和2421基本上一模一样……
/* author:jxy lang:C/C++ university:China,Xidian University **If you need to reprint,please indicate the source** */ #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #define INF 1E9 using namespace std; int d[101][101]; int vis[101]; int dis[101]; int K; int main() { int n,m; while(~scanf("%d",&n)&&n) { memset(vis,0,sizeof(vis)); memset(dis,127,sizeof(dis)); int i,j,a,b; K=1; for(i=0;i<n;i++) for(j=0;j<n;j++) scanf("%d",&d[i][j]); int ans=0,Min,t; int now=0; for(i=0;i<n;i++) { Min=INF; for(j=0;j<n;j++) { if(dis[j]>=Min||vis[j])continue; Min=dis[j];now=j; } vis[now]=1; if(now)ans+=Min; for(j=0;j<n;j++) { if(vis[j])continue; dis[j]=min(dis[j],d[now][j]); } } printf("%d\n",ans); } }