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

What's Wrong?!

Posted by hasan83 at 2011-12-25 08:07:19 on Problem 3625
#include<iostream>
#include<stdio.h>
#include<cmath>
using namespace std;

struct point
{
    __int64 x,y;
}p[1010];

const int ns = 1010;
int n, m, inTree[ns];
double weight[ns][ns], d[ns];

void updateDistances(int target) {
	for (int i = 0; i < n; ++i)
	   if(i!=target && d[i] > weight[target][i])
			d[i] = weight[target][i];
}

double dist(__int64 x1,__int64 y1,__int64 x2, __int64 y2)
{
    return sqrt(double((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)));
}

int main()
{	
	double total = 0;
	int i, j, x, y, min, treeSize=0;
	
    cin >> n >> m;
	for (i = 0; i < n; ++i)
	   cin >> p[i].x >> p[i].y;
    
	for (i = 0; i < n; ++i)
	{
        d[i] = 10000000.0 * 10000000.0;
		inTree[i] = 0;
    }
	
    for (i = 0; i < n; ++i)
        for (j = 0; j < n; ++j)
            weight[i][j] = weight[j][i] = dist( p[i].x, p[i].y, p[j].x, p[j].y );
                
	for (i = 0; i < m; ++i)
	{
	   cin >> x >> y;
	   x-=1,y-=1;
       
       weight[x][y] = weight[x][y] = 0.0;
    }
    
    inTree[0]=1;
    updateDistances(0);
	for ( treeSize=1; treeSize < n; ++treeSize) {
	
		min = -1;
		for (i = 0; i < n; ++i)
			if (!inTree[i])
				if (min == -1 || d[min] > d[i])
					min = i;

		total += d[min];
        inTree[min] = 1;
		updateDistances(min);
	}
    
	printf("%0.2lf\n",total);
//    system("pause");
    return 0;
}

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