zoukankan      html  css  js  c++  java
  • LA3983 Robotruck

    Robotruck

     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. 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 robots maximum load capacity. The order of the input is the order of appearance in the conveyer. Output For each test case, print 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

    被格式卡了。。真cd

    total[i]表示从原点依次收集1,2,...,i的距离
    dp[i]表示从源点开始,收集1...i后回到源点最短路程
    dp[i] = min(dp[j] - total[j + 1] + list[j + 1]) + list[i] + total[i], w[i] - w[j] <= c, j <= i
    维护一个单调递增的队列

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <algorithm>
     6 #include <queue>
     7 #include <vector>
     8 #define min(a, b) ((a) < (b) ? (a) : (b))
     9 #define max(a, b) ((a) > (b) ? (a) : (b))
    10 #define abs(a) ((a) < 0 ? (-1 * (a)) : (a))
    11 inline void swap(long long &a, long long &b)
    12 {
    13     long long tmp = a;a = b;b = tmp;
    14 }
    15 inline void read(long long &x)
    16 {
    17     x = 0;char ch = getchar(), c = ch;
    18     while(ch < '0' || ch > '9') c = ch, ch = getchar();
    19     while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
    20     if(c == '-') x = -x;
    21 }
    22 
    23 const long long INF = 0x3f3f3f3f;
    24 const long long MAXN = 100000 + 10;
    25 
    26 long long t, n, c, x[MAXN], y[MAXN], w[MAXN], dp[MAXN], sumw[MAXN], total[MAXN],he,ta,q[MAXN];
    27 
    28 inline long long list(long long a)
    29 {
    30     return abs(x[a]) + abs(y[a]);
    31 }
    32 
    33 inline long long func(long long a)
    34 {
    35     return dp[a] - total[a + 1] + list(a + 1);
    36 }
    37 
    38 /*
    39 total[i]表示从原点依次收集1,2,...,i的距离 
    40 dp[i]表示从源点开始,收集1...i后回到源点最短路程
    41 dp[i] = min(dp[j] - total[j + 1] + list[j + 1]) + list[i] + total[i], w[i] - w[j] <= c, j <= i
    42 维护一个单调递增的队列 
    43 */
    44 
    45 int main()
    46 {
    47     read(t);
    48     for(;t;--t)
    49     {
    50         read(c), read(n);
    51         for(register long long i = 1;i <= n;++ i) 
    52         {
    53             read(x[i]), read(y[i]), read(w[i]);
    54             sumw[i] = sumw[i - 1] + w[i];
    55             total[i] = total[i - 1] + abs(x[i] - x[i - 1]) + abs(y[i] - y[i - 1]);
    56         }
    57         he = 0, ta = 1;
    58         q[he] = 0;
    59         for(register long long i = 1;i <= n;++ i)
    60         {
    61             while(sumw[i] - sumw[q[he]] > c && he < ta) ++ he;
    62             dp[i] = func(q[he]) + list(i) + total[i];
    63             while(func(i) < func(q[ta - 1])) -- ta;
    64             q[ta ++] = i;
    65         }
    66         printf("%lld
    ", dp[n]);
    67         if(t > 1)printf("
    ");
    68     }
    69     return 0;
    70 }
    LA3983
  • 相关阅读:
    一道打印的面试题
    Quartz使用总结
    子类和父类之间的静态代码块、静态方法、非静态代码块、构造函数之间的执行关系
    springboot使用 @EnableScheduling、@Scheduled开启定时任务
    springboot的Interceptor、Filter、Listener及注册
    ConcurrentHashMap 的工作原理及代码实现
    为什么Hashtable ConcurrentHashmap不支持key或者value为null
    Android 通过Java代码生成创建界面。动态生成View,动态设置View属性。addRules详解
    Android 自定义title 之Action Bar
    Android常用控件之GridView与ExpandableListView的用法
  • 原文地址:https://www.cnblogs.com/huibixiaoxing/p/8301681.html
Copyright © 2011-2022 走看看