Metro(Ural_1119)

来源:互联网 发布:大华130万网络摄像机 编辑:程序博客网 时间:2024/05/23 19:16

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

这题就是一道简单的动规,设dp[i][j]为到达坐标(i,j)时的最短路,那么到达该坐标的方法一共只有三种,从西边来,从南边来和从西南边来,从 到达这三种状态的最短路径长度加上从这三种状态再到(i,j)的路径长度 中找出最小的那个,就是到达(i,j)的最短路。(囧rz

代码

#include <iostream>#include <cstdio>#include <cstring>using namespace std;int dia[1010][1010];double dp[1010][1010];int main(){    int n, m;    int a, b, c;    memset(dia, 0, sizeof(dia));    memset(dp, 0, sizeof(dp));    scanf("%d%d", &n, &m);    scanf("%d", &a);    for(int i = 0; i < a; i++)    {        scanf("%d%d", &b, &c);        dia[b][c] = 1;    }    for(int i = 1; i <= n; i++)        dp[i][0] = dp[i-1][0] + 100;    for(int i = 1; i <= m; i++)        dp[0][i] = dp[0][i-1] + 100;    for(int i = 1; i <= n; i++)    {        for(int j = 1; j <= m; j++)        {            dp[i][j] = min(dp[i][j-1], dp[i-1][j]) + 100;            if(dia[i][j])            {                dp[i][j] = min(dp[i][j], dp[i-1][j-1] + 141.421356); //注意精度,这里一开始写了141.4,WA了            }        }    }    printf("%.0f\n",dp[n][m]); //要求四舍五入,千万别强制转换成int,会WA    return 0;}
0 0
原创粉丝点击