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

我也用了DP,但总是WA,各位大牛,小牛帮忙看一下吧

Posted by lianzhouxiaowu at 2009-10-17 03:07:46 on Problem 2137
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <cmath>
#include <FLOAT.H>
using namespace std;
struct dot
{
	int x;
	int y;
};
dot Coordinate[101][41];      //每头牛可取的坐标
int CoorNum[101];             //每头牛的坐标的数量
double Least[101][41];        //Least[i][j]表示第i头牛取第j个坐标时的最小距离
int N;
int Ans();
int main()
{
	int i, j;
	while(scanf("%d", &N) != EOF)
	{
		for(i = 1; i <= N; i++)
		{
			scanf("%d", &CoorNum[i]);
			for(j = 1; j <= CoorNum[i]; j++)
				scanf("%d%d", &Coordinate[i][j].x, &Coordinate[i][j].y);
		}
		printf("%d\n",  Ans());
	}
	return 0;
}

int Ans()
{
	int i, j, k, g;
	for(i = 1; i <= CoorNum[1]; i++)
		Least[1][i] = 0;
	double d, Min, ans = DBL_MAX;
	for(i = 1; i <= CoorNum[1]; i++)   //先固定第一个点
	{
		for(j = 2; j <= N; j++)
		{
			for(k = 1; k <= CoorNum[j]; k++)
			{
				Min = DBL_MAX;
				for(g = 1; g <= CoorNum[j - 1]; g++)
				{
					d = sqrt(pow(Coordinate[j][k].x - Coordinate[j - 1][g].x, 2.0) + pow(Coordinate[j][k].y - Coordinate[j - 1][g].y, 2.0)) + Least[j - 1][g];
  				    if(d < Min)
					    Min = d;
				}
				Least[j][k] = Min;
			}
		}
		//
		for(j = 1; j <= CoorNum[N]; j++)
		{
			d = Least[N][j] + sqrt(pow(Coordinate[1][i].x - Coordinate[N][j].x, 2.0) + pow(Coordinate[1][i].y - Coordinate[N][j].y, 2.0));
			if(d < ans)
				ans = d;
		}
	}
	ans *= 100;
	int a = (int)ans;
	return a;
}

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