Metro
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 8192/4096K (Java/Other)
Total Submission(s) : 8 Accepted Submission(s) : 4
Problem Description
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.
input output 3 2 3 1 1 3 2 1 2 383
这道题动归方程应该很好想
f[i][j]=min(f[i-1][j]+100,f[i][j-1]+100,f[i-1][j-1]+sqrt(20000),f[i][j])
我f[i][j]设为double,自己测能过,但是提交死活报错ce。
最后求助机智的网友,是类型转换的问题,于是我加入了强制转换,动归方程调整为
f[i][j]=min(f[i-1][j]+double(100),f[i][j-1]+double(100),f[i-1][j-1]+sqrt(double(20000)),f[i][j])
代码
#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
long can[1005][1005]={0},i,j,n,m;
double f[1005][1005]={0};
int main()
{
int a,b,k;
cin>>n>>m;
cin>>k;
for (i=1;i<=k;i++)
{
cin>>a>>b;
can[a][b]=1;
}
for (i=1;i<=n+1;i++)
for (j=1;j<=m+1;j++)
f[i][j]=99999999;
f[1][1]=0;
for (i=1;i<=n+1;i++)
for (j=1;j<=m+1;j++)
{
if (f[i-1][j]+double(100)<f[i][j]&&i-1>0) f[i][j]=f[i-1][j]+double(100);
if (f[i][j-1]+double(100)<f[i][j]&&j-1>0) f[i][j]=f[i][j-1]+double(100);
if (can[i-1][j-1]==1&&f[i-1][j-1]+sqrt(double(20000))<f[i][j]) f[i][j]=f[i-1][j-1]+sqrt(double(20000));
}
printf("%0.0lf",f[n+1][m+1]);
return 0;
}