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-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 ( 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
Problem Author: Leonid Volkov
Problem Source:  USU Open Collegiate Programming Contest October'2001 Junior Session

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

f[i][j]=min{f[i-1][j],f[i][j-1]}+1 (此处不能穿过)

f[i][j]=min{f[i-1][j]+1,f[i][j-1]+1,f[i-1,j-1]+sqrt(2)} (此处可以穿过)

        左           下             对角线



#include<stdio.h>
#include<math.h>
int n,m;
bool map[1005][1005];
double f[1005][1005];
double min(double x,double y)
{
    return x<y?x:y;
}
int main()
{
    int i,j,x,y,k;
    scanf("%d %d",&n,&m);
    scanf("%d",&k);
    for(i=0; i<k; i++)
    {
        scanf("%d %d",&x,&y);
        map[x][y]=true;
    }
    for(i=1; i<=n; i++)
        f[0][i]=f[0][i-1]+100;
    for(i=1; i<=m; i++)
    {
        f[i][0]=f[i-1][0]+100;
        for(j=1; j<=n; j++)
            f[i][j]=f[i][j-1]+100;
        for(j=1; j<=n; j++)
        {
            f[i][j]=min(f[i][j-1],f[i-1][j])+100;
            if(map[j][i])
                f[i][j]=f[i-1][j-1]+sqrt(2.0)*100;
        }
    }
    printf("%.lf",f[m][n]);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值