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.

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的容器进一步省内存。

有空继续优化一下。

时间: 2024-12-17 16:28:18

Timus 1119. Metro 动态规划的相关文章

递推DP URAL 1119 Metro

题目传送门 1 /* 2 题意:已知起点(1,1),终点(n,m):从一个点水平或垂直走到相邻的点距离+1,还有k个抄近道的对角线+sqrt (2.0): 3 递推DP:仿照JayYe,处理的很巧妙,学习:) 4 好像还要滚动数组,不会,以后再补 5 */ 6 #include <cstdio> 7 #include <iostream> 8 #include <algorithm> 9 #include <cmath> 10 #include <cs

ural 1119 Metro dp水

点击打开链接 1119. Metro Time limit: 0.5 second Memory limit: 64 MB 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-d

Ural 1119 Metro(DP)

题目地址:Ural 1119 因为还有一个可不可以穿的问题,所以需要再加一维.0代表可穿不可穿,可穿设置成0,不可穿就设置成无穷大.1代表当前这格的最短距离. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h

timus.1353. Milliard Vasya&#39;s Function 动态规划

题目传送门 题目大意: 输入一个1到81(9*9)的数字s,求在1到109有多少个数字的数位和等于s. 解题思路: 我们很容易知道一位数字等于s的个数.要使一个i位的数字数位和等于s,可以通过一个i-1位的数字后面加上0~9(如果s<9就是0~s).于是很容易得出方程 dp[i][j]=dp[i-1][j-0]+dp[i-2][j-1]+……+dp[i-9][j-9];其中i表示i位的数字,j表示s.dp[i][j]表示一个i位数字数位和为j的方案数. #include <iostream&g

UVA 1025 - A Spy in the Metro (DAG的动态规划)

第一遍,刘汝佳提示+题解:回头再看!!! POINT: dp[time][sta]; 在time时刻在车站sta还需要最少等待多长时间: 终点的状态很确定必然是的 dp[T][N] = 0 ---即在T时刻的时候正好达到N站点 我们可以 从终点的状态往起始的状态转化, 一步步走就可以了. has_train[t][i][0]; t时刻在i车站是否有往右开的火车 has_train[t][i][1]; t时刻在i车站是否有往左开的火车 #include <iostream>#include &l

UVa 1025 A Spy in the Metro(动态规划)

传送门 Description Secret agent Maria was sent to Algorithms City to carry out an especially dangerous mission. After several thrilling events we find her in the first station of Algorithms City Metro, examining the time table. The Algorithms City Metro

timus 1210 Kind Spirits(最短路)(动态规划)

Kind Spirits Time limit: 1.0 secondMemory limit: 64 MB Ivanushka the Fool lives at the planet of 0-level. It's very unpleasant to live there. An awful climate, 80 hours working week, ugly girls… He, as well as every inhabitant of his planet, dreams t

UVa 1025 (动态规划) A Spy in the Metro

题意: 有线性的n个车站,从左到右编号分别为1~n.有M1辆车从第一站开始向右开,有M2辆车从第二站开始向左开.在0时刻主人公从第1站出发,要在T时刻回见车站n 的一个间谍(忽略主人公的换乘时间).输出最少的等待时间,如果无解输出impossible. 分析: d(i, j)表示第i时刻在第j个车站,最少还需要的等待时间.边界是:d(T, n) = 0, d(T, i) = +∞ 预处理: has_train[t][i][0]数组是用来记录t时刻第i个车站是否有向右开的车,类似has_train

UVa 1025 A Spy in the Metro (DP动态规划)

题意:一个间谍要从第一个车站到第n个车站去会见另一个,在是期间有n个车站,有来回的车站,让你在时间T内时到达n,并且等车时间最短, 也就是尽量多坐车,最后输出最少等待时间. 析:这个挺复杂,首先时间是一个顺序,设d(i,j)表示时刻 i 在第 j 个车站,最少还要等待多长时间,那么边界是d(T, n) = 0. 并且有三种决策: 决策一:等着 d[i][j] = d[i + 1][j] + 1; 为什么从i + 1 过来呢? 你想一下,DP表示等待的时间,那么是不是应该倒着来呢? 决策二:有往右