UVAlive 3983 Robotruck

题目:

 
Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

Submit Status

Description

 

Problem C - Robotruck

Background

This problem is about a robotic truck that distributes mail packages to several locations in a factory. The robot sits at the end of a conveyer at the mail office and waits for packages to be loaded into its cargo area. The robot has a maximum load capacity, which means that it may have to perform several round trips to complete its task. Provided that the maximum capacity is not exceeded, the robot can stop the conveyer at any time and start a round trip distributing the already collected packages. The packages must be delivered in the incoming order.

The distance of a round trip is computed in a grid by measuring the number of robot moves from the mail office, at location (0,0), to the location of delivery of the first package, the number of moves between package delivery locations, until the last package, and then the number of moves from the last location back to the mail office. The robot moves a cell at a time either horizontally or vertically in the factory plant grid. For example, consider four packages, to be delivered at the locations (1,2), (1,0), (3,1), and (3,1). By dividing these packages into two round trips of two packages each, the number of moves in the first trip is 3+2+1=6, and 4+0+4=8 in the second trip. Notice that the two last packages are delivered at the same location and thus the number of moves between them is 0.

Problem

Given a sequence of packages, compute the minimum distance the robot must travel to deliver all packages.

Input

Input consists of multiple test cases the first line of the input contains the number of test cases. There is a blank line before each dataset. The input for each dataset consists of a line containing one positive integer C, not greater then 100, indicating the maximum capacity of the robot, a line containing one positive integer N, not greater than 100,000, which is the number of packages to be loaded from the conveyer. Next, there are N lines containing, for each package, two non-negative integers to indicate its delivery location in the grid, and a positive integer to indicate its weight. The weight of the packages is always smaller than the robot�s maximum load capacity. The order of the input is the order of appearance in the conveyer.

Output

One line containing one integer representing the minimum number of moves the robot must travel to deliver all the packages. Print a blank line between datasets.

Sample Input

1

10

4

1 2 3

1 0 3

3 1 4

3 1 4

Sample Output

14

-------------------------------------------------------------------------------------------------------------------------------------------------------------------

思路:

  设d[i]为将前i个垃圾收完并放进垃圾桶的最小距离。同时定义total_dist[i]为从0点依次走过前i个垃圾到i的曼哈顿距离和,定义dist2origin[i]为i到0点的曼哈顿距离。

转移方程:

d[i]=min{d[j]-total_dist(j+1)+dist2origin(j+1)  // j 满足w(j+1,i)<=c } +total_dist[i]+dist2origin[i]   //代表一次将j+1..i的垃圾收回放到0点

单调队列优化:

设func(j)=d[j]-total_dist(j+1)+dist2origin(j+1) 则转移方程为:d[i]=min{func(j)  // j 满足w(j+1,i)<=c } + ...

用单调队列维护一个满足w(q[front]+1,i)<=c的滑动窗口,单调队列中的值按照func的递增序排列。每次考虑一个新的i 都要适当移动窗口,使得满足单调队列的性质。维护的单调队列可以在O(1)的时间内返回min{func(j)  // j 满足w(j+1,i)<=c}

具体操作如下:

         int q[maxn];  //单调队列
front=rear=;
for(int i=;i<=n;i++) {
while(front<=rear && total_w[i]-total_w[q[front]]>c) front++; //维护队列中w(j+1,i)<=c
d[i]=func(q[front]) + total_dist[i] + dist2origin[i]; //func(q[front])是队列中的最小值
while(front<=rear && func(i)<=func(q[rear])) rear--; //维护单调队列中的单调递增序
q[++rear]=i;
}

完整代码:

 #include<iostream>
#include<algorithm>
using namespace std; const int maxn = + ; int x[maxn],y[maxn];
int total_dist[maxn],dist2origin[maxn],total_w[maxn];
int d[maxn]; inline int func(int j) {
return d[j]-total_dist[j+]+dist2origin[j+];
} int main() {
int T,n,c,w,front,rear;
cin>>T;
while(T--) {
cin>>c>>n;
total_w[]=total_dist[]=x[]=y[]=;
for(int i=;i<=n;i++) {
cin>>x[i]>>y[i]>>w;
total_w[i]=total_w[i-]+w;
total_dist[i]=total_dist[i-]+abs(x[i]-x[i-])+abs(y[i]-y[i-]);
dist2origin[i]=abs(x[i])+abs(y[i]);
} int q[maxn];
front=rear=;
for(int i=;i<=n;i++) {
while(front<=rear && total_w[i]-total_w[q[front]]>c) front++;
d[i]=func(q[front]) + total_dist[i] + dist2origin[i];
while(front<=rear && func(i)<=func(q[rear])) rear--;
q[++rear]=i;
} cout<<d[n]<<endl;
if(T) cout<<endl;
}
}

【暑假】[深入动态规划]UVAlive 3983 Robotruck的更多相关文章

  1. [UVALive 3983] Robotruck

    图片加载可能有点慢,请跳过题面先看题解,谢谢 设状态 \(f[i][j]\) 为,当前垃圾序号为 \(i\) ,当前承重为 \(j\) 的最小路程,好的这道题做完了 O(NC) G烂 $ $ 我们这样 ...

  2. UVALive 3983 Robotruck (单调队列,dp)

    如果状态定义为序号和重量的话,决策就是下一个垃圾捡或者不减,但是状态数太多了. 如果只定义序号作为状态的话,决策就变成从前面的某个j一直捡到i才送回垃圾. 这就变成了一个区间选最小值的问题,用单调队列 ...

  3. UVaLive 3983 Robotruck (DP + 单调队列)

    题意:有n个垃圾,第i个垃圾坐标为(xi,yi),重量为wi,有一个机器人,要按照编号从小到大的顺序剑气所有的垃圾兵扔进垃圾桶,垃圾桶在原点, 每次总重量不能超过C,两点间距离为曼哈顿距离,求出最短的 ...

  4. 【暑假】[深入动态规划]UVAlive 4794 Sharing Chocolate

    UVAlive 4794 Sharing Chocolate 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=12055 ...

  5. LA 3983 Robotruck

    这道题感觉挺吃力的,还用到了我不熟悉的优先队列 题目中的推导也都看明白了,总之以后还要多体会才是 这里用优先对列的原因就是因为要维护一个滑动区间的最小值,比如在区间里2在1的前面,2在离开这个滑动区间 ...

  6. UVA LA 3983 - Robotruck DP,优先队列 难度: 2

    题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  7. 2015暑假训练(UVALive 5983 - 5992)线段树离线处理+dp

    A: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=83690#problem/A 题意:N*M的格子,从左上走到右下,要求在每个点的权值 ...

  8. UVALive 3983 捡垃圾的机器人 DP

    这个题目我最初的做法沿用树形DP的做法,设置一个 dp[i][0]表示机器人在i点不回去的最短路径,dp[i][1]表示机器人在i点回去的最短路径,规划方向为i-1向i转移,结果发现这个不能用树形的结 ...

  9. 大白第一章第四节dp例题

    入口 UVALive - 3882 #include<cstdio> using namespace std; ; int n,m,k,f[N]; int main(){ //f[i]表示 ...

随机推荐

  1. IE 浏览器下 button元素自动触发click?

    为什么这么说呢?且看下面的情形: 当用户焦点在输入框中的时候,当用户按了“Enter”键之后,达到点击登录的效果:所以这里就监听了input的keydown事件,当keyCode是13的时候,就发送请 ...

  2. HDU 3339 In Action 最短路+01背包

    题目链接: 题目 In Action Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...

  3. ***总结:在linux下连接redis并进行命令行操作(设置redis密码)

    [root@iZ254lfyd6nZ ~]# cd / [root@iZ254lfyd6nZ /]# ls bin boot dev etc home lib lib64 lost+found med ...

  4. Linux---Ls命令 初级实现

    By xxx0624Done:    ls    ls -a    ls -l    ls /tmp    ls -R    ls -t    FileName color    FileName o ...

  5. [itint5]合并K个有序链表

    merge sort,leet code里面曾经做过.但一开始没这么写,遍历来做,效率n*k了,用了merge sort后,变成logn*k. 用了dummy node.同时要注意size为0的情况. ...

  6. jquery.dataTables插件使用例子详解

    DataTables是一个jQuery的表格插件.这是一个高度灵活的工具,依据的基础逐步增强,这将增加先进的互动控制,支持任何HTML表格 效果图 代码 <!doctype html> & ...

  7. sizeof运算符来获取各种数据类型在内存中所占字节数--gyy整理

    C++并没有规定各种数据类型在内存中的存储大小,依赖于不同的编译器的不同而不同,要想获知当前编译器对各种数据类型分配的大小,可以通过sizeof运算符来获取. 使用方法1: sizeof(数据类型) ...

  8. linux page cache和buffer cache

    主要区别是,buffer cache缓存元信息,page cache缓存文件数据 buffer 与 cache 是作为磁盘文件缓存(磁盘高速缓存disk cache)来使用,主要目的提高文件系统系性能 ...

  9. TCP/IP的三次握手和四次分手以及超时机制

    使用INADDR_ANY的时候,往往针对多网卡情况,采用tcp连接方式,需要选择使用哪一个网卡发送,自己猜想应该是使用三次握手机制,如何判断目标地址不可达,应该使用的是超时机制,即握手超时则不可到达. ...

  10. C3p0/元数据/内省-Bean/自定义查询封装类/查询/分页

    c3p0连接池(C3p0连接池,只有当用户获取连接时,才会包装Connection.) 第一步:导入c3p0 第二步:在classpath目录下,创建一个c3p0-config.xml 第三步:创建工 ...