Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

用c++ac的第一道题目!给力!有多次输入!给位!

Posted by chenxuan123456789 at 2012-10-26 11:33:57 on Problem 1258
#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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator