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 |
前一题还是用prim做的AC了 ,这题就是简单的prim,测了多组数据了,就是过不了,帮我看看,多谢!#include <iostream> #include <stdio.h> using namespace std; double edge[101][101],key[101]; int findmin(double *a, int n) { int i,j; double min=10000000; for(i=1;i<=n;i++) { if((a[i]!=0)&&(a[i]<min)) { min=a[i]; j=i; } } return j; } void main() { int i,j,k,n; double sum; while(cin>>n) { sum=0; for(i=1;i<=n;i++) for(j=1;j<=n;j++) { cin>>edge[i][j]; } for(i=1;i<=n;i++) { key[i]=edge[1][i]; } key[1]=0; for(i=2;i<=n;i++) { j=findmin(key,n); sum+=key[j]; key[j]=0; for(k=2;k<=n;k++) { if(edge[j][k]<key[k]) key[k]=edge[j][k]; } } cout<<sum<<endl; } } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator