---
id: 101
lang: en
translation_of: index.md
source_sha256: 7d221c76eb5202af8e14071c88ed0dbf726f66b823f38fa9c7bf23b3275f1127
title: "P1594: Convoy — Solution"
description: "One look at this problem and you know it is a DP that partitions the vehicles."
tags: ["OI","DP"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "护卫队 (problem name) rendered as “Convoy”."
---

## Approach

One look at this problem and you know it is a DP that partitions the vehicles. At first I thought of F[first i vehicles][number of partitions j] = the time for the first i vehicles split j times, then found this formulation is very slow; besides, this problem does not say how many partitions there are, and we only need the solution for the first n vehicles.

Transition equation: F[i] = F[j-1] + L / (lowest_spd in [1,j]), and the transition is allowed only when there is no overload.

This problem is pretty <span class="slang" data-zh="毒瘤" data-gloss="dúliú, “tumor”. A problem (or problem setter) that is painfully hard, tedious, or full of traps.">nasty</span>: int will <span class="slang" data-zh="被卡" data-gloss="bèi kǎ, “get stuck”. OI slang for a solution being made to fail by test data built against it.">get caught out</span>, and the infinity for double has to be a <span class="slang" data-zh="真·无穷大" data-gloss="zhēn · wúqióngdà, “True · infinity”. The 真· prefix mimics game titles like 真·三国无双 (Dynasty Warriors); a jokey way to stress the real thing.">True·Infinity</span>.

## Code

```cpp
// P1594.cpp
#include <iostream>
#include <iomanip>

using namespace std;

#define ll long long
const double INF = 9223372036854775807ll;
const ll maxn = 1100;
struct car
{
    double v, w;
} cars[maxn];
double W, L, N;
double F[maxn];

void DP()
{
    F[0] = 0;
    for (ll i = 1; i <= N; i++)
    {
        F[i] = (double)INF;
        double load = 0;
        double lspd = INF;
        for (ll j = i; j > 0; j--)
        {
            load += cars[j].w;
            if (load > W)
                break;
            lspd = min(lspd, cars[j].v);
            F[i] = min(F[i], F[j - 1] + L / lspd);
        }
    }
}

int main()
{
    cin >> W >> L >> N;
    for (ll i = 1; i <= N; i++)
        cin >> cars[i].w >> cars[i].v;
    DP();
    cout << fixed << setprecision(1) << F[(ll)N] * 60;
    return 0;
}
```
