• 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
    ",Dp[n][m]);
        }
        return 0;
    }
    
  • 相关阅读:
    array_udiff_assoc — 带索引检查计算数组的差集,用回调函数比较数据
    array_sum — 对数组中所有值求和
    array_splice — 去掉数组中的某一部分并用其它值取代
    array_slice — 从数组中取出一段
    array_multisort — 对多个数组或多维数组进行排序
    array_merge — 合并一个或多个数组
    array_keys — 返回数组中部分的或所有的键名
    array_key_exists — 检查数组里是否有指定的键名或索引
    array_intersect_assoc — 带索引检查计算数组的交集
    array_flip — 交换数组中的键和值
  • 原文地址:https://www.cnblogs.com/juechen/p/5255867.html
Copyright © 2020-2023  润新知