Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
秒咯,新手上路 ^_^#include<cstdio> #include<cstring> #define INF 100000000 #define maxn 105 int n,sum; int map[maxn][maxn]; int used[maxn]; int dis[maxn]; void Prim() { int i, j, k, min; memset(used, 0, sizeof(used)); used[1] = 1; for(i=1; i<=n; i++) dis[i] = map[1][i]; for(i=1; i<n; i++) { min=INF; for(j=1; j<=n; j++) { if(!used[j] && dis[j]<min) { min=dis[j]; k=j; } } used[k] = 1; sum+=min; for(j=1; j<=n; j++) { if(!used[j] && dis[j]>map[k][j]) { dis[j]=map[k][j]; } } } } int main() { int i, j; while(scanf("%d",&n)!=EOF) { sum = 0; for(i=1; i<=n; i++) { for(j=1; j<=n; j++) { scanf("%d", &map[i][j]); } } Prim(); printf("%d\n",sum); } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator