题目链接:

zxa and wifi

Time Limit: 2000/1000 MS (Java/Others)    

Memory Limit: 65536/65536 K (Java/Others)

Problem Description
 
zxa went to Q town as a volunteer, and the town mayor intended to achieve network coverage for the n families living in the town. This n families are able to be seen as the points in the axis, and the families from east to west are numbered from 1 to n, where the distance between the i-th family and the (i+1)-th family is di(1≤i<n).

zxa was in charge of the planning of this project, and he was informed that the carriers were given two ways to set up the network. One way is using one wireless router and cables associated at the i-th family for some families network coverage, where the distance from the i-th family to each covered family (include the i-th family) is no more than ri , which needs ai costs. Another way is using one optical fiber cable at the i-th family for the i-th family network coverage, which needs bi costs.

zxa is interested to know, assuming that it is only permitted to use at most k wireless routers for network coverage in order to avoid too large Wi-Fi radiation, then what is the minimum cost for this n families network coverage, can you help him?

 
Input
 
The first line contains an positive integer T, represents there are T test cases.

For each test case:

The first line contains two positive integers n and k.

The second line contains (n−1) positive integers, represent d1,d2,⋯,dn−1.

The next n lines, the i-th line contains three positive integers ai,ri and bi.

There is a blank between each integer with no other extra space in one line.

1≤T≤100,2≤n≤2⋅10^4,1≤k≤min(n,100),1≤ai,bi,di,ri≤10^5,1≤∑n≤10^5

 
Output
 
For each test case, output in one line a positive integer, repersents the minimum cost for this n families network coverage.
 
Sample Input
 
2
2 1
1
12 11 3
1 7 4
5 5
7 4 8 6
13 6 3
14 2 3
3 6 4
11 12 2
9 14 4
 
Sample Output
 
1
12
 
 
 
题意:
 
第i户与第i+1户相距d[i],第i户装光缆需钱b[i],装WiFi需要a[i],且r[i]范围内的用户都可以用,WiFi的个数不超过k,问使全都能上网的最小花费;
 
思路:
 
dp[i][j]表示装i个WiFi使的前j户可以上网的最小花费;
对于第j户可以有两种选择,装光缆dp[i][j]=min(dp[i][j],dp[i][j-1]+b[j])
装WiFi  dp[i][r[j]]=min(dp[i][r[j]],dp[i][x]+a[j])  l[i]-1<=x<=j;
还有就是先处理出第i户装WiFi时它能作用的范围[l[i],r[i]];
ans=min(ans,dp[i][n])0<=i<=k;
 
 
AC代码:
 
//#include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
const LL mod=1e9+;
const double PI=acos(-1.0);
int inf=0x3f3f3f3f;
const int N=2e4+;
int n,k,a[N],b[N],sum[N],dis[N],l[N],r[N],d[N];
int dp[][N];
int main()
{
inf*=;
int t;
scanf("%d",&t);
while(t--)
{
mst(dp,inf);
scanf("%d%d",&n,&k);
sum[]=sum[]=;
Riep(n-)scanf("%d",&d[i]),sum[i+]=sum[i]+d[i];
Riep(n)scanf("%d%d%d",&a[i],&dis[i],&b[i]);
Riep(n)
{
int L=,R=i;
while(L<=R)
{
int mid=(L+R)>>;
if(sum[i]-sum[mid]>dis[i])L=mid+;
else R=mid-;
}
l[i]=L;
L=i,R=n;
while(L<=R)
{
int mid=(L+R)>>;
if(sum[mid]-sum[i]>dis[i])R=mid-;
else L=mid+;
}
r[i]=R;
}
for(int i=;i<=k;i++)dp[i][]=;
for(int i=;i<=n;i++)dp[][i]=min(dp[][i],dp[][i-]+b[i]);
for(int i=;i<=k;i++)
{
Rjep(n)
{
dp[i][j]=min(dp[i][j],dp[i][j-]+b[j]);
for(int x=l[j]-;x<=j;x++)
{
dp[i][r[j]]=min(dp[i][r[j]],dp[i-][x]+a[j]);
}
}
}
int ans=inf;
for(int i=;i<=k;i++)
{
ans=min(ans,dp[i][n]);
}
printf("%d\n",ans);
}
return ;
}

hdu-5681 zxa and wifi(dp)的更多相关文章

  1. HDU 1003 Max Sum --- 经典DP

    HDU 1003    相关链接   HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...

  2. hdu 5094 Maze 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092176.html 题目链接:hdu 5094 Maze 状态压缩dp+广搜 使用广度优先 ...

  3. hdu 2829 Lawrence(斜率优化DP)

    题目链接:hdu 2829 Lawrence 题意: 在一条直线型的铁路上,每个站点有各自的权重num[i],每一段铁路(边)的权重(题目上说是战略价值什么的好像)是能经过这条边的所有站点的乘积之和. ...

  4. hdu 4568 Hunter 最短路+dp

    Hunter Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  5. HDU 1231.最大连续子序列-dp+位置标记

    最大连续子序列 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  6. HDU 1078 FatMouse and Cheese ( DP, DFS)

    HDU 1078 FatMouse and Cheese ( DP, DFS) 题目大意 给定一个 n * n 的矩阵, 矩阵的每个格子里都有一个值. 每次水平或垂直可以走 [1, k] 步, 从 ( ...

  7. HDOJ(HDU).1284 钱币兑换问题 (DP 完全背包)

    HDOJ(HDU).1284 钱币兑换问题 (DP 完全背包) 题意分析 裸的完全背包问题 代码总览 #include <iostream> #include <cstdio> ...

  8. HDU 6156 - Palindrome Function [ 数位DP ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛

    普通的数位DP计算回文串个数 /* HDU 6156 - Palindrome Function [ 数位DP ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛 2-36进制下回文串个数 */ ...

  9. HDU 5682 zxa and leaf 二分 树形dp

    zxa and leaf 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5682 Description zxa have an unrooted t ...

随机推荐

  1. oracle:自定义多行合并聚合函数

    原始表 COUNTRY CITY            -------------------- -------------- 中国 台北              中国 香港             ...

  2. 跟SAP系统集成的Android应用

    首先吐槽一点,这是我的第一个Android应用,很糙. 这个应用适合于上了SAP系统的企业内部使用,并且限于制造型MTO模式,需要针对生产订单报工操作的场景,因为此应用主要的一个目的,就是用来方便报工 ...

  3. erlang 查看进程相关信息

    出自: http://blog.sina.com.cn/s/blog_96b8a1540100zczz.html

  4. 3.依赖倒置原则(Dependence Inversion Principle)

    1.定义 高层模块不应该依赖于低层模块,二者都应该依赖于抽象:抽象不应该依赖细节:细节应该依赖抽象. 2.定义解读 依赖倒置原则在程序编码中经常运用,其核心思想就是面向接口编程,高层模块不应该依赖低层 ...

  5. HTTP(一) 连接管理

    ・HTTP是如何使用TCP连接的 HTTP传送一条报文时,以流的形式将报文数据内容通过一条打开的TCP连接按序传输. TCP收到数据流之后,由TCP/IP软件将数据流砍成被称作段的小数据块,并将段封装 ...

  6. 不间断图片滚动JS

    (从已经死了一次又一次终于挂掉的百度空间人工抢救出来的,发表日期 2014-05-07) MSClass是一款通用不间断滚动JS封装类,几乎支持目前所有流行风格的图片或文字滚动,横向/竖向/连续/间断 ...

  7. windows的iis做后门,隐藏访问,无日志<转>

    windows下的iis5/iis6做后门,隐藏访问,不留访问记录或者不留日志 好不容易攻下一台Windows2000/2003 IIS服务器,你一定会想,怎样才能长期占有这个“肉鸡”呢?聪明的你肯定 ...

  8. 【M33】将非尾端类设计为抽象类

    1.考虑下面的需求,软件处理动物,Cat与Dog需要特殊处理,因此,设计Cat和Dog继承Animal.Animal有copy赋值(不是虚方法),Cat和Dog也有copy赋值.考虑下面的情况: Ca ...

  9. 标准SAP中的物料类型

    DIEN -服务 ERSA -备件 FERT -成品 HALB -半成品 HAWA -贸易商品 HIBE -经营供应 LEER -虚拟件 NLAG -费存储物料 ROH -原材料 VERP -包装 W ...

  10. 如何从iTunes Connect中提款呢?

    最近在AppStore有点小小小收入,但如何从iTunes Connect中提款呢? (Payments and Financial Reports) 网上查了下,发现有种说法:“只要账号余额达到15 ...