标签:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 45047 | Accepted: 18477 |
Description
Input
Output
Sample Input
4 0 4 9 21 4 0 8 17 9 8 0 16 21 17 16 0
Sample Output
28
Source
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<string> #include<iostream> #include<cstring> #include<cmath> #include<stack> #include<queue> #include<vector> #include<map> #include<stdlib.h> #include<algorithm> #define LL __int64 using namespace std; const int MAXN=100+5; const int INF=0x3f3f3f3f; int w[MAXN][MAXN]; int d[MAXN]; int vis[MAXN]; int n,ans; void prim() { ans=0; memset(vis,0,sizeof(vis)); for(int i=2;i<=n;i++) d[i]=w[1][i]; vis[1]=1; for(int i=1;i<n;i++) { int minn=INF,tmp; for(int j=1;j<=n;j++) { if(!vis[j] && d[j]<minn) { minn=d[j]; tmp=j; } } //if(minn==INF) break; vis[tmp]=1; ans+=minn; for(int j=1;j<=n;j++) if(w[tmp][j]<d[j] && !vis[j]) d[j]=w[tmp][j]; } } int main() { while(scanf("%d",&n) && n) { for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) scanf("%d",&w[i][j]); prim(); printf("%d\n",ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/clliff/p/4706179.html