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 |
一个写的代码,就是定义改了个名字,一个AC,一个WA,我真是看不懂了这个是AC了得代码:#include<iostream> #include<cstdio> #include<cstring> #include<math.h> using namespace std; int g[760][760]; int n,m; int prim() { int i,j,k,maxcost =999999999,min; int dist[760],pre[760]; bool vis[760]; memset(vis,true,sizeof(vis)); for(i=1;i<=n;i++) { dist[i]=maxcost; pre[i]=1; } vis[1]=false;dist[1]=0; for(i=1;i<=n;i++) { min=maxcost;k=1; for(j=1;j<=n;j++) if(vis[j]&&dist[j]<min) { k=j; min=dist[j]; } vis[k]=false; for(j=1;j<=n;j++) if(dist[j]>g[k][j]&&vis[j]) { dist[j]=g[k][j]; pre[j]=k;} } min=0; for(i=1;i<=n;i++) min+=dist[i]; return min; } int main() { int i,j; while(cin>>n) { memset(g,0,sizeof(g)); for(i=1;i<=n;i++) for(j=1;j<=n;j++) cin>>g[i][j]; cin>>m; while(m--) { cin>>i>>j; g[i][j]=g[j][i]=0; } cout<<prim()<<endl; } return 0; } 这个是将上面的代码中得某些定义改了下名字:g改成map,vis改成mark。#include<iostream> #include<cmath> #include<algorithm> using namespace std; int map[800][800]; int n,m; int prim() { int i,k,min; int mark[800]; int pre[800]; int dist[800]; memset(mark,0,sizeof(mark)); for(i=1;i<=n;i++) dist[i]=999999999,pre[i]=1; mark[1]=1; dist[1]=0; m=n; while(m--) { min=999999999,k=1; for(i=1;i<=n;i++) if(!mark[i]&&dist[i]<min) min=dist[i],k=i; mark[i]=1; for(i=1;i<=n;i++) if(!mark[i]&&map[k][i]<dist[i]) dist[i]=map[k][i],pre[i]=k; } min=0; for(i=1;i<=n;i++) min+=dist[i]; return min; } int main() { int i,j; while(cin>>n) { memset(map,0,sizeof(map)); for(i=1;i<=n;i++) for(j=1;j<=n;j++) cin>>map[i][j]; cin>>m; while(m--) { cin>>i>>j; map[i][j]=map[j][i]=0; } cout<<prim()<<endl; } return 0; } 求各位大神给个说法 Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator