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 |
用c++ac的第一道题目!给力!有多次输入!给位!#include <iostream> #include <string.h> using namespace std; #define MAXSIZE 110 #define MAXVALUE 999999 class poj1258 { private: int dist[MAXSIZE]; int distance[MAXSIZE][MAXSIZE]; bool flag[MAXSIZE]; int n; public: void input(int k); void result(); }; void poj1258::input(int k) { n=k; for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) cin>>distance[i][j]; } } void poj1258::result() { int sum,i,j,k,t; memset(dist,0,sizeof(dist)); memset(flag,false,sizeof(flag)); for(k=1;k<=n;k++) dist[k]=distance[1][k]; flag[1]=true; sum=0; for(i=1;i<n;i++) { t=MAXVALUE; for(j=1;j<=n;j++) if(!flag[j]&&dist[j]<t) { t=dist[j]; k=j; } sum+=t; flag[k]=true; for(j=1;j<=n;j++) if(!flag[j]&&distance[k][j]<dist[j]) { dist[j]=distance[k][j]; } } cout<<sum<<endl; } int main() { int m; while(cin>>m) { poj1258 t; t.input(m); t.result(); } return 1; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator