Description
Many of SKB Kontur programmers like to get to work by Metro because the main office was situated quite close the station Ur Almash. So, since a sedentary life requires active exercises off-duty, many of the staff-nikifor among Them-walk from their ho Mes to Metro stations on foot. Nikifor lives in a part of our city where streets form a grid of residential quarters. All the quarters is squares with side meters. A Metro entrance is situated at one of the crossroads. Nikifor starts his another crossroad which are south and west of the Metro entrance. Naturally, nikifor, starting from he home, walks along the streets leading either to the north or to the east. On his, he may cross some quarters diagonally from their south-western corners to the north-eastern ones. Thus, some of the routes is shorter than others. Nikifor wonders, how long is the shortest route. You is to write a program that would calculate the length of the shortest route from the South-western corner ofThe grid to the North-eastern one.
Input
There is integers in the first line:
Nand
M(0 <
N,
M≤1000)-west-east and South-north sizes of the grid. Nikifor starts his to from a crossroad which was situated south-west of the quarter with coordinates (1, 1). A Metro Station was 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 so can be crossed diagonally. Then
KLines with pairs of numbers separated with a space Follow-these is the coordinates of those quarters.
Output
Your program was to output a length of the shortest route from Nikifor's home to the Metro station in meters, rounded to th E integer amount of meters.
Sample Input
input |
Output |
3 231 13) 21 2 |
383 |
The main idea: To find the shortest point to another point, the precision of the pit Orz ... Open double, it's over.For normal DP, set the initialized value, list the state transfer equation, and determine the end point trilogy
#include <cstdio> #include <cstring> #include <algorithm> #include <cmath>using namespace std; const int inf = 0x3f3f3f3f;double dp[1005][1005];int map1[1005][1005];int main () {int n,m,k,x,y; while (~SCANF ("%d%d", &n,&m)) {scanf ("%d", &k); memset (map1,0,sizeof (MAP1)); for (int i = 1; I <= K; i++) {scanf ("%d%d", &x,&y); MAP1[X+1][Y+1] = 1; } for (int i = 0, I <= n+1; i++) for (int j = 0; J <= M+1; j + +) dp[i][j] = inf; DP[1][1] = 0; for (int i = 1, i <= n+1; i++) {for (int j = 1; J <= M+1; j + +) {if (i = = 1 && j = = 1 ) continue; if (map1[i][j] = = 1) dp[i][j] = min (min (dp[i][j-1]+100,dp[i-1][j]+100), DP[I-1][J-1]+100*SQRT (2)); else Dp[i][j] = min (dp[i][j-1]+100,dp[i-1][j]+100); }} printf ("%.0lf\n", dp[n+1][m+1]); }return 0;}
Ural1119--dp--metro