标签:
#include<iostream> #include<cstdio> #include<cmath> #include<algorithm> #include<cstring> #include<string> #include<cstdlib> #include<vector> using namespace std; typedef long long ll; const int INF=10e7; const int MAXN=110; int cost[MAXN][MAXN]; int lowcost[MAXN]; bool vis[MAXN]; int Prim(int n) { int ans=0; int minn,k; memset(vis,0,sizeof(vis)); vis[1]=1; for(int i=1;i<=n;i++) lowcost[i]=cost[1][i]; for(int i=1;i<=n-1;i++) { minn=INF;k=-1; for(int j=1;j<=n;j++) if(!vis[j]&&minn>lowcost[j]) { minn=lowcost[j]; k=j; } if(k==-1) break; ans+=minn;vis[k]=1; for(int j=1;j<=n;j++) if(!vis[j]&&lowcost[j]>cost[k][j]) lowcost[j]=cost[k][j]; } return ans; } int main() { int n; while(~scanf("%d",&n)) { for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) scanf("%d",&cost[i][j]); printf("%d\n",Prim(n)); } return 0; }
标签:
原文地址:http://www.cnblogs.com/atmacmer/p/5196889.html