C. Gas Pipeline DP
2 seconds
256 megabytes
standard input
standard output
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment [0,n][0,n] on OXOX axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval (x,x+1)(x,x+1) with integer xx. So we can represent the road as a binary string consisting of nn characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.
Usually, we can install the pipeline along the road on height of 11 unit with supporting pillars in each integer point (so, if we are responsible for [0,n][0,n] road, we must install n+1n+1 pillars). But on crossroads we should lift the pipeline up to the height 22, so the pipeline won't obstruct the way for cars.
We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment [x,x+1][x,x+1] with integer xx consisting of three parts: 0.50.5 units of horizontal pipe + 11 unit of vertical pipe + 0.50.5 of horizontal. Note that if pipeline is currently on height 22, the pillars that support it should also have length equal to 22 units.
Each unit of gas pipeline costs us aa bourles, and each unit of pillar — bb bourles. So, it's not always optimal to make the whole pipeline on the height 22. Find the shape of the pipeline with minimum possible cost and calculate that cost.
Note that you must start and finish the pipeline on height 11 and, also, it's guaranteed that the first and last characters of the input string are equal to 0.
The fist line contains one integer TT (1≤T≤1001≤T≤100) — the number of queries. Next 2⋅T2⋅T lines contain independent queries — one query per two lines.
The first line contains three integers nn, aa, bb (2≤n≤2⋅1052≤n≤2⋅105, 1≤a≤1081≤a≤108, 1≤b≤1081≤b≤108) — the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.
The second line contains binary string ss (|s|=n|s|=n, si∈{0,1}si∈{0,1}, s1=sn=0s1=sn=0) — the description of the road.
It's guaranteed that the total length of all strings ss doesn't exceed 2⋅1052⋅105.
Print TT integers — one per query. For each query print the minimum possible cost of the constructed pipeline.
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
94
25
2900000000
13
The optimal pipeline for the first query is shown at the picture above.
The optimal pipeline for the second query is pictured below:
The optimal (and the only possible) pipeline for the third query is shown below:
The optimal pipeline for the fourth query is shown below:
题意:管道工人需要在一段凹凸不平的道路上铺管子,给一串长度为n的01序列描述地形,1表示一定要把水管用钢管架高在离地面2个单位,0表示水管离地面高度可以为1,也可以为2,
在转折的地方需要额外花费1单位的水管,给定水管和钢管的价格a,b;问最少需要花费多少钱
#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
#include<math.h>
#include<string>
#include<string.h>
#include<vector>
#include<utility>
#include<map>
#include<queue>
#include<set>
#include<stack>
#define mx 0x3f3f3f3f3f3f3f
#define ll long long
#define MAXN 100
using namespace std;
ll dp[][];
char s[];
ll n,a,b,t;
int main()
{
cin>>t;
while(t--)
{
cin>>n>>a>>b;
scanf("%s",s+);
for(int i=;i<=n;i++)//初始化dp
{
dp[i][]=mx;
dp[i][]=mx;
}
dp[][]=b;
dp[][]=mx;
for(int i=;i<=n;i++)
{
if(s[i]=='')//高柱子的右边一定是长度为2的管子
dp[i][]=min(dp[i][],dp[i-][]+a+b*);
else//低柱子的右边可能是长度为1的,也可能是长度为2的管子,两者取更小的花费
{
dp[i][]=min(dp[i][],min(dp[i-][]+a+b,dp[i-][]+*a+b));
dp[i][]=min(dp[i][],min(dp[i-][]+*a+*b,dp[i-][]+a+b*));
}
}
cout<<dp[n][]<<endl;
}
return ;
}
C. Gas Pipeline DP的更多相关文章
- [贪心,dp] Educational Codeforces Round 71 (Rated for Div. 2) C. Gas Pipeline (1207C)
题目:http://codeforces.com/contest/1207/problem/C C. Gas Pipeline time limit per test 2 seconds memo ...
- Codeforces 1207C Gas Pipeline (dp)
题目链接:http://codeforces.com/problemset/problem/1207/C 题目大意是给一条道路修管道,相隔一个单位的管道有两个柱子支撑,管道柱子高度可以是1可以是2,道 ...
- Educational Codeforces Round 71
https://www.cnblogs.com/31415926535x/p/11460682.html 上午没课,做一套题,,练一下手感和思维,,教育场的71 ,,前两到没啥,,后面就做的磕磕巴巴的 ...
- 第9周cf刷题(dp)
Problems(1300-1600) an ac a day keeps the doctor away Gas Pipeline (1500) 2019.10.28 题意: 管道工人需要在一段凹凸 ...
- Educational Codeforces Round 71 (Rated for Div. 2)
传送门 A.There Are Two Types Of Burgers 签到. B.Square Filling 签到 C.Gas Pipeline 每个位置只有"高.低"两种状 ...
- Educational Codeforces Round 71 (Rated for Div. 2) Solution
A. There Are Two Types Of Burgers 题意: 给一些面包,鸡肉,牛肉,你可以做成鸡肉汉堡或者牛肉汉堡并卖掉 一个鸡肉汉堡需要两个面包和一个鸡肉,牛肉汉堡需要两个面包和一个 ...
- CF Edu Round 71
CF Edu Round 71 A There Are Two Types Of Burgers 贪心随便模拟一下 #include<iostream> #include<algor ...
- PAT甲级考前整理(2019年3月备考)之一
转载请注明出处:https://www.cnblogs.com/jlyg/p/7525244.html 终于在考前,刷完PAT甲级131道题目,不容易!!!每天沉迷在刷题之中而不能超脱,也是一种 ...
- 一份程序猿单词列表(updating)
以下单词是个人平时阅读英文文档时遇到的一些“生”单词,该文档将持续更新,可以持续关注https://github.com/hylinux1024/word-list-for-programmer hi ...
随机推荐
- 7 scrapy 初识
scrapy框架 框架介绍: Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架,非常出名,非常强悍.所谓的框架就是一个已经被集成了各种功能(高性能异步下载,队列,分布式,解析,持久化 ...
- VScode Vim插件快速上手与配置
快速安装: 打开market place,搜索vim即可安装. 基本改建与配置说明: 方向键: 用I k j l表示光标上下左右,需要编辑visual mode和normal mode { " ...
- Leader:这样的 Bug 你也写的出来???
Hello~各位读者新年好!不知道大家春节假期是否已延长,小黑哥刚接到通知,假期延长到 2 月 2 号,另外回去之后需要在家办公,自行隔离两周.还没试过在家办公,小黑哥就怕到时候生物钟还没调整过来,一 ...
- java并发基础知识
这几天全国都是关键时候,放假了,还是要学习啊!很久没有写博客了,最近看了一本书,有关于java并发编程的,书名叫做“java并发编程之美”,讲的很有意思,这里就做一个笔记吧! 有需要openjdk8源 ...
- 聊聊面试中常问的GC机制
GC 中文直译垃圾回收,是一种回收内存空间避免内存泄漏的机制.当 JVM 内存紧张,通过执行 GC 有效回收内存,转而分配给新对象从而实现内存的再利用. JVM GC 机制虽然无需开发主动参与,减轻不 ...
- SqlHelper类编写前奏:DataReader关闭链接出现问题
SqlHelper是一个执行数据库操作的助手类,但是当我们没学过DataSet之前,要想使用using搭配SqlConnection和SqlCommand写出一个真正独立的SqlHelper都是不太可 ...
- mysql5.7修改root密码
use mysql; update mysql.user set authentication_string=password('123456') where user='root'; flush p ...
- 洛谷P1073最优贸易(跑两遍dij)
题目描述 CC C国有n n n个大城市和m mm 条道路,每条道路连接这 nnn个城市中的某两个城市.任意两个城市之间最多只有一条道路直接相连.这 mmm 条道路中有一部分为单向通行的道路,一部分为 ...
- 【Fine学习笔记】python 文件l操作方法整理
python脚本可以对excel进行创建.读.写.保存成指定文件名,保存到指定路径的操作.整理了以下处理方法: 首先区别几个操作方式: "r" 以读方式打开,只能读文件 , 如 ...
- Android Studio中 安卓模拟器 联网
方案一:之前做测试的时候发现虚拟机无法联网(浏览器打开 www.baidu.com, 显示 403 forbidden): 解决方案:关闭已经打开的虚拟机,进入到 SDK 安装目录下,按住 shift ...