码迷,mamicode.com
首页 > 其他好文 > 详细

uva 1347 poj 2267 Tour 最短双调回路

时间:2015-04-19 01:14:02      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:uva 1347   poj   2267   tour   最短双调回路   

// uva1347 Tour 最短双调路线
// 这道题是看着紫书上面写着的
// dp[i][j]表示1至max(i,j)都已经走过时并且第一个人在i
// 第二个人在j点时所要走的最短的距离,则dp[i][j] = dp[j][i]
// 状态转移方程为
// dp[i+1][j] = max(dp[i][j]+dist[i][i+1],dp[i+1][i]+dist[j][i+1])
// 其实就是考虑第i+1号点是与i相连还是与j相连,(本来dp[i+1][i]是dp[i][i+1]
// 因为规定第一维比第二维的大,所以用dp[i+1][i]);
// 边界问题就是dp[n-1][j] = dist[n-1][n]+dist[j][n];
// 最后答案为dist[1][2]+d[2][1];
// 考虑到当某个人走到2时另一个人一定会在1而这个状态就是dp[2][1];
// 这时候直接走到终点就可以了
//
// 第二种方法就是搜了一下题解,看看有没有多种解法,拓展一下思路
// dp[i][j]表示第一个人在i时第二个人在j时所走的最短的距离
// 状态转移方程还是根据i+1号节点是和i相连还是和j相连分两种情况
// 第一种 i+1与i相连
// dp[i+1][j] = dp[i][j]+dist[i][i+1];
// 第二种 i+1与j相连
// dp[i+1][i] = min(dp[i][j]+dp[j][i+1]);
//#include <algorithm>
//#include <bitset>
//#include <cassert>
//#include <cctype>
//#include <cfloat>
//#include <climits>
//#include <cmath>
//#include <complex>
//#include <cstdio>
//#include <cstdlib>
//#include <cstring>
//#include <ctime>
//#include <deque>
//#include <functional>
//#include <iostream>
//#include <list>
//#include <map>
//#include <numeric>
//#include <queue>
//#include <set>
//#include <stack>
//#include <vector>
//#define ceil(a,b) (((a)+(b)-1)/(b))
//#define endl '\n'
//#define gcd __gcd
//#define highBit(x) (1ULL<<(63-__builtin_clzll(x)))
//#define popCount __builtin_popcountll
//typedef long long ll;
//using namespace std;
//const int MOD = 1000000007;
//const long double PI = acos(-1.L);
//
//template<class T> inline T lcm(const T& a, const T& b) { return a/gcd(a, b)*b; }
//template<class T> inline T lowBit(const T& x) { return x&-x; }
//template<class T> inline T maximize(T& a, const T& b) { return a=a<b?b:a; }
//template<class T> inline T minimize(T& a, const T& b) { return a=a<b?a:b; }
//
//const int maxn = 1008;
//double d[maxn][maxn];
//double x[maxn],y[maxn],dist[maxn][maxn];
//int n;
//
//double getdist(int i,int j){
//	double dis = (x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]);
//	return sqrt(dis);
//}
//
//void init(){
//	for (int i=1;i<=n;i++)
//		scanf("%lf%lf",&x[i],&y[i]);
////	memset(d,0x4f,sizeof(d));
//	for (int i=1;i<=n;i++)
//		for (int j=1;j<=n;j++)
//			dist[i][j] = getdist(i,j);
//}
//
//void print(){
//	for (int i=1;i<=n;i++){
//		for (int j=1;j<=n;j++)
//			printf("%.2lf ",d[i][j]);
//		puts("");
//	}
//}
//
//void solve(){
//	for (int i=n-1;i>=1;i--)
//		for (int j=1;j<i;j++){
//			if (i==n-1)	d[i][j] = dist[i][n]+dist[j][n];
//			else d[i][j] = min(dist[i][i+1]+d[i+1][j],dist[j][i+1]+d[i+1][i]);
//		}
//	//print();
//	printf("%.2lf\n",dist[1][2]+d[2][1]);
//}
//
//
//
//int main() {
//	freopen("G:\\Code\\1.txt","r",stdin);
//	while(scanf("%d",&n)!=EOF){
//		init();
//		solve();
//	}
//	return 0;
//}

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define ceil(a,b) (((a)+(b)-1)/(b))
#define endl '\n'
#define gcd __gcd
#define highBit(x) (1ULL<<(63-__builtin_clzll(x)))
#define popCount __builtin_popcountll
typedef long long ll;
using namespace std;
const int MOD = 1000000007;
const long double PI = acos(-1.L);

template<class T> inline T lcm(const T& a, const T& b) { return a/gcd(a, b)*b; }
template<class T> inline T lowBit(const T& x) { return x&-x; }
template<class T> inline T maximize(T& a, const T& b) { return a=a<b?b:a; }
template<class T> inline T minimize(T& a, const T& b) { return a=a<b?a:b; }

const int maxn = 1008;
double x[maxn],y[maxn],dist[maxn][maxn],d[maxn][maxn];
int n;

double getdist(int i,int j){
	double dis = (x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]);
	return sqrt(dis);
}

void print(){
	for (int i=1;i<=n;i++){
		for (int j=1;j<=n;j++)
			printf("%.2lf ",d[i][j]);
		puts("");
	}
}

void init(){
	for (int i=1;i<=n;i++)
		scanf("%lf%lf",&x[i],&y[i]);
//	memset(d,0x4f,sizeof(d));
	//memset(d,0,sizeof(d));
	for (int i=1;i<=n;i++)
		for (int j=1;j<=n;j++){
			dist[i][j] = getdist(i,j);
		}
	d[1][1]=0;
	for (int i=1;i<=n;i++)
		d[i][1] = dist[i][1];
}


void solve(){
	for (int i=2;i<=n-1;i++){
		d[i+1][i]=1e15;
		for (int j=1;j<i;j++){
			d[i+1][j] = d[i][j]+dist[i][i+1];
			d[i+1][i] = min(d[i+1][i],d[i][j]+dist[j][i+1]);
		}
	}
	//print();
	printf("%.2lf\n",d[n][n-1]+dist[n][n-1]);
}

int main() {
	//freopen("G:\\Code\\1.txt","r",stdin);
	while(scanf("%d",&n)!=EOF){
		init();
		if (n==1){
			puts("0.00");
			continue;
		}
		solve();
	}
	return 0;
}

uva 1347 poj 2267 Tour 最短双调回路

标签:uva 1347   poj   2267   tour   最短双调回路   

原文地址:http://blog.csdn.net/timelimite/article/details/45120343

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!