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-duty, many of the staff — Nikifor among them — walk from their homes to Metro stations on foot.



Problem illustration

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 (N, M). 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

3 2

3

1 1

3 2

1 2

output

383

Problem Author:

Leonid Volkov

Problem Source:

USU Open Collegiate Programming Contest October’2001 Junior Session

对于图中的某一点ij,则Dp[i][j]表示从(0,0)到(i,j)的最短距离。所以对于(i,j)可以到达他的点为(i-1,j)(i,j-1)和(i-1,j-1)(如果可以),所以可以从下到上,从左到右推过去。

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <iostream>
#include <algorithm> using namespace std; const int INF = 0x3f3f3f3f; const int Max = 1010; const double len = 100*sqrt(2); double Dp[Max][Max]; bool Map[Max][Max]; int n,m; void Init()
{
for(int i=0;i<=n;i++)
{
for(int j=0;j<=m;j++)
{
Map[i][j]=false; Dp[i][j] = INF;
}
}
} bool Judge(int x,int y)
{
if(x<=n&&y<=m)
{
return true;
}
return false;
} int main()
{
int num; int u,v; while(~scanf("%d %d",&m,&n))
{
Init(); scanf("%d",&num); while(num--)
{
scanf("%d %d",&u,&v); Map[v-1][u-1] = true; }
Dp[0][0]=0;
for(int i=0;i<=n;i++)
{
for(int j=0;j<=m;j++)
{
if(Judge(i+1,j))
{
Dp[i+1][j]= min(Dp[i+1][j],Dp[i][j]+100);
}
if(Judge(i,j+1))
{
Dp[i][j+1] = min(Dp[i][j+1],Dp[i][j]+100);
}
if(Map[i][j]&&Judge(i+1,j+1))
{
Dp[i+1][j+1]=min(Dp[i+1][j+1],Dp[i][j]+len);
}
}
} printf("%.0f\n",Dp[n][m]);
}
return 0;
}

Metro-Ural119递推的更多相关文章

  1. 递推DP URAL 1119 Metro

    题目传送门 /* 题意:已知起点(1,1),终点(n,m):从一个点水平或垂直走到相邻的点距离+1,还有k个抄近道的对角线+sqrt (2.0): 递推DP:仿照JayYe,处理的很巧妙,学习:) 好 ...

  2. 【BZOJ-2476】战场的数目 矩阵乘法 + 递推

    2476: 战场的数目 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 58  Solved: 38[Submit][Status][Discuss] D ...

  3. 从一道NOI练习题说递推和递归

    一.递推: 所谓递推,简单理解就是推导数列的通项公式.先举一个简单的例子(另一个NOI练习题,但不是这次要解的问题): 楼梯有n(100 > n > 0)阶台阶,上楼时可以一步上1阶,也可 ...

  4. Flags-Ural1225简单递推

    Time limit: 1.0 second Memory limit: 64 MB On the Day of the Flag of Russia a shop-owner decided to ...

  5. 利用Cayley-Hamilton theorem 优化矩阵线性递推

    平时有关线性递推的题,很多都可以利用矩阵乘法来解决. 时间复杂度一般是O(K3logn)因此对矩阵的规模限制比较大. 下面介绍一种利用利用Cayley-Hamilton theorem加速矩阵乘法的方 ...

  6. 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】

    还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...

  7. 简单递推 HDU-2108

    要成为一个ACMer,就是要不断学习,不断刷题...最近写了一些递推,发现递推规律还是挺明显的,最简单的斐波那契函数(爬楼梯问题),这个大家应该都会,看一点稍微进阶了一点的,不是简单的v[i] = v ...

  8. [ACM_动态规划] 数字三角形(数塔)_递推_记忆化搜索

    1.直接用递归函数计算状态转移方程,效率十分低下,可以考虑用递推方法,其实就是“正着推导,逆着计算” #include<iostream> #include<algorithm> ...

  9. 矩阵乘法&矩阵快速幂&矩阵快速幂解决线性递推式

    矩阵乘法,顾名思义矩阵与矩阵相乘, 两矩阵可相乘的前提:第一个矩阵的行与第二个矩阵的列相等 相乘原则: a b     *     A B   =   a*A+b*C  a*c+b*D c d     ...

  10. openjudge1768 最大子矩阵[二维前缀和or递推|DP]

    总时间限制:  1000ms 内存限制:  65536kB 描述 已知矩阵的大小定义为矩阵中所有元素的和.给定一个矩阵,你的任务是找到最大的非空(大小至少是1 * 1)子矩阵. 比如,如下4 * 4的 ...

随机推荐

  1. 带你玩转JavaWeb开发之六-mysql基本语法详解及实例(4)

    按照分类的名称统计每个分类商品所花的总钱数[排序查询] SQL中对查询的列进行排序,使用关键字order by.默认情况下是升序的排序(从小到大的排序顺序关键字 asc).使用降序排序需要使用关键字d ...

  2. Method Swizzling

    学习博客: http://www.cocoachina.com/ios/20160121/15076.html (这个作者太牛了,写了我一直想知道的类簇的swizz方法) 一. 一般的swizz 先给 ...

  3. ipxe引导远程的windows

    使用ipxe解决本地引导远程系统 本地安装的centos7,然后修改grub.cfg来使用ipxe技术引导远程windows,实现双系统 os-->centos7 修改grub.cfg 在文件最 ...

  4. 点击链接跳转到固定div位置处(类似锚点链接)

    $('.joinbtn').click(function(){ var a = $("#contact").offset().top;$("html,body" ...

  5. php字符截取

    mb_substr($str,0,2) 0 索引的位置  2 长度 mb_substr($str,0,-1) 最后一位如果是负数 表示从前面0开始到后面-1 0,-1都是索引

  6. setTimeout调用带参数的函数的方法

    function test(s){    alert(s);}window.setTimeout(function(){test('str');},1000);这样就可以了...为什么是这样呢.因为s ...

  7. python 3.5 关于sys问题总结

    想把自己写的模组直接调用的时候,使用这个sys.path.append(): 但是总是报错(图1): 解决方案,去掉前面的from public; 解决思路: 1.sys.path.append(&q ...

  8. shortcuts on Windows and MacOS

    我现在使用Window 10与MacOS,发现各千秋,也发现Window向MacOS学习并借鉴了一些东西. MacOS有一点非常好的地方是,它可以不怎么使用鼠标,而通过TouchPad便可完成.体验起 ...

  9. Android四大核心组件之ContentProvider

    实验内容 学习ContextProvider用法 编码实现简单ContextProvider功能 实验要求 通过简单代码了解ContextProvider功能和用法 实验步骤 ContextProvi ...

  10. 通过Chrome浏览器检测和优化页面

    1.访问(http://www.cnblogs.com/viaiu/) 2.点击F12 前两步就在扯淡 3.点击Audits标签,进入测试界面 4.点击按钮开始检测 5.如下图可以进行页面加载资源的详 ...