题目本身一点儿都不难,但是就是被字典序搞死了。写的挺麻烦,但是过了,逆向做好做一点儿。
/* 1619 */
#include <cstdio>
#include <cstring>
#include <cstdlib> #define MAXN 15
#define MAXM 105
#define INF 9999999 int dp[MAXN][MAXM];
int path[MAXN][MAXM];
int map[MAXN][MAXM];
int stack[MAXM], top;
int d[];
int ans;
int n, m; void solve(int beg) {
int i, j, k, tmp; for (i=; i<n; ++i)
dp[i][m-] = INF;
dp[beg][m-] = map[beg][m-];
path[beg][m-] = -; for (j=m-; j>=; --j) {
for (i=; i<n; ++i) {
if (i == )
d[] = n - ;
else
d[] = i - ;
d[] = i;
if (i == n-)
d[] = ;
else
d[] = i + ;
if (dp[d[]][j+] == dp[d[]][j+]) {
k = d[]<d[] ? d[]:d[];
if (dp[k][j+] == dp[d[]][j+])
k = k<d[] ? k:d[];
else if (dp[k][j+] < dp[d[]][j+])
k = k;
else
k = d[];
} else if (dp[d[]][j+] < dp[d[]][j+]) {
if (dp[d[]][j+] == dp[d[]][j+])
k = d[]<d[] ? d[]:d[];
else if (dp[d[]][j+] < dp[d[]][j+])
k = d[];
else
k = d[];
} else {
if (dp[d[]][j+] == dp[d[]][j+])
k = d[]<d[] ? d[]:d[];
else if (dp[d[]][j+] < dp[d[]][j+])
k = d[];
else
k = d[];
}
path[i][j] = k;
dp[i][j] = dp[k][j+];
if (dp[i][j] != INF)
dp[i][j] += map[i][j];
}
} tmp = INF;
for (i=; i<n; ++i) {
if (dp[i][] < tmp) {
tmp = dp[i][];
k = i;
}
} if (tmp < ans) {
ans = tmp;
top = ;
j = ;
stack[top++] = k + ;
while (path[k][j] >= ) {
stack[top++] = path[k][j] + ;
k = path[k][j];
++j;
}
}
} int main() {
int i, j, k, tmp; #ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif while (scanf("%d %d", &n, &m) != EOF) {
for (i=; i<n; ++i)
for (j=; j<m; ++j)
scanf("%d", &map[i][j]);
ans = INF;
for (i=; i<n; ++i)
solve(i); for (i=; i<m-; ++i)
printf("%d ", stack[i]);
printf("%d\n", stack[m-]);
printf("%d\n", ans);
} return ;
}