The title means to start with the first column and go to the last column, which is the minimum amount of weight.
There are three ways to go, the first line on top is the last line (see figure).
The optimal substructure is d[i] [j] =min (d[k][J]-g[I [j]) where K is the column J column, three transformations of the number of rows.
Walk from the point in column J of line I to the last row minimum value, equal to the value of the dot K row j + 1 column go to the last column of the minimum value plus (I,J) this point.
Because it requires the same length as the dictionary order, we can only go from the post DP.
AC Code:
#include <stdio.h> #include <string.h> #include <algorithm> #define LL long long using namespace std;
const int M = 15;
const int N = 105;
CONST LL inf = 0x3f3f3f3f3f3f3f3f;
ll G[m][n];
ll D[m][n];
int r,c;
ll min (ll A, ll B, ll c) {ll temp = (A < b? a:b);
ll temp2 = (Temp < c temp:c);
return TEMP2;
} ll DP (int l, int t) {if (d[l][t]! = INF) return d[l][t];
int Up,down;
int ahead = L;
if (L = = 0) up = r-1;
else up = l-1;
if (L = = r-1) down = 0;
else down = l + 1;
ll m = min (dp (UP, T + 1), DP (down, T + 1), DP (ahead, T + 1));
D[l][t] = m + g[l][t];
return d[l][t];
} void print (int l, int t) {if (t = = c-1) {printf ("%d", L + 1);
Return
} printf ("%d", L + 1);
int Up,down;
int ahead = L;
if (L = = 0) up = r-1;
else up = l-1;
if (L = = r-1) down = 0;
else down = l + 1;
int temp[3] = {up, ahead, down};
Sort (Temp, temp + 3); for (int i = 0; i < 3; i++) {if (D[l][t]-g[l][t] = = d[temp[i]][t + 1]) {
Print (Temp[i], T + 1);
Break }}} int main () {while (~scanf ("%d%d", &r,&c)) {for (int i = 0; i < M; i++) {for (int j = 0; J &L T
N; j + +) d[i][j] = inf;
} memset (g, 0, sizeof (g));
for (int i = 0; i < R; i++) {for (int j = 0; J < C; j + +) {scanf ("%lld", &g[i][j]);
}} for (int i = 0; i < R; i++) {d[i][c-1] = g[i][c-1];
} ll m = inf;
int res;
for (int i = 0; i < R; i++) {d[i][0] = DP (i, 0);
if (D[i][0] < m) {m = d[i][0];
res = i;
}} print (res, 0);
printf ("\ n");
printf ("%lld\n", m); }
}