poj 2677 Tour

Tour查看提交统计提问总时间限制: 1000ms内存限制: 65536kB
描述
John Doe, a skilled pilot, enjoys traveling. While on vacation, he rents a small plane and starts visiting beautiful places.
To save money, John must determine the shortest closed tour that connects his destinations. Each destination is represented
by a point in the plane pi = < xi,yi >. John uses the following strategy: he starts from the leftmost point, then he goes
strictly left to right to the rightmost point, and then he goes strictly right back to the starting point. It is known that
the points have distinct x-coordinates.

Write a program that, given a set of n points in the plane, computes the shortest closed tour that connects the points according
 to John's strategy.

输入
The program input is from a text file. Each data set in the file stands for a particular set of points. For each set of points
the data set contains the number of points, and the point coordinates in ascending order of the x coordinate. White spaces can
occur freely in input. The input data are correct.

The number of points of each data set is at most 50, and each coordinate does not exceed 20000 by an absolute value.
输出
For each set of data, your program should print the result to the standard output from the beginning of a line. The tour length,
 a floating-point number with two fractional digits, represents the result. An input/output sample is in the table below. Here
 there are two data sets. The first one contains 3 points specified by their x and y coordinates. The second point, for example,
  has the x coordinate 2, and the y coordinate 3. The result for each data set is the tour length, (6.47 for the first data set
                                                                                                     in the given example).
样例输入
3
1 1
2 3
3 1
4
1 1
2 3
3 1
4 2样例输出
6.47
7.89

我一开始没有看清楚题目,后来才完全明白题目的意思。
也即,以最左端的点为起点与终点,过程中要到达每一个点,如果从排名序号上来看,则从1到最大值为单调递增序列,从最大值到1为单调递减序列。
原来想要用枚举next_permutation,但是发现并没有这么简单,而且数据一多容易超时。所以改用stack,下面是代码:
//2015.06.01 儿童节的礼物
//有些命名习惯不太好,凑合着看吧
#include
#include
#include
#include
#include
#include
#include
using namespace std;


class point{
public:
	double x,y;
	point() { x=y=0.0; }
	point(double _x,double _y):x(_x),y(_y){}
	void set(double _x,double _y) { x=_x, y=_y;}
	void clear(){x=y=0.0;}
}po[52];
int used[52]={0};
double dp[52][52]={0.0},minlen=0.0,tmplen=0.0;
stack _s0,_s1;


int main(){
	int ncase;
	double x,y;
	while(cin >> ncase){
		minlen = 0.0;
		while(!_s0.empty()){_s0.pop();}
		while(!_s1.empty()){_s1.pop();}
		
		for(int i = 1; i <= ncase; ++i){
			cin >> x >> y;
			po[i].set(x,y);
		}
		for(int i = 1; i < ncase; ++i)
			for(int j = i+1; j <= ncase; ++j)
				dp[j][i] = dp[i][j] = sqrt((po[i].x-po[j].x)*(po[i].x-po[j].x)+(po[i].y-po[j].y)*(po[i].y-po[j].y));


		minlen = dp[1][ncase];
		for(int i = 2; i <= ncase; ++i)
			minlen += dp[i-1][i];


		int time = 1 << (ncase-1);
		memset(used,0,sizeof(used));
		for(int t = 1; t < time; ++t){
			tmplen = 0.0;
			int i;
			++used[1];
			if(used[1] > 1){
				used[1] = 0;
				++used[2];
			}
			_s0.push(1);_s1.push(1);
			for(i = 2; i < ncase; ++i){
				if(used[i] > 1){
					used[i] = 0;
					++used[i+1];
				}
				if(used[i]==0){_s0.push(i);}
				else{_s1.push(i);}
			}
			_s0.push(ncase);_s1.push(ncase);
			int i1=1,i2=2;
			bool flag = false;
			i1=_s0.top();_s0.pop();
			while(!_s0.empty()){
				i2=_s0.top();_s0.pop();
				tmplen += dp[i1][i2];
				if(tmplen > minlen){
					flag = true;
					break;
				}
				i1=i2;
			}
			i1=_s1.top();_s1.pop();
			while(!_s1.empty()){
				i2=_s1.top();_s1.pop();
				tmplen += dp[i1][i2];
				if(tmplen > minlen){
					flag = true;
					break;
				}
				i1=i2;
			}
			if(flag)
				continue;
			minlen = tmplen;
		}
		cout << setiosflags(ios::fixed) << setprecision(2) << minlen << endl;
	}
        return 0;
}

你可能感兴趣的:(poj)