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

Timus 1119. Metro 动态规划

时间:2014-04-30 22:21:38      阅读:369      评论:0      收藏:0      [点我收藏+]

标签:timus   1119. metro   动态规划   

Many of SKB Kontur programmers like to get to work by Metro because the main office is situated quite close the station Uralmash. So, since a sedentary life requires active exercises off-duty, many of the staff — Nikifor among them — walk from their homes to Metro stations on foot.
mamicode.com,码迷
Nikifor lives in a part of our city where streets form a grid of residential quarters. All the quarters are squares with side 100 meters. A Metro entrance is situated at one of the crossroads. Nikifor starts his way from another crossroad which is south and west of the Metro entrance. Naturally, Nikifor, starting from his home, walks along the streets leading either to the north or to the east. On his way he may cross some quarters diagonally from their south-western corners to the north-eastern ones. Thus, some of the routes are shorter than others. Nikifor wonders, how long is the shortest route.
You are to write a program that will calculate the length of the shortest route from the south-western corner of the grid to the north-eastern one.

Input

There are two integers in the first line: N and M (0 < N,M ≤ 1000) — west-east and south-north sizes of the grid. Nikifor starts his way from a crossroad which is situated south-west of the quarter with coordinates (1, 1). A Metro station is situated north-east of the quarter with coordinates (NM). The second input line contains a number K (0 ≤ K ≤ 100) which is a number of quarters that can be crossed diagonally. Then K lines with pairs of numbers separated with a space follow — these are the coordinates of those quarters.

Output

Your program is to output a length of the shortest route from Nikifor‘s home to the Metro station in meters, rounded to the integer amount of meters.

Sample

input output
3 2
3
1 1
3 2
1 2
383


很好的一道动态规划法题目。

注意:

1 行列别搞错了,要很细心一点一点对起来

2 要以边线思考,不要以方块来计算, N*M个方块就成了(N+1)*(M+1)条交叉线了,最下面和最左边的线就方便初始化了

3 注意C++的四舍五入的方法

动态规划的状态转移方程:

if (A[y][x]) B[x] = t + 1.414213562;
else 	B[x] = min(B[x-1], B[x]) + 1 ;
A[y][x]表示是否有对角线,有对角线必定是走对角线的。


void Metro1119_Vec()
{
	int N, M, K, x, y;
	cin>>N>>M;//小心什么是行什么是列
	vector<vector<bool> > A(M+1, vector<bool>(N+1));
	cin>>K;
	for (int i = 0; i < K; i++)
	{
		cin>>x>>y;//列和行别搞错了
		A[y][x] = 1;
	}
	vector<double> B(N+1);
	for (int i = 0; i <= N; i++)
	{
		B[i] = (double)i;
	}
	for (y = 1; y <= M; y++)
	{
		double t = B[0];
		B[0] += 1.0;
		for (x = 1; x <= N; x++)
		{
			double a = B[x];
			if (A[y][x]) B[x] = t + 1.414213562;
			else 	B[x] = min(B[x-1], B[x]) + 1 ;
			t = a;
		}
	}
	cout<<(int)(B[N] * 100.0 + 0.5);//修改要全面
}


逛了下网上,发现本题还可以利用LIS的思想去做的。

还可以使用一下bitset的容器进一步省内存。

有空继续优化一下。


Timus 1119. Metro 动态规划

标签:timus   1119. metro   动态规划   

原文地址:http://blog.csdn.net/kenden23/article/details/24716179

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