URAL 1119 Metro
题目:click here
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int INF = 0x3f3f3f3f; 5 const int M = 1e3+3; 6 7 int n, m, k; 8 double dp[M][M]; // dp[i][j] 表示从[0,0]到[i,j]的最短距离 9 int mark[M][M]; // mark[i][j] 标记[i,j]与[i-1][j-1]之间有对角线 10 int main() { 11 while( ~scanf("%d%d%d", &n, &m, &k ) ) { 12 memset( mark, 0, sizeof(mark) ); 13 for( int i=0; i<k; i++ ) { 14 int x, y; scanf("%d%d", &x, &y ); 15 mark[x][y] = 1; 16 } 17 dp[0][0] = 0; 18 for( int i=1; i<=n; i++ ) dp[i][0] = dp[i-1][0] + 100; 19 for( int j=1; j<=m; j++ ) dp[0][j] = dp[0][j-1] + 100; 20 for( int i=1; i<=n; i++ ) { 21 for( int j=1; j<=m; j++ ) { 22 dp[i][j] = min( dp[i-1][j]+100, dp[i][j-1]+100 ); 23 if( mark[i][j] ) 24 dp[i][j] = min( dp[i][j], dp[i-1][j-1]+141.421 ); 25 } 26 } 27 printf("%.0f\n", dp[n][m] ); 28 } 29 return 0; 30 }