题解 | #矩阵的最小路径和#
矩阵的最小路径和
http://www.nowcoder.com/practice/38ae72379d42471db1c537914b06d48e
#include<iostream>
using namespace std;
int graph[501][501],dp[501][501];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n,m;
cin >> n >> m;
for(int i = 1;i <= n;++i)
for(int j = 1;j <= m;++j){
cin >> graph[i][j];
if(i == 1 && j == 1)
dp[i][j] = graph[i][j];
else if(i == 1)
dp[i][j] = dp[i][j - 1] + graph[i][j];
else if(i >= 2 && j != 1)
dp[i][j] = min(dp[i - 1][j] + graph[i][j],dp[i][j - 1] + graph[i][j]);
else
dp[i][j] = dp[i - 1][j] + graph[i][j];
}
cout << dp[n][m];
}