A +---2---+---3---+----1---+----2---+ | | | | | 2 1 2 2 3 | | | | | +---2---+---3---+----4---+---5----+ | | | | | 3 4 1 2 3 | | | | | +---2---+---2---+---1----+---4----+ | | | | | 2 2 1 3 4 | | | | | +---1---+---3---+---2----+---3----+ B
A +---2---+---3---+----1---+----2---+ | | | | | 2 1 2 2 3 | | | | | +---2---+---3---+----4---+---5----+ | | | | | 3 4 1 2 3 | | | | | +---2---+---2---+---1----+---4----+ | | | | | 2 2 1 3 4 | | | | | +---1---+---3---+---2----+---3----+ B
4 5 2 3 1 2 2 3 4 5 2 2 1 4 1 3 2 3 2 3 2 1 4 2 2 1 1 2 2 3 3 3 4
13
只能向下或向右
大意:求最短路就是另所有的路为无穷大,能走的取最小值,最后输出dp[n][m],这道题的数据处理有点难..
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int inf = 0x3f3f3f3f; int main() { int dp[110][110]; int x[110][110],y[110][110]; int n,m; while(~scanf("%d%d",&n,&m)){ //heng for(int i = 0; i <= n ;i++) for(int j = 0 ; j <= m ;j++) x[i][j] = y[i][j] = dp[i][j] = inf; for(int i = 1; i <= n;i++){ for(int j = 1; j <= m-1 ;j++){ scanf("%d",&x[i][j]); } } //shu for(int i = 1; i <= m ;i++){ for(int j = 1; j <= n-1 ;j++){ scanf("%d",&y[i][j]); } } dp[1][1] = 0; for(int i = 1;i <= n;i++){ for(int j = 1; j <= m;j++){ if(i == 1 && j == 1) continue; dp[i][j] = 0; dp[i][j] +=min(dp[i-1][j] + y[j][i-1],dp[i][j-1]+x[i][j-1]); } } printf("%d\n",dp[n][m]); } return 0; }
原文:http://www.cnblogs.com/zero-begin/p/4374699.html