递推DP URAL 1119 Metro

时间:2022-08-08 20:52:55

题目传送门

 /*
题意:已知起点(1,1),终点(n,m);从一个点水平或垂直走到相邻的点距离+1,还有k个抄近道的对角线+sqrt (2.0);
递推DP:仿照JayYe,处理的很巧妙,学习:)
好像还要滚动数组,不会,以后再补
*/
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std; const int MAXN = 1e3 + ;
const int INF = 0x3f3f3f3f;
double dp[MAXN][MAXN];
int used[MAXN][MAXN];
int a[MAXN][MAXN]; int main(void) //URAL 1119 Metro
{
freopen ("C.in", "r", stdin); int n, m, k;
while (scanf ("%d%d", &n, &m) == )
{
scanf ("%d", &k);
int u, v;
memset (used, , sizeof (used));
while (k--) {scanf ("%d%d", &u, &v); used[u][v] = true;} for (int i=; i<=n; ++i)
{
for (int j=; j<=m; ++j)
{
if (!i && !j) dp[i][j] = ;
else if (!i) dp[][j] = dp[][j-] + ;
else if (!j) dp[][j] = dp[][j] + ;
else dp[][j] = min (dp[][j-], dp[][j]) + ;
if (used[i][j]) dp[][j] = min (dp[][j] + , dp[][j-] + sqrt (2.0));
}
} printf ("%.0f\n", dp[][m] * ); } return ;
}