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 |
Re:给大家一个正确的代码,供不过者参考In Reply To:给大家一个正确的代码,供不过者参考 Posted by:zpdlut at 2009-02-19 16:56:02 > #include<iostream> > using namespace std; > void prim(int input[26][26],int n) > { > int sum=0,min,i,k; > int s[26]={0},lowcost[26]; > s[0]=1; > for(i=1;i<n;i++) > lowcost[i]=input[0][i]; > for(i=0;i<n-1;i++) > { > min=101; > int j=0; > for(k=1;k<n;k++) > { > if(!s[k]&&lowcost[k]<min) > { > min=lowcost[k]; > j=k; > } > } > sum+=min; > s[j]=1; > for(k=1;k<n;k++) > { > if(!s[k]&&input[j][k]<lowcost[k]) > lowcost[k]=input[j][k]; > } > } > cout<<sum<<endl; > } > int main() > { > int input[26][26],start,end; > int num,n; > char ch1; > while(cin>>num&&num!=0) > { > memset(input,101,sizeof(input)); > for(int i=0;i<num-1;i++) > { > cin>>ch1>>n; > start=ch1-'A'; > for(int j=0;j<n;j++) > { > cin>>ch1; > end=ch1-'A'; > cin>>input[start][end]; > input[end][start]=input[start][end]; > } > } > prim(input,num); > } > > return 1; > } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator