zoukankan      html  css  js  c++  java
  • ural 1119. Metro(动态规划)

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

    inputoutput
    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中的最小值。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 
     5 
     6 using namespace std;
     7 int s[1010][1010]={0};
     8 double dp[1010][1010]={0};
     9 
    10 
    11 
    12 double min(double a,double b,double c=9999999999)
    13 {
    14     if(a>b)
    15         return b<c?b:c;
    16     else
    17         return a<c?a:c;
    18 }
    19 
    20 
    21 
    22 int main()
    23 {
    24 //    freopen("1.txt","r",stdin);
    25     int n,m;
    26     cin>>n>>m;
    27     int k;
    28     cin>>k;
    29     int i,j;
    30     int a,b;
    31     n++;
    32     m++;
    33     for(i=1;i<=n;i++)
    34         dp[0][i]=9999999999;
    35     for(i=1;i<=m;i++)
    36         dp[i][0]=9999999999;
    37     for(i=0;i<k;i++)
    38     {
    39         cin>>a>>b;
    40         s[b+1][a+1]=1;
    41     }
    42     for(i=1;i<=m;i++)
    43     {
    44         for(j=1;j<=n;j++)
    45         {
    46             if(i==1&&j==1)continue;
    47             if(s[i][j]==1)
    48             {//如果改点可以由一点斜着到达
    49                 dp[i][j]=min(dp[i][j-1]+100,dp[i-1][j]+100,dp[i-1][j-1]+sqrt(2.0)*100);//比较得出dp[i][j-1]+100、dp[i-1][j]+100、dp[i-1][j-1]+sqrt(2)*100中的最小值;
    50             }//注意sqrt()里面是精度数,例如不可以是2,单可以是2.0
    51             else
    52             {//改点不可以由一点斜着到达
    53                 dp[i][j]=min(dp[i][j-1]+100,dp[i-1][j]+100);//比较求出dp[i][j-1]+100、dp[i-1][j]+100中的最小值
    54             }
    55         }
    56     }
    57     printf("%.0lf
    ",dp[m][n]);
    58     return 0;
    59 }
    View Code
  • 相关阅读:
    遇到容易忘记的问题(二)
    遇到容易忘记的问题(三)
    遇到容易忘记的问题(一)
    UC浏览器input文本框输入文字回车键自动提交
    弹框在当前屏幕完全居中的方法
    遇到的浏览器问题总结
    HTML常用的特殊符号&前端使用的标点符号
    小程序里的页面跳转
    移除所有子视图
    UIView用户事件响应
  • 原文地址:https://www.cnblogs.com/zhangchengbing/p/3304420.html
Copyright © 2011-2022 走看看