1. 程式人生 > >POJ:To Europe! To Europe!

POJ:To Europe! To Europe!

總時間限制: 
1000ms
 
記憶體限制: 
65536kB
描述
Almost everyone in the candidate states wants to `go to Europe'', although most of the people have very vague ideas about what this actually means. Anyway, immediately after the borders are open, the inhabitants will take their cars and trucks and will `go to Europe''. This can cause many troubles, as the roads will be suddenly overloaded by vehicles of various types. You are to help to solve some of these traffic jams. 

Assume a convoy of vehicles has lined up in front of a single lane and one-way bridge over a river. Note that since the street is single lane, no vehicle can overtake any other. The bridge can sustain a given maximum load. To control the traffic on the bridge, operators are stationed on either end of the bridge. The convoy of vehicles is to be divided into groups, such that all the vehicles in any group can cross the bridge together. When a group reaches the other side, the operator on that side of the bridge uses a telephone to inform the operator on this side that the next group can start its journey over the bridge. 

The weight of each vehicle is known. The sum of the weights of the vehicles in any group cannot exceed the maximum load sustainable by the bridge. Associated with each vehicle is the maximum speed with which it can travel over the bridge. The time taken by a group of vehicles is calculated as the time taken by the slowest vehicle in the group to cross the bridge. The problem is to find the minimum amount of time in which the entire convoy can cross the bridge. 
輸入
The input consists of several test cases. The first line of each test case contains three positive integers (separated by blanks): the first one represents the maximum load that the bridge can sustain b (in tonnes); the second one represents the length of the bridge l (in kms); and the third one is the number of vehicles (n) in the convoy. 

Each of the next n lines of input contains a pair of positive integers, wi and si (separated by blanks), where wi is the weight of the vehicle (in tonnes) and si is the maximum speed (in kmph) with which this vehicle can travel over the bridge. The weights and speeds of the vehicles are specified in the same order as the order in which the vehicles are queued up. You can assume that 1 <= n,b,l,s <= 1000 and any i in [1..n]: wi <= b. 

After the last vehicle, the next test case description begins. The last test case is followed by a line containing three zeros.
輸出
The output of the program should be a single real number specifying the minimum time in minutes in which the convoy can cross the bridge. The number should be displayed with one digit after the decimal point.
樣例輸入
100 5 10
40 25
50 20
50 20
70 10
12 50
9 70
49 30
38 25
27 50
19 70
0 0 0
樣例輸出
75.0


動歸,一維,dp[i],表示第i組資料之前最優值,然後從i-1,i-2,一直到i-k其中i和k之間合不大於b.在其中求最優值。


#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<cmath>
#include<algorithm>
const int INF = 0x3f3f3f3f;
using namespace std;
double mind(double a, double b){
    return a < b ? a : b;
}

int main()
{
    int b,le,n;
    int wi[1100], si[1100];
    double dp[1100];
    while (scanf("%d %d %d", &b, &le, &n) && b != 0 && le != 0 && n != 0){
        for (int i = 1; i <= n; i++){
            scanf("%d %d", &wi[i], &si[i]);
        }
        dp[0] = 0;
        for (int i = 1; i <= n; i++){
            int sum = 0,mins=INF;
            dp[i] = INF;
            for (int k = i; k > 0; k--){
                sum += wi[k];
                mins = min(mins, si[k]);
                if (sum <= b){
                    dp[i] = mind(dp[i], dp[k - 1] + le*1.0 / mins);
                }
                else break;
            }
        }

        printf("%.1lf\n", dp[n] * 60);



    }

    return 0;
}