poj1485 Fast Food 动态规划

 
  
//poj 1485
//fenix_bao
#include 

using namespace std;
const int MAXN=256,MAXK=32;
int pos[MAXN],dp[MAXN][MAXK],dis[MAXN][MAXN],one_cover[MAXN][MAXN];
int sum_dis[MAXN][MAXN],sum_dis_rev[MAXN][MAXN];
int one_cover_mark[MAXN][MAXN];
int dp_mark[MAXN][MAXK];

void myprint(int n,int k){
    if(k==0){
        return;
    }
    int pre_n = dp_mark[n][k];
    int pre_k = k-1;
    myprint(pre_n,pre_k);
    int from = pre_n+1;
    int to = n;
    if(from!=to){
        printf("Depot %d at restaurant %d serves restaurants %d to %d\n",k,one_cover_mark[from][to],from,to);
    }else{
        printf("Depot %d at restaurant %d serves restaurant %d\n",k,from,from);
    }
}


int main() {
    int n,k,case_cnt=0;
    while(scanf("%d%d",&n,&k)==2&&(n||k)){
        for(int i=1;i<=n;++i)
            scanf("%d",&pos[i]);

        for(int i=1;i<=n;++i)
            for(int j=i;j<=n;++j)
                dis[i][j]=dis[j][i]=pos[j]-pos[i];

        for(int i=1;i<=n;++i) {
            sum_dis[i][i]=0;
            for (int j = i-1; j >= 1; --j) {
                sum_dis[j][i] = sum_dis[j+1][i]+dis[j][i];
            }
            sum_dis_rev[i][i]=0;
            for (int j = i+1; j<=n; ++j) {
                sum_dis_rev[i][j] = sum_dis_rev[i][j-1]+dis[i][j];
            }
        }

        for(int i=1;i<=n;++i)
            for(int j=1;j<=n;++j)
                one_cover[i][j]=INT_MAX,dp[i][j]=INT_MAX;

        for(int i=1;i<=n;++i)
            for(int j=i;j<=n;++j)
                for(int p=i;p<=j;++p){
                    if(sum_dis[i][p]+sum_dis_rev[p][j]<=one_cover[i][j]){
                        one_cover[i][j] = sum_dis[i][p]+sum_dis_rev[p][j];
                        one_cover_mark[i][j] = p;
                    }
                }

        dp[0][0]=0;
        for(int i=1;i<=n;++i)
            for(int p=1;p<=i&&p<=k;++p)
                for(int j=p-1;j

你可能感兴趣的:(dp)