【动态规划】[USACO2016 金组]Circular Barn Revisited

题目描述

After the last debacle involving Farmer John’s circular barn, one would think he had learned his lesson about non-traditional architecture. However, he thinks he can still make his circular barn (from the preceding problem) function properly by allowing multiple cows into each room. To recap, the barn consists of a ring of nn rooms, numbered clockwise from 1…n1…n around the perimeter of the barn (3≤n≤1003≤n≤100). Each room has doors to its two neighboring rooms, and also a door opening to the exterior of the barn.
Farmer John wants exactly riri cows to end up in room ii (1≤ ri ≤1,000,0001≤ ri ≤1,000,000). To herd the cows into the barn in an orderly fashion, he plans to unlock k exterior doors (1≤k≤7), allowing the cows to enter through only those doors. Each cow then walks clockwise through the rooms until she reaches a suitable destination. Farmer John wants to unlock the exterior doors that will cause his cows to collectively walk a minimum total amount of distance after entering the barn (they can initially line up however they like outside the k unlocked doors; this does not contribute to the total distance in question). Please determine the minimum total distance his cows will need to walk, if he chooses the best k such doors to unlock.

INPUT FORMAT (file cbarn2.in):

The first line of input contains nn and kk. Each of the remaining nn lines contain r1…rnr1…rn.

OUTPUT FORMAT (file cbarn2.out):

Please write out the minimum amount of distance the cows need to travel.

SAMPLE INPUT:

6 2
2
5
4
2
6
2

SAMPLE OUTPUT:

14

题目分析

我们令 dp(i,j,k) 表示第一个位置在 i 当前放置的位置在j正在选择了第 k 个门的位置的最优状态那么我们显然有答案为min{dp(i,j,K)}同时有

dp(i,j,k)=min{dp(i1,t,k1)+val(t,j1,0)}
其中 val(i,j,k) 表示从 i j中所有的牛从 i 出发归位需要走过的总距离k表示修正值为了特殊处理当 k=K 的情况
dp(i,j,K)=min{dp(i,t,k1)+val(t,j1,0)+val(j,n,0)+val(1,i,nj+1)}

代码

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
const int MAXN = 100;
const long long INF = 1e18+7;
long long dp[MAXN+10][10], sum[MAXN+10];
int n, k;
inline long long val(int l, int r, long long fix){
    long long ret = 0;
    for(int i=l;i<r;i++)
        ret += sum[i] * (i - l + fix);
    return ret;
}
long long solve(int beg){
    for(int i=1;i<k;i++){
        for(int j=1;j<=n;j++) if(~dp[j][i]){
            for(int t=j+1;t<=n;t++){
                if(dp[t][i+1] == -1) dp[t][i+1] = INF;
                dp[t][i+1] = min(dp[t][i+1], dp[j][i]+val(j, t, 0));
            }
        }
    }
    long long ret = INF;
    for(int i=1;i<=n;i++)
        if(~dp[i][k])
            ret = min(ret, dp[i][k]+val(i, n+1, 0)+val(1, beg, n-i+1));
    return ret;
}
int main(){
    scanf("%d%d", &n, &k);
    for(int i=1;i<=n;i++)scanf("%lld", &sum[i]);
    long long ans = INF;
    for(int i=1;i<=n;i++){
        memset(dp, -1, sizeof dp);
        dp[i][1] = 0;
        ans = min(ans, solve(i));
    }
    printf("%lld\n", ans);

    return 0;
}
USACO2022金组是国际在线判题系统USACO的最高级别,题目难度较高,在该比赛中取得好成绩是一项巨大的成就。以下是对该比赛的一些题目解析。 第一题:“交通计划” 题目要求:给定一个n个节点的有向图,每条边有一个长度,希望添加最少的边使得所有节点连通,求最小生成树的权值和。 解析:该题可以使用Kruskal算法求解,将每条边按权值从小到大排序,再依次加入,判断加入的边是否会形成环,若形成则不加入,直到所有节点连通为止。此时Kruskal算法得到的最小生成树的权值和即为所求。 第二题:“点火计划” 题目要求:给定一个n个节点的有向图,每条边有一个权值和一个点火时长,每个节点有一个点火启动时刻和时刻结束时刻,希望从其中选出一些边点火,使得所有节点都可从点火的边出发到达,且所选点火边的总点火时长最小。 解析:该题可以使用最小费用最大流算法求解。将每条边看做一个容量为1,费用为点火时长的边,源点向节点的点火边容量为1,费用为0的边,节点的点火边向汇点的容量为1,费用为0的边,对这个网络进行最小费用最大流即可得到所选边的总点火时长最小。 第三题:“美味佳肴” 题目要求:给定n个菜品,每个菜品有它的权值和两个类别,希望选出k个菜品,使得选出的菜品数量在每个类别中都不超过$\frac{k}{3}$个,且所选菜品的权值和最大。 解析:该题可以使用动态规划求解。设$f[i][j][k]$表示前i个菜品中,选择j个一类菜品,选择k个二类菜品的最大权值和,状态转移方程为$f[i][j][k]=max(f[i-1][j][k],f[i-1][j-1][k]+a[i],f[i-1][j][k-1]+b[i])$,其中a[i]为i号菜品的权值,若为一类则为该权值,否则为0,b[i]为i号菜品的权值,若为二类则为该权值,否则为0。最终答案为$f[n][$k/3$][$k/3$]。 以上是对USACO2022金组的部分题目的解析,USACO比赛是全球范围内的计算机竞赛,竞争非常激烈,能够在该比赛中脱颖而出是一项非常棒的成就。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值