uva1169 - Robotruck 维护递增序列

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 integerN, not greater than 100,000, which is the number of packages to be loaded from the conveyer. Next, there areN 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


  捡垃圾的机器人要从起点出发,按1-N的顺序捡垃圾,再回到起点。可以捡几个回到起点再接着去下个位置捡,负重不能超过C,问最小共需要走多少距离。

  设d[i]为捡前i个垃圾又回到起点的最小距离,origindist[i]为起点到i垃圾的距离。d[i]=min(d[j]+origindist[j+1]+origindist[i]+j+1到i的距离)(0<=j<i,j+1到i的垃圾重量和不超过C)。

设sum[i]为0-i垃圾的重量和,于是d[i]=min(d[j]+origindist[j+1]-sum[j+1]+origindist[i]+sum[i],设f[i]=d[i]+origindist[i+1]-sum[i+1],也就是要在i之前找个f[j]最小的之间并且负重不超过C的。用一个队列,i从1循环到N,队列中的元素都是有可能成为j的。若队列的front到i负重超过C,这个元素可以删了,因为再往后的话肯定更超过C。若rear元素的f大于等于f[i],也可以删掉,因为再往后肯定是选f[i]更优(负重又小,f也小),最后把i加入队尾,维护这么一个递增数列。这样只需要O(N)的时间遍历一遍就得到d[N]。


#include<cstdio>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#define INF 0x3f3f3f3f
#define MAXN 100010
#define MAXM 1010
#define eps 1e-9
#define pi 4*atan(1.0)
#define pii pair<int,int>
using namespace std;
int T,N,C;
int x[MAXN],y[MAXN],origindist[MAXN],dist[MAXN],weight[MAXN],q[MAXN],f[MAXN],d[MAXN];
int main(){
    freopen("in.txt","r",stdin);
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&C,&N);
        int w;
        x[0]=y[0]=dist[0]=weight[0]=0;
        for(int i=1;i<=N;i++){
            scanf("%d%d%d",&x[i],&y[i],&w);
            origindist[i]=x[i]+y[i];
            dist[i]=dist[i-1]+abs(x[i]-x[i-1])+abs(y[i]-y[i-1]);
            weight[i]=weight[i-1]+w;
        }
        int front=0,rear=1;
        q[0]=f[0]=0;
        for(int i=1;i<=N;i++){
            while(front<rear&&weight[i]-weight[q[front]]>C) front++;
            d[i]=f[q[front]]+origindist[i]+dist[i];
            f[i]=d[i]+origindist[i+1]-dist[i+1];
            while(front<rear&&f[q[rear-1]]>=f[i]) rear--;
            q[rear++]=i;
        }
        printf("%d\n",d[N]);
        if(T) puts("");
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值