URAL 1119. Metro(简单dp)

URAL 1119. Metro(简单dp)

题目链接:https://cn.vjudge.net/contest/190727#problem/C
题意:给一个n*m的方格,某人从(0,0)出发,要到达(n,m),给出k个点的坐标,当走到这些点时可以沿对角线到达,每方格边长为100,问他需走的最短路程。
题目分析:对于每个将要到达的点,取它dp[i -1][j] ,dp[i][j - 1], dp[i - 1][j - 1]最小值即可。

input:
3 2
3
1 1
3 2
1 2
output:
383

#include 
using namespace std;

const int maxn = 1e3 + 100;
const double e = sqrt(2) * 100;
double dp[maxn][maxn];
bool vis[maxn][maxn];

int main()
{
    int n, m;
    int k;
    scanf("%d %d", &m, &n);
    scanf("%d", &k);
    for(int i = 0; i < k; i++) {
        int u, v;
        scanf("%d %d", &u, &v);
        vis[v][u] = true;
    }

    for(int i = 0; i <= n; i++) {
         for(int j = 0; j <= m; j++)
            dp[i][j] = 1e10;
    }
    for(int i = 1; i <= m; i++) dp[0][i] = i * 100;
    for(int i = 1; i <= n; i++) dp[i][0] = i * 100;
    dp[0][0] = 0;

    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= m; j++) {
            if(vis[i][j]) dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + e);
            dp[i][j] = min(dp[i][j], dp[i - 1][j] + 100);
            dp[i][j] = min(dp[i][j], dp[i][j - 1] + 100);
        }
    }
    printf("%.0lf\n", dp[n][m]);
}

你可能感兴趣的:(简单dp)