这道题也成功AC~
#include <iostream>
#include <bits/stdc++.h>
#define maxn 100+10
#define INF 0x3f3f3f3f
using namespace std;
int maze[maxn][maxn];
int d[maxn][maxn];
int row[3];
int m,n;
int ne[maxn][maxn];
int main(){
while(cin>>m>>n && m && n){
memset(d,INF, sizeof(d));
memset(ne,0, sizeof(ne));
for(int i = 0;i<m;i++){
for(int j = 0;j<n;j++){
cin>>maze[i][j];
}
}
for(int i = 0;i<m;i++){
d[i][n-1] = maze[i][n-1];
}
for(int j = n-2;j>=0;j--){
for(int i = 0;i<m;i++){
row[0] = i;
row[1] = i-1;
row[2] = i+1;
if(i == 0){
row[1] = m-1;
}
if(i == m-1){
row[2] = 0;
}
sort(row,row+3);
//deal
for(int k = 0;k<3;k++) {
if(d[row[k]][j + 1] + maze[i][j]<d[i][j]){
d[i][j] = d[row[k]][j + 1] + maze[i][j];
ne[i][j] = row[k];
}
}
}
}
int ans = INF;
int first = 0;
for(int i = 0;i<m;i++){
if(ans>d[i][0]){
ans = d[i][0];
first = i;
}
}
//print
cout<<first+1;
int col = 0;
while (col<n-1){
first = ne[first][col];
cout<<" "<<first+1;
col++;
}
cout<<endl<<ans<<endl;
}
return 0;
}
/**
5 6
3 4 1 2 8 6
6 1 8 2 7 4
5 9 3 9 9 5
8 4 1 3 2 6
3 7 2 8 6 4
5 6
3 4 1 2 8 6
6 1 8 2 7 4
5 9 3 9 9 5
8 4 1 3 2 6
3 7 2 1 2 3
2 2
9 10 9 10
**/