UVA 662 Fast Food +经典动态规划

题目链接:点击进入

以前也碰到过这种类型的dp,感觉就是状态不好定义和转移;原来定义状态的时候总是认为dp[i][j]就应该由dp[i-1][j-1]转移而来,这样的话就会导致需要记忆前面i-1步的所有状态,然后就是转移方程没法写了。对于这道题,我们定义状态dp[i][j]表示前j个餐馆建立i个仓库时的最小代价,然后状态转移为dp[i][j]=dp[i-1][k-1]+cost[k][j],意思是:已经建立的前i-1个仓库负责前k-1个餐馆,然后第i个餐馆负责第k–j个餐馆,其中k<=i<=j; 然后就是这个题目还需要打印每一次的选择,我们通过最后的状态反推回去就行了。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

int dp[33][220],n,k;
int a[222],cost[220][220];

int abs(int x)
{
    if(x<0) x=-x;
    return x;
}

int cal(int i,int j)
{
    int m=(i+j)/2;
    int sum=0;
    for(int d=i;d<=j;d++)
        sum+=abs(a[d]-a[m]);
    return sum;
}

void print(int i,int j)
{
    if(i==1)
    {
        int d=(i+j)/2;
        printf("Depot %d at restaurant %d serves restaurants %d to %d\n",i,d,i,j);
        return ;
    }
    else
    {
        for(int d=i;d<=j;d++)
        {
            if(dp[i-1][d-1]+cost[d][j]==dp[i][j])
            {
               print(i-1,d-1);
               printf("Depot %d at restaurant %d serves restaurants %d to %d\n",i,(d+j)/2,d,j);
               return ;
            }
        }
    }

}

int main()
{
    ///freopen("in.txt","r",stdin);
    int Case=0;
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        if(n==0&&k==0) break;
        for(int i=1;i<=n;i++)
           scanf("%d",&a[i]);
        memset(dp,0x3f,sizeof(dp));
        for(int i=1;i<=n;i++)
           for(int j=i;j<=n;j++)
              cost[i][j]=cal(i,j);
        for(int i=1;i<=n;i++)
           dp[1][i]=cost[1][i];
        for(int i=2;i<=k;i++)
          for(int j=i;j<=n;j++)
          {
             for(int d=i;d<=j;d++)
               dp[i][j]=min(dp[i-1][d-1]+cost[d][j],dp[i][j]);
          }
        printf("Chain %d\n",++Case);
        print(k,n);
        printf("Total distance sum = %d\n\n",dp[k][n]);
    }
  return 0;
}
时间: 2024-10-05 10:47:37

UVA 662 Fast Food +经典动态规划的相关文章

UVa 662 - Fast Food

题目:一条街道上有n家餐馆,现在想建立k个仓库,储存代价是每个餐馆到最近的仓库的距离和: 求最小的储存代价. 分析:dp,中位数,动态规划. 状态:f(i,j)表示前j家餐馆建立i个仓库的最小储存代价: 状态转移:f(i,j)= min(f(i-1,k)+ cost(k+1,j)){ 其中 i-1 < k < j }: 其中cost(k+1,j)是在k+1,..,到j间建立一个仓库供这j-k个餐馆使用的最小单价: 此时,必然建立在中位数位置:存储路径递归逆序输出即可. 说明:之前写的不是永中位

UVA 662 Fast Food(DP)

The fastfood chain McBurger owns several restaurants along a highway. Recently, they have decided to build several depots along the highway, each one located at a restaurent and supplying several of the restaurants with the needed ingredients. Natura

DP(递归打印路径) UVA 662 Fast Food

题目传送门 题意:n个饭店在一条直线上,给了它们的坐标,现在要建造m个停车场,饭店没有停车场的要到最近的停车场,问所有饭店到停车场的最短距离 分析:易得区间(i, j)的最短距离和一定是建在(i + j) / 2的饭店,预处理出(i, j)的距离和sum[i][j],mark[i][j] 表示区间的最优停车场的位置,mid[i][j]表示(i + j) / 2.状态转移方程:dp[i][j] = max (dp[k-1][j-1] + sum[k][i]): 收获:学习递归打印路径 代码: /*

Problem W UVA 662 二十三 Fast Food

Fast Food Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 662 Appoint description:  System Crawler  (2015-08-27) Description The fastfood chain McBurger owns several restaurants along a highway. Recen

UVA 11992 - Fast Matrix Operations

Fast Matrix Operations There is a matrix containing at most 106 elements divided into r rows and c columns. Each element has a location (x,y) where 1<=x<=r,1<=y<=c. Initially, all the elements are zero. You need to handle four kinds of operati

uva 662

dp +路径输出 #include <cstdio> #include <cstdlib> #include <cmath> #include <stack> #include <vector> #include <sstream> #include <cstring> #include <string> #include <map> #include <set> #include &l

uva 1626 Brackets Sequence ?(动态规划)

状态表示方法:d[ i ][ j ]表示的是一条序列的开始和结束: 状态定义:d[ i ][ j ]表示字串s[ i~j ] 需要添加的数量. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n; char s[105]; int d[105][105]; bool match(char ch1,char ch2) { if((ch1=='['&&am

UVA 11992(Fast Matrix Operations-线段树区间加&amp;改)[Template:SegmentTree]

Fast Matrix Operations There is a matrix containing at most 106 elements divided into r rows and c columns. Each element has a location (x,y) where 1<=x<=r,1<=y<=c. Initially, all the elements are zero. You need to handle four kinds of operati

UVa 11992 Fast Matrix Operations (线段树)

Fast Matrix Operations Description There is a matrix containing at most 106 elements divided into r rows and c columns. Each element has a location (x, y) where 1 ≤ x ≤ r, 1 ≤ y ≤ c. Initially, all the elements are zero. You need to handle four kinds