Description
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.
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.
大意:求一点到另一点的最短路,精度好坑orz。。。开double就过了
就普通dp,弄好初始化的值,列好状态转移方程,确定终点三部曲
#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
",dp[n+1][m+1]);
}
return 0;
}