标签:des blog http strong width os
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 38326 | Accepted: 15424 |
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
代码:
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <algorithm> #define minn 0x3f3f3f3f #define maxx 105 int map[maxx][maxx],low[maxx],visit[maxx]; int n,m; int prim() { int i,j; int pos,result=0,min; memset(visit,0,sizeof(visit)); visit[1]=1,pos=1; for(i=1;i<=n;i++) { if(i!=pos) { low[i]=map[pos][i]; } } for(i=1;i<n;i++) { min=minn; for(j=1;j<=n;j++) { if(visit[j]==0 && min>low[j]) { min=low[j]; pos=j; } } result+=min; visit[pos]=1; for(j=1;j<=n;j++) { if(visit[j]==0 && low[j]>map[pos][j]) { low[j]=map[pos][j]; } } } return result; } int main() { int i,j,k,t; while(scanf("%d",&n)!=EOF) { memset(map,minn,sizeof(map)); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { scanf("%d",&map[i][j]); } } int result=prim(); printf("%d\n",result); } return 0; }
标签:des blog http strong width os
原文地址:http://www.cnblogs.com/ccccnzb/p/3830267.html