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. JSon数据查询---Jlinq
  2. 【Linux开发】V4L2应用程序框架
  3. python 程序1【登录接口】
  4. linux 中卸载提示设备正忙怎么办?
  5. [html] 如何使用html5进行图片压缩上传?
  6. HTML网页使用CDN的jquery.qrcode.min.js生成页面二维码(直接可以复制使用)
  7. pycharm设置中文
  8. Javascript自动登录B/S系统的简单实现
  9. Fetion2008 分析 Part1:准备工作
  10. 把Windows Phone应用发布到中国
  11. html css字体最小,html-字体大小CSS问题
  12. 金蝶服务器显示已离线,金蝶K3提示云服务器已离线
  13. ESP-AT 应用: AT+MQTT 对接腾讯 QCloud 云
  14. IDEA光标变成白色粗条的问题
  15. linux车机按键学习,linux就该这么学
  16. 物联网卡企业的选择应该怎么避雷
  17. 西南大学计算机学院推免,2019年西南大学计算机与信息科学学院硕士研究生拟录取名单的公示(不含推免生)...
  18. Hadoop 入门总结
  19. 如何用简单的方式将数组转成json
  20. Node-RED教程(一):Node-RED的介绍与安装

热门文章

  1. 六个网络安全专业人员就有一个年薪十万英镑
  2. 用于光学神经网络的高速光学卷积加速器
  3. java.lang.NoSuchMethodException: com.wwy.missionallowance.entities.Level.<init>()问题解决
  4. LMT LicManager系统对许可证(license)管理创新中的新附加值
  5. 国家计算机一级字处理怎么过?
  6. 使用 xlsx 下载 xlsx 文件
  7. JAVA基础水平考试
  8. Talend 使用thttpRequest请求json数据,并使用tExtractJSONFields解析数据
  9. 使用C4D制作iPhone模型:第2部分
  10. 请概述计算机技术的发展是现代核磁共振光谱法得以应用的关键,仪器分析复习题...