ACM: 动态规划题 LA 3983

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

 

题意: 现在又一个机器人捡垃圾, 机器人的最大容量是C, 有n个垃圾, 要求顺序的捡取, 每个垃圾的坐标

      和w体积已知, 垃圾桶的位置在(0,0)上, 求出机器人将全部垃圾放入垃圾桶的最短行走路径.

      (机器人在行列中行走的距离是|xi-xj|+|yi-yj|);

 

解题思路:

      1. 要我顺序捡取垃圾, 这个提示设状态dp[i]: 表示机器人捡前i个垃圾需要的最短行走路径,并且

         表示机器人在(0,0)位置上.继续假设: total_dist[i]表示从第1个点结果2,3,...到第i个点的

         总距离. dist[i]表示(0,0)到第i个点的距离.

         状态方程: dp[i] = min( dp[j]+dist[j+1]+(total_dist[i]-total_dist[j+1])+dist[i] );

         改变的值只有j, 可以变成:

                   dp[i] = min(dp[j]+dist[j+1]-total_dist[j+1])+dist[i]+total_dist[i];

      2. 因为机器人的容量有限制C, min(dp[j]+dist[j+1]-total_dist[j+1])的j会有一系列的点集,随着

         i的增大, 点集j会随着变化, 相当于一个范围, 当范围超出C时, 就要把左边的点删除. 这里跟

         线段树扫描区间相识.

 

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
#define MAX 100005

int n, C;
int dp[MAX];
int total_dist[MAX], dist[MAX], w[MAX];
int x[MAX], y[MAX];
int qu[MAX];

inline int getFunc(int i)
{
 return dp[i]+dist[i+1]-total_dist[i+1];
}

int main()
{
// freopen("input.txt", "r", stdin);
 int caseNum, i;
 scanf("%d", &caseNum);
 while(caseNum--)
 {
  scanf("%d %d", &C, &n);
  total_dist[0] = w[0] = x[0] = y[0] = 0;
  int temp;
  for(i = 1; i <= n; ++i)
  {
   scanf("%d %d %d", &x[i], &y[i], &temp);
   total_dist[i] = total_dist[i-1]+abs(x[i]-x[i-1])+abs(y[i]-y[i-1]);
   dist[i] = abs(x[i])+abs(y[i]);
   w[i] = w[i-1]+temp;
  }

  int front = 1, rear = 1;
  memset(qu, 0, sizeof(qu));
  for(i = 1; i <= n; ++i)
  {
   while(front <= rear && w[i]-w[qu[front]] > C)
    front++;
   dp[i] = getFunc(qu[front])+total_dist[i]+dist[i];
   while(front <= rear && getFunc(qu[rear]) >= getFunc(i))
    rear--;
   qu[++rear] = i;
  }

  printf("%d\n", dp[n]);
  if(caseNum) printf("\n");
 }
 return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值