日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

NYOJ 716 River Crossing(动态规划)

發布時間:2025/3/16 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 NYOJ 716 River Crossing(动态规划) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

River Crossing

時間限制:1000?ms ?|? 內存限制:65535?KB 難度:4

描述

Afandi is herding?N?sheep?across the expanses of?grassland??when he finds himself blocked by a river. A single raft is available for transportation.

?

Afandi knows that he must ride on the raft for all crossings, but adding sheep to the raft makes it traverse the river more slowly.

?

When Afandi is on the raft alone, it can cross the river in M minutes When the i sheep are added, it takes Mi minutes longer to cross the river than with i-1 sheep (i.e., total M+M1 ??minutes with one sheep, M+M1+M2 with two, etc.).

?

Determine the minimum time it takes for Afandi to get all of the sheep across the river (including time returning to get more sheep).


輸入
On the first line of the input is a single positive integer k, telling the number of test cases to follow. 1 ≤ k ≤ 5 Each case contains:

* Line 1: one space-separated integers: N and M (1 ≤ N ≤ 1000 , 1≤ M ≤ 500).

* Lines 2..N+1: Line i+1 contains a single integer: Mi (1 ≤ Mi ≤ 1000)

輸出

For each test case, output a line with the minimum time it takes for Afandi to get all of the sheep across the river.
樣例輸入
2 2 10 3 5 5 10 3 4 6 100 1
樣例輸出
18 50

這是去年省賽的一道題,當時寫的代碼交上去一直WA,和隊友討論了好長時間也沒找到哪里錯了。后來才知道我們的思想就是錯的。因為這個題是dp,但是我們一直當成貪心做的。比賽完就放下沒做,沒想到今天比賽又拉出來了,雖然知道是dp,但是由于沒有找到狀態轉移方程,導致今天又沒有做出來。好傷心。。。。

題意:有1個人和N只羊要過河。一個人單獨過河花費的時間是M,每次帶一只羊過河花費時間M+M1,帶兩只羊過河花費時間M+M1+M2……給出N、M和Mi,問N只羊全部過河最少花費的時間是多少。

分析:用一個前綴和數組time,time[i]表示單獨運送i只羊所花費的時間。dp[i]表示一個人和i只羊過河所花費的最短時間,則開始時dp[i] = time[i] + M,以后更新時,dp[i] = min(dp[i],dp[i-j] + m + dp[j]),j從1循環到i-1,即把i只羊分成兩個階段來運,只需求出這兩個階段的和,然后加上人從對岸回來所用的時間,與dp[i]進行比較,取最小值。

#include<stdio.h> #include<algorithm> using namespace std; int dp[1005], time[1005]; int main() {int T, n, m, i, j;scanf("%d",&T);while(T--){int a;scanf("%d%d",&n, &m);time[0] = 0;for(i = 1; i <= n; i++){scanf("%d",&a);time[i] = time[i-1] + a;}for(i = 1; i <= n; i++){dp[i] = time[i] + m;for(j = 1; j < i; j++)dp[i] = min(dp[i], dp[i-j] + dp[j] + m);}printf("%d\n",dp[n]);}return 0; } //time[i]表示一次運送i只羊所花費的時間 //dp[i]表示人和i只羊一起過河所花費的最短時間

總結

以上是生活随笔為你收集整理的NYOJ 716 River Crossing(动态规划)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。