Metro-Ural119递推
| 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递推的更多相关文章
- 递推DP URAL 1119 Metro
题目传送门 /* 题意:已知起点(1,1),终点(n,m):从一个点水平或垂直走到相邻的点距离+1,还有k个抄近道的对角线+sqrt (2.0): 递推DP:仿照JayYe,处理的很巧妙,学习:) 好 ...
- 【BZOJ-2476】战场的数目 矩阵乘法 + 递推
2476: 战场的数目 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 58 Solved: 38[Submit][Status][Discuss] D ...
- 从一道NOI练习题说递推和递归
一.递推: 所谓递推,简单理解就是推导数列的通项公式.先举一个简单的例子(另一个NOI练习题,但不是这次要解的问题): 楼梯有n(100 > n > 0)阶台阶,上楼时可以一步上1阶,也可 ...
- Flags-Ural1225简单递推
Time limit: 1.0 second Memory limit: 64 MB On the Day of the Flag of Russia a shop-owner decided to ...
- 利用Cayley-Hamilton theorem 优化矩阵线性递推
平时有关线性递推的题,很多都可以利用矩阵乘法来解决. 时间复杂度一般是O(K3logn)因此对矩阵的规模限制比较大. 下面介绍一种利用利用Cayley-Hamilton theorem加速矩阵乘法的方 ...
- 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】
还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...
- 简单递推 HDU-2108
要成为一个ACMer,就是要不断学习,不断刷题...最近写了一些递推,发现递推规律还是挺明显的,最简单的斐波那契函数(爬楼梯问题),这个大家应该都会,看一点稍微进阶了一点的,不是简单的v[i] = v ...
- [ACM_动态规划] 数字三角形(数塔)_递推_记忆化搜索
1.直接用递归函数计算状态转移方程,效率十分低下,可以考虑用递推方法,其实就是“正着推导,逆着计算” #include<iostream> #include<algorithm> ...
- 矩阵乘法&矩阵快速幂&矩阵快速幂解决线性递推式
矩阵乘法,顾名思义矩阵与矩阵相乘, 两矩阵可相乘的前提:第一个矩阵的行与第二个矩阵的列相等 相乘原则: a b * A B = a*A+b*C a*c+b*D c d ...
- openjudge1768 最大子矩阵[二维前缀和or递推|DP]
总时间限制: 1000ms 内存限制: 65536kB 描述 已知矩阵的大小定义为矩阵中所有元素的和.给定一个矩阵,你的任务是找到最大的非空(大小至少是1 * 1)子矩阵. 比如,如下4 * 4的 ...
随机推荐
- MVC+knocKout.js 实现下拉框级联
数据库:部门表和员工表 在控制器里面的操作: public ActionResult Index3() { ViewBag.departments = new SelectList(getDepart ...
- 常用git命令纪录
git branch xxx 新建分支xxx git branch -a 查看所有分支(包括远程) git remote add origin http://xxx.git 在本地添加一个远程仓库, ...
- Ubuntu 16.04 几个国内更新源
http://blog.csdn.net/paincupid/article/details/52895676 如何更改源 1/ 在修改source.list前,最好先备份一份 执行备份命令 sudo ...
- VS2013 ViewData ViewBag Ajax等关键词报错(当前上下文不存在名称)而且不提示也点不出来,但是可以正常运行,
这个多数问题是因为 视图 的Web.config 内的配置问题 在Views文件夹下 有一个Web.config文件,把里面的版本号(System.Web.Mvc, Version=5.2.2.0) ...
- matlab中patch函数的用法
http://blog.sina.com.cn/s/blog_707b64550100z1nz.html matlab中patch函数的用法——emily (2011-11-18 17:20:33) ...
- C#利用微软库完成设备网络定位(经纬度-地址)
public delegate void OnPositionChangedEventHandle(object sender, PositionChangedEventArgs e); public ...
- LeetCode:Two Sum II
public class Solution { public int[] twoSum(int[] numbers, int target) { int left = 0; int right = n ...
- 第二天 ado.net, asp.net ,三层笔记
1. ado.net步骤: 一:倒入命名空间 using System.Data; using System.Data.sqlclient; 二:第一个模型 int ...
- 【003:switch 不加 break的结果
#include <stdio.h> int main(){ char ch = 's'; switch(ch){ case 'a':{ printf("aaaaa") ...
- Yii2中多表关联查询(join、joinwith)
我们用实例来说明这一部分 表结构 现在有客户表.订单表.图书表.作者表, 客户表Customer (id customer_name) 订单表Order (id order_name ...