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-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

题意;城市为正方形格子,每个格子的边长为100米。地铁站在其中一个十字路口。Nikanor从家里步行到地铁站。他沿着街道走,也可以穿越某一些格子的对角线,这样会近一些。 求Nikanor从西南角的家到东北角地铁站的最短路径。

思路:利用dp做,有两个递推方程,对于一个点来说,如果可以另一点斜着过了则求dp[i][j-1]+100、dp[i-1][j]+100、dp[i-1][j-1]+sqrt(2)*100中的最小值,否则求dp[i][j-1]+100、dp[i-1][j]+100中的最小值。

 #include<iostream>
#include<cstdio>
#include<cmath> using namespace std;
int s[][]={};
double dp[][]={}; double min(double a,double b,double c=)
{
if(a>b)
return b<c?b:c;
else
return a<c?a:c;
} int main()
{
// freopen("1.txt","r",stdin);
int n,m;
cin>>n>>m;
int k;
cin>>k;
int i,j;
int a,b;
n++;
m++;
for(i=;i<=n;i++)
dp[][i]=;
for(i=;i<=m;i++)
dp[i][]=;
for(i=;i<k;i++)
{
cin>>a>>b;
s[b+][a+]=;
}
for(i=;i<=m;i++)
{
for(j=;j<=n;j++)
{
if(i==&&j==)continue;
if(s[i][j]==)
{//如果改点可以由一点斜着到达
dp[i][j]=min(dp[i][j-]+,dp[i-][j]+,dp[i-][j-]+sqrt(2.0)*);//比较得出dp[i][j-1]+100、dp[i-1][j]+100、dp[i-1][j-1]+sqrt(2)*100中的最小值;
}//注意sqrt()里面是精度数,例如不可以是2,单可以是2.0
else
{//改点不可以由一点斜着到达
dp[i][j]=min(dp[i][j-]+,dp[i-][j]+);//比较求出dp[i][j-1]+100、dp[i-1][j]+100中的最小值
}
}
}
printf("%.0lf\n",dp[m][n]);
return ;
}

ural 1119. Metro(动态规划)的更多相关文章

  1. 递推DP URAL 1119 Metro

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

  2. URAL 1119. Metro(BFS)

    点我看题目 题意  : 这个人在左下角,地铁在右上角,由很多格子组成的地图,每一条边都是一条路,每一条边都是100米.还有的可以走对角线,问你从起点到终点最短是多少. 思路 : 其实我想说一下,,,, ...

  3. ural 1119 Metro

    http://acm.timus.ru/problem.aspx?space=1&num=1119 #include <cstdio> #include <cstring&g ...

  4. URAL 1119. Metro(DP)

    水题. #include <cstring> #include <cstdio> #include <string> #include <iostream&g ...

  5. UVA1025-A Spy in the Metro(动态规划)

    Problem UVA1025-A Spy in the Metro Accept: 713  Submit: 6160Time Limit: 3000 mSec Problem Descriptio ...

  6. URAL DP第一发

    列表: URAL 1225 Flags URAL 1009 K-based Numbers URAL 1119 Metro URAL 1146 Maximum Sum URAL 1203 Scient ...

  7. URAL(DP集)

    这几天扫了一下URAL上面简单的DP 第一题 简单递推 1225. Flags #include <iostream> #include<cstdio> #include< ...

  8. 要back的题目 先立一个flag

    要back的题目 目标是全绿!back一题删一题! acmm7 1003 1004 acmm8 1003 1004 sysu20181013 Stat Origin Title Solved A Gy ...

  9. CJOJ 1976 二叉苹果树 / URAL 1018 Binary Apple Tree(树型动态规划)

    CJOJ 1976 二叉苹果树 / URAL 1018 Binary Apple Tree(树型动态规划) Description 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的 ...

随机推荐

  1. WinForm笔记一:文本框只允许输入数字

    在WinForm的文本框中,有时候只允许数字,而不能输入除数字以外的其他字符,要调用TextBox的KeyPress事件,代码如下: //只允许输入数字 if (e.KeyChar<'0'||e ...

  2. Python学习笔记——进阶篇【第九周】———MYSQL操作

    Mysql 增删改查操作 查看数据库 show databases; 创建数据库并允许中文插入 create database s12day9 charset utf8; 使用数据库 use s12d ...

  3. [SOJ] DAG?

    Description 输入一个有向图,判断该图是否是有向无环图(Directed Acyclic Graph). Input 输入的第一行包含两个整数n和m,n是图的顶点数,m是边数.1<=n ...

  4. 关于mac地址的一点感想

    因为怕mac地址冲突导致环路影响,所以修改了本地设备的mac地址.地址修改为 77:77:77:00:22:11, 结果导致 wlan0 下发不下来. 查看配置选项/etc/config/wirele ...

  5. iOS编程中throttle那些事

    不知道大家对throttle这个单词是否看着眼熟,还是说对这个计算机基础概念有很清晰的了解了.今天就来聊聊和throttle相关的一些技术场景. 定义 我经常有一种感觉,对于英语这门语言的语感,会影响 ...

  6. Windows Azure Storage

    之前都是在博客园看别人的文章,今天开始就开启自己的博客咯,欢迎阅读,共同探讨! 简单点说Widows Azure Storage就是一个大的网盘,可以让用户存储任何想存储的数据,数据一旦存储到“云”中 ...

  7. invalid receiver type

    Because in a case like this: type I int type P *I func (i I) Get() int { return int(i) } func (p P) ...

  8. timeit模块 与 time模块,计时的区别

    time  模块,理解容易 import time time_start = time.time() time_end = time.time() time_use = time_end - time ...

  9. python 基础学习3-函数

    1. 函数参数-默认参数 python函数也可以跟C语言一样,在函数的形参中设定默认值. >>> def test(flag, port = 8080) ... print port ...

  10. js调用函数时传入的参数个数与函数定义时的参数个数不符时的操作

    在js中函数没有重载的概念,如果声明了多个重名的函数,不管函数的形参个数是否一样,只有最有一个有效,其他的函数声明都是无效的.比如说声明了两个函数fn(),第一次声明时没有形参,第二次声明时形参有两个 ...