标签:
#include<bits/stdc++.h> #define REP(i,a,b) for(int i=a;i<=b;i++) #define MS(a) memset(a,0,sizeof(a)) #define MS1(a) memset(a,-1,sizeof(dp)) using namespace std; typedef long long ll; const int maxn=1100; const int INF=1<<29; int n,m; int a[maxn][maxn]; int dp[maxn][maxn]; int nx[maxn][maxn]; int dfs(int i,int j) { int &res=dp[i][j]; if(~res) return res; if(i==n) return res=a[i][j]; int nj[3]={j,(j-2+m)%m+1,j%m+1}; sort(nj,nj+3); res=INF; REP(k,0,2){ int t=dfs(i+1,nj[k])+a[i][j]; if(t<res){ res=t; nx[i][j]=nj[k]; } } return res; } int main() { freopen("in.txt","r",stdin); while(cin>>m>>n){ if(m==0) break; REP(i,1,m) REP(j,1,n) scanf("%d",&a[j][i]); /* REP(i,1,m){ REP(j,1,n) cout<<a[j][i]<<" "; cout<<endl; } */ MS1(dp);MS1(nx); int ans=INF; int x=1; REP(i,1,m){ int t=dfs(1,i); if(t<ans) ans=t,x=i; } //cout<<"ans="<<ans<<endl; printf("%d",x); int cur=1; for(int i=nx[cur][x];i!=-1;cur++,i=nx[cur][i]) printf(" %d",i); printf("\n%d\n",ans); } return 0; } /** 还是用dfs去弄字典序最小比较方便,终于AC了 */
标签:
原文地址:http://www.cnblogs.com/--560/p/5063862.html