24h購物| | PChome| 登入
2013-09-11 07:25:46| 人氣2,130| 回應0 | 上一篇 | 下一篇

[UVA][dp] 12324 - Philip J. Fry Problem

推薦 0 收藏 0 轉貼0 訂閱站台


  Philip J. Fry Problem 

It's been a few months since Bender solved his famous bending problem. A special request has arrived to Planet Express Inc., the interplanetary courier company where Bender works. Professor Farnsworth (founder, CEO, and chairman of the board) immediately prepared the mission and summoned his crew: Philip J. Fry, Turanga Leela, and Bender. To succeed in the mission, the crew has to make n trips $ tau_{1}^{}$, $ tau_{2}^{}$, ..., $ tau_{n}^{}$, in the exact order established by Professor Farnsworth. He has also provided the crew with a notebook that specifies how many minutes each trip in the spaceship will take.

epsfbox{p12324.eps}
Nibbler, from Futurama.
©The Curiosity Company and 20thCentury Fox.

Nibbler, Leela's stupid pet, is also a member of the crew and a critical element to succeed in the mission. Nibbler's feces, which it expels every now and then, consist of spheres of dark matter that can be used to increase the speed of the spaceship. In fact, a sphere used during a trip halves its duration. Bender's role in the mission is to pick up the heavy spheres and put them in the reactor of the spaceship. However, he will never put two spheres during the same trip: the accumulation of dark matter is extremely dangerous and may destroy the spaceship.


In each trip of the mission, Nibbler expels a certain number of dark matter spheres that can be used to decrease the duration of any of the upcoming trips. In other words, a sphere of dark matter produced during the trip $ tau_{i}^{}$ can be used to duplicate the speed of the spaceship in one of the trips $ tau_{j}^{}$, with i < j $ leq$ n.


Fry is responsible for planning how to use the spheres to reduce the total travel time. Your task is to help Fry in determining what is the minimum duration of the mission if he uses Nibbler's spheres cleverly.

Input 

The input consists of several test cases. The first line of each case contains an integer n indicating the number of trips ( 1 $ leq$ n $ leq$ 100). Then, n lines follow describing each of the trips $ tau_{1}^{}$, $ tau_{2}^{}$, ..., $ tau_{n}^{}$: each line contains two integers ti and bi separated by blanks, where ti ( 2 $ leq$ ti $ leq$ 1000) indicates the duration in minutes specified by Professor Farnsworth for the trip $ tau_{i}^{}$ (ti is always even) and bi ( 0 $ leq$ bi $ leq$ n) indicates the number of dark matter spheres that Nibbler will expel during the trip $ tau_{i}^{}$. The last test case is followed by a line containing a single `0'.

Output 

For each test case, print a line with an integer number indicating the minimum time required to complete the mission.

Sample Input 

2
24 1
10 0
2
10 1
24 0
3
10 0
24 0
38 0
3
10 1
24 0
14 0
3
10 1
24 0
38 0
3
10 1
24 1
38 0
3
10 3
24 0
38 1
0

Sample Output 

29
22
72
36
53
41
41




題目描述:


一個生物跟著科學家旅行,根據輸入列的順序進行移動,而每階段的移動這生物可以獲得暗物質,
一個暗物質,可以使得接下來的某次旅行時間減半,假設我們可以預先知道每階段的資訊。

問全程的最少時間。

題目解法:


dp[i][j] 已經走到第 i 階段,當前有 j 個暗物質 的最少時間。

#include <stdio.h>
#include <algorithm>
using namespace std;
int dp[105][105];
int main() {
    int n, t[105], b[105];
    int i, j, k;
    while(scanf("%d", &n) == 1 && n) {
        for(i = 0; i < n; i++)
            scanf("%d %d", &t[i], &b[i]);
        for(i = 0; i <= n; i++)
            for(j = 0; j <= n; j++)
                dp[i][j] = 0xfffffff;
        dp[0][0] = 0;
        for(i = 0; i < n; i++) {
            for(j = 0; j <= n; j++) {
                int bb = min(j+b[i], n);
                dp[i+1][bb] = min(dp[i+1][bb], dp[i][j]+t[i]);//not using.
                if(j)//using.
                    dp[i+1][bb-1] = min(dp[i+1][bb-1], dp[i][j]+t[i]/2);
            }
        }
        int ret = 0xfffffff;
        for(i = 0; i <= n; i++)
            ret = min(ret, dp[n][i]);
        printf("%d\n", ret);
    }
    return 0;
}

台長: Morris
人氣(2,130) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 教育學習(進修、留學、學術研究、教育概況) | 個人分類: UVA |
此分類下一篇:[UVA][dp] 702 - The Vindictive Coach
此分類上一篇:[UVA] 10536 - Game of Euler

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文