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

#include<iostream>
#include<stdio.h>
#include<vector>
#include<utility>
#include<cmath>
#include<algorithm>using namespace std;double minTime(vector<pair<int, double>>&con, int, int);int main() {int b, l, n;cin >> b >> l >> n;while (b || l || n) {vector<pair<int, double>> convoy;convoy.push_back(make_pair(0, 0.0));for (int i = 0; i < n; ++i) {int w_temp, s_temp;cin >> w_temp >> s_temp;double t_temp = (double)l / s_temp;t_temp *= (double)60;convoy.push_back(make_pair(w_temp, t_temp));}double res = minTime(convoy, b, n);printf("%.1f\n", res);convoy.clear();cin >> b >> l >> n;}
}double minTime(vector<pair<int, double>>& con, int ws, int n)
{vector<double> dp(n+1);dp[0] = 0;dp[1] = con[1].second;for (int i = 2; i <= n; ++i) {double sim = dp[i - 1] + con[i].second;vector<double> compareList;compareList.push_back(sim);int sum = con[i].first;double time = con[i].second;int index1 = i - 1;int index2 = i - 2;while (index2 >= 0) {sum += con[index1].first;if (sum > ws) break;time = max(time, con[index1].second);double res = time + dp[index2];--index1;--index2;compareList.push_back(res);}sort(compareList.begin(), compareList.end());dp[i] = compareList.front();}return dp[n];
}

【算法】To Europe! To Europe!-动态规划相关推荐

  1. 数据结构与算法之暴力递归改动态规划

    数据结构与算法之暴力递归改动态规划 目录 二维数组最小路径和 暴力递归改动态规划解析 任意选择数组arr中的数字,看能不能累加得到aim 1. 二维数组最小路径和 (一) 题目描述 (二) 思路 递归 ...

  2. 算法复习第四章动态规划

    算法复习第四章动态规划 动态规划 TSP问题 0-1bag 动态规划 TSP问题 0-1bag 最长公共子序列不考:

  3. AcWing基础算法课Level-2 第五讲 动态规划

    AcWing基础算法课Level-2 第五讲 动态规划 背包问题 AcWing 2. 01背包问题3018人打卡 AcWing 3. 完全背包问题2749人打卡 AcWing 4. 多重背包问题255 ...

  4. random_state的值如何选_算法萌新如何学好动态规划(3)

    本文是「动态规划」系列文章的第三篇,作为 算法萌新如何学好动态规划(2) 的一个延伸.本篇文章将主要聚焦于动态规划经典模型 -- 背包问题的讲解. 背包问题属于线性 DP 模型,之所以单独拎出来讲,主 ...

  5. 算法练习(7) —— 动态规划 Strange Printer

    算法练习(7) -- 动态规划 Strange Printer 动态规划 动态规划算法通常处理的是多阶段的决策最优化问题.挺多的问题都含有递推的思想.做这样的问题,最重要的就是找到对应的状态转移方程. ...

  6. 算法 64式 8、动态规划算法整理_第1部分_1到15题

    1 算法思想 动态规划 1.1含义 把问题分解成多阶段或多个子问题,顺序求解各个子问题,最后一个子问题就是初始问题的解. 概念 阶段: 问题分成的顺序的几个环节.例如最长递增子序列中每个字符就是一个阶 ...

  7. 算法设计与分析:动态规划(3)-序列联配问题(以算代存)

    文章目录 前言 高级动态规划 应用分治思想减少空间 计算得分 从后缀匹配到前缀匹配 伪代码 分治点计算改进 总结 本文参考UCAS卜东波老师算法设计与分析课程撰写 前言 本文内容承接上一次算法设计与分 ...

  8. _42LeetCode代码随想录算法训练营第四十二天-动态规划 | 121.买卖股票的最佳时机、122.买卖股票的最佳时机II

    _42LeetCode代码随想录算法训练营第四十二天-动态规划 | 121.买卖股票的最佳时机.122.买卖股票的最佳时机II 题目列表 121.买卖股票的最佳时机 122.买卖股票的最佳时机II 1 ...

  9. 算法 64式 8、动态规划算法整理

    1 算法思想 动态规划 1.1含义 把问题分解成多阶段或多个子问题,顺序求解各个子问题,最后一个子问题就是初始问题的解. 概念 阶段: 问题分成的顺序的几个环节.例如最长递增子序列中每个字符就是一个阶 ...

最新文章

  1. 某程序员吐槽:面试八股文害死人!公司新来的应届生满口框架,根本不会写代码!网友:八股文只能招到背题家!...
  2. bzoj 2179 FFT快速傅立叶
  3. JavaWeb:上传下载文件
  4. 使用Python增加csdn的访问量
  5. boost::mp11::mp_from_sequence相关用法的测试程序
  6. cisco 防火墙模拟器_【方案分享】如何解决cisco设备无法进入系统问题?
  7. .NET Core 3.0 可卸载程序集原理简析
  8. python封装工具类多个项目使用_【arcpy项目实战】将多个点两两生成的最短路径pyhon代码封装入script中...
  9. uni-app获取腾讯地图计算两经纬度的实际距离(可批量)
  10. (2) pandas 文件读写 (csv)
  11. [010]Try块和异常处理
  12. Java 单向链表翻转
  13. 网站克隆工具_Kali Linux工具篇十三:网站克隆技巧Httrack使用技巧
  14. UltraISO/Nero/Daemon Tools
  15. 创建对象的几种常用写法
  16. 西南大学计算机学院导师,西南大学计算机与信息科学学院研究生导师简介-胡小方...
  17. 留学目的地选择之内华达州
  18. led灯闪烁代码_如何设置LED灯并使其通过代码闪烁
  19. Java 操作 word 文档 (二)初识 WordprocessingML 标签
  20. 从新生儿性别比例数据,看各地重男轻女程度高低

热门文章

  1. Python在自然语言处理领域的应用 Natural Language Processing With Python: Analyzing Text
  2. CSS 水平居中设置
  3. 专业计算机限制日语吗,如果高考选日语,大学选专业有什么限制?
  4. 有个厉害的程序员老婆是什么体验?,最新JAVA面试合集
  5. ctfshow-misc49
  6. DA转换器芯片DP4361 PIN2PIN CS4361
  7. 医学影像中呼吸门控的动态显示
  8. 文件扩展名,你知道这些吗?(续)
  9. 如何监听页面 DOM 变动并高效响应
  10. 切换浅色和暗夜模式的vue组件 darkmode-switch-btn