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 |
这个题目的牛逼之处是三重循环的prime写法也没问题//POJ1258 #include<iostream> #include<cstdlib> #include<cstring> using namespace std; const int MAX = 501; const int COST_MAX = 0x7FFFFFFF; int prime(int G[][MAX], int N) { int sum = 0; int v[MAX]; memset(v,0,MAX); v[0] = 1; int cal = 0; while( cal != N-1) { int min = COST_MAX; int x = 0; int y = 0; for( int i = 0; i != N; ++i) { if(v[i]==0) continue; for(int j = 1; (j != N); ++j) { if(v[j]==1) continue; if( G[i][j] && G[i][j]<min ) { min = G[i][j]; x = i; y = j; } } } if(min != COST_MAX) { sum += min; v[x] = 1; v[y] = 1; ++cal; } } return sum; } int main() { int N; int G[MAX][MAX]; while(cin>>N) { memset( G, 0, MAX*MAX ); for(int i = 0; i != N; ++i) for(int j = 0; j != N; ++j) cin>>G[i][j]; cout<<prime(G,N)<<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