[USACO08MAR]跨河River Crossing dp
題目描述
Farmer John is herding his N cows (1 <= N <= 2,500) across the expanses of his farm when he finds himself blocked by a river. A single raft is available for transportation.
FJ knows that he must ride on the raft for all crossings and that that adding cows to the raft makes it traverse the river more slowly.
When FJ is on the raft alone, it can cross the river in M minutes (1 <= M <= 1000). When the i cows are added, it takes M_i minutes (1 <= M_i <= 1000) longer to cross the river than with i-1 cows (i.e., total M+M_1 minutes with one cow, M+M_1+M_2 with two, etc.). Determine the minimum time it takes for Farmer John to get all of the cows across the river (including time returning to get more cows).
Farmer John以及他的N(1 <= N <= 2,500)頭奶牛打算過一條河,但他們所有的渡河工具,僅僅是一個木筏。 由於奶牛不會划船,在整個渡河過程中,FJ必須始終在木筏上。在這個基礎上,木筏上的奶牛數目每增加1,FJ把木筏劃到對岸就得花更多的時間。 當FJ一個人坐在木筏上,他把木筏劃到對岸需要M(1 <= M <= 1000)分鐘。當木筏搭載的奶牛數目從i-1增加到i時,FJ得多花M_i(1 <= M_i <= 1000)分鐘才能把木筏劃過河(也就是說,船上有1頭奶牛時,FJ得花M+M_1分鐘渡河;船上有2頭奶牛時,時間就變成M+M_1+M_2分鐘。後面的依此類推)。那麼,FJ最少要花多少時間,才能把所有奶牛帶到對岸呢?當然,這個時間得包括FJ一個人把木筏從對岸劃回來接下一批的奶牛的時間。
輸入輸出格式
輸入格式:* Line 1: Two space-separated integers: N and M
* Lines 2..N+1: Line i+1 contains a single integer: M_i
輸出格式:* Line 1: The minimum time it takes for Farmer John to get all of the cows across the river.
輸入輸出樣例
輸入樣例#1: 複製5 10 3 4 6 100 1輸出樣例#1: 複製
50
說明
There are five cows. Farmer John takes 10 minutes to cross the river alone, 13 with one cow, 17 with two cows, 23 with three, 123 with four, and 124 with all five.
Farmer John can first cross with three cows (23 minutes), then return (10 minutes), and then cross with the last two (17 minutes). 23+10+17 = 50 minutes total.
考慮 dp[ i ] 表示前 i 個奶牛的總耗費;
那麼 dp 轉移的時候列舉轉移點即可;注意要加上單獨返回時的時間;
#include<iostream> #include<cstdio> #include<algorithm> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<map> #include<set> #include<vector> #include<queue> #include<bitset> #include<ctime> #include<deque> #include<stack> #include<functional> #include<sstream> //#include<cctype> //#pragma GCC optimize("O3") using namespace std; #define maxn 200005 #define inf 0x3f3f3f3f #define INF 9999999999 #define rdint(x) scanf("%d",&x) #define rdllt(x) scanf("%lld",&x) #define rdult(x) scanf("%lu",&x) #define rdlf(x) scanf("%lf",&x) #define rdstr(x) scanf("%s",x) typedef long long ll; typedef unsigned long long ull; typedef unsigned int U; #define ms(x) memset((x),0,sizeof(x)) const long long int mod = 1e9 + 7; #define Mod 1000000000 #define sq(x) (x)*(x) #define eps 1e-3 typedef pair<int, int> pii; #define pi acos(-1.0) const int N = 1005; #define REP(i,n) for(int i=0;i<(n);i++) typedef pair<int, int> pii; inline ll rd() { ll x = 0; char c = getchar(); bool f = false; while (!isdigit(c)) { if (c == '-') f = true; c = getchar(); } while (isdigit(c)) { x = (x << 1) + (x << 3) + (c ^ 48); c = getchar(); } return f ? -x : x; } ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a%b); } ll sqr(ll x) { return x * x; } /*ll ans; ll exgcd(ll a, ll b, ll &x, ll &y) { if (!b) { x = 1; y = 0; return a; } ans = exgcd(b, a%b, x, y); ll t = x; x = y; y = t - a / b * y; return ans; } */ ll qpow(ll a, ll b, ll c) { ll ans = 1; a = a % c; while (b) { if (b % 2)ans = ans * a%c; b /= 2; a = a * a%c; } return ans; } int n, m; int dp[maxn]; int main() { //ios::sync_with_stdio(0); rdint(n); rdint(dp[0]); for (int i = 1; i <= n; i++) { int tmp; rdint(tmp); dp[i] = dp[i - 1] + tmp; } for (int i = 1; i <= n; i++) { for (int j = 1; j < i; j++) { dp[i] = min(dp[i], dp[j] + dp[i - j] + dp[0]); } } cout << dp[n] << endl; return 0; }