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

Re:哪里说了要保留两位小数?

Posted by zoujing1978 at 2016-10-13 22:32:12 on Problem 2502 and last updated at 2016-10-13 22:33:02
In Reply To:哪里说了要保留两位小数? Posted by:zoujing1978 at 2016-10-13 22:31:45
> 这道题我已经看出是个求最短路径的Dijkstra算法的题目,网上一搜,果然是用这个算法,然后我就放心按照这个思路去做,Sample通过了,但是提交WA了好几次,郁闷之下,再看网上别人AC的代码,发现程序框架和算法都是一样的,只是最后输出的时候别人保留了两位小数,而我没有直接输出的。后来我也该为输出保留2位小数,就AC了。但是再一看题目,不知道题目哪里说了要保留两位小数啊,好像没说啊!我英语都忘光了,谁能告诉我题目哪里说了要保留两位小数?

#include <fstream>
#include <cstdio>
#include <iostream>
#include <vector>
#include <cmath>
#include <iomanip>
using namespace std;

//const int DBL_MAX = 100000000.0;

struct Point
{
	double x;
	double y;
	double Dist(const Point& p) const;
};
double Point::Dist(const Point& p) const
{
	return sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y));
}

class Graph
{
protected:
	double **arcs; //邻接矩阵,arcs[i][j]为顶点i到顶点j的距离,非负
	int vexnum; // 图的当前顶点数
	int arcnum; //图的边数
	double **D; //D[i][j]表示求出来的从顶点i到顶点j的最短路径
	void Dijkstra(int v0); //求从v0到其余各顶点的最短路径
public:
	Graph(int vexnum, double map[][205]); //构造函数
	~Graph(); //析构函数
	double GetDist(int start, int end);
};

Graph::Graph(int vexnum, double map[][205])
{
	this->vexnum = vexnum;
	this->arcs = new double*[vexnum];
	this->D = new double*[vexnum];
	for (int i = 0; i < vexnum; i++)
	{
		(this->arcs)[i] = new double[vexnum];
		memcpy((this->arcs)[i], map[i], sizeof(double) * vexnum);
		(this->D)[i] = new double[vexnum];
	}
}

Graph::~Graph()
{
	for (int i = 0; i < vexnum; i++)
	{
		delete[] arcs[i];
		delete[] D[i];
	}
	delete[] arcs;
	delete[] D;
}

void Graph::Dijkstra(int v0)
{
	bool* final = new bool[vexnum]; //final[i]表示从顶点v0到顶点i的最短距离是否已经求出
	for (int v = 0; v < vexnum; ++v)
	{
		final[v] = false;
		D[v0][v] = arcs[v0][v];
	}
	D[v0][v0] = 0;
	final[v0] = true;
	for (int i = 0; i < vexnum; ++i) //循环vexnum次
	{
		//在尚未求出最短路径的顶点集合中找到距离v0最近的点v
		double min = DBL_MAX;
		int v = v0; // DBL_MAX为机器中表示无穷大的整数
		for (int w = 0; w < vexnum; ++w)
		{
			if (!final[w] && D[v0][w] < min)
			{
				v = w;
				min = D[v0][w];
			}
		}
		final[v] = true;
		for (int w = 0; w < vexnum; ++w) //更新当前最短路径和距离
		{
			if (!final[w] && min + arcs[v][w] < D[v0][w])
			{
				D[v0][w] = min + arcs[v][w];
			}
		}
	}
	delete[] final;
}

double Graph::GetDist(int start, int end)
{
	Dijkstra(start);
	return D[start][end];
}

int main()
{
	//ifstream cin("Text.txt");
	const int MAXVEXNUM = 205;
	double map[MAXVEXNUM][MAXVEXNUM];
	for (int i = 0; i < MAXVEXNUM; i++)
	{
		for (int j = 0; j < MAXVEXNUM; j++)
		{
			if (i == j)
			{
				map[i][j] = 0;
			}
			else
			{
				map[i][j] = DBL_MAX;
			}
		}
	}
	Point p[205];
	Point start, end;
	int v0 = 0, v1 = 1;
	while (cin >> p[0].x >> p[0].y >> p[1].x >> p[1].y)
	{
		int n = 2;
		bool flag = false;
		while (cin >> p[n].x >> p[n].y)
		{
			if (p[n].x == -1 && p[n].y == -1)
			{
				flag = false;
				continue;
			}
			if (flag)
			{
				double time = p[n].Dist(p[n - 1]) / 40000.0;          //存储的是时间,地铁速度是40km/h
				map[n][n - 1] = map[n - 1][n] = time;
			}
			n++;
			if (!flag)
			{
				flag = true;
			}
		}
		for (int i = 0; i < n; i++)
		{
			for (int j = i + 1; j < n; j++)
			{
				if (map[i][j] == DBL_MAX)
				{
					map[i][j] = map[j][i] = p[i].Dist(p[j]) / 10000.0;    //步行的速度是10km/h
					double tmp = map[i][j];
					int tt = 0;
				}
			}
		}
		Graph g(n, map);
		cout << setprecision(2) << g.GetDist(0, 1) * 60 << "\n";
	}
	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