The main effect of the topic
(LRJ "Training Guide")
There are n rubbish, the coordinates of the first I rubbish is (xi,yi), the weight is wi. There is a robot that picks up all the rubbish and throws it into the trash bin (trash bin at the origin (0,0)) in the order of numbers from small to large. A robot can pick up a few rubbish and throw it together, but at any time the total weight of the rubbish in its hands cannot exceed the maximum load C. The distance between two points is the Manhattan distance (i.e. the absolute value of the difference between the horizontal coordinate and the absolute value of the difference of ordinate). Find the shortest total distance of a robot to walk (at the beginning, the robot is in (0,0)).
"Input Format"
The number of first behavior data groups entered. The first behavior of each group of data is maximum bearing C (1≤C≤100), the second behavior positive integer n (1≤n≤100 000), that is, the quantity of rubbish, the following n rows of two nonnegative integers x, y and a positive integer w, i.e. coordinates and weights (weight guarantee not exceeding C).
"Output Format"
For each set of data, the shortest length of the total path is output.
Ideas
Method One:
Sumdis[i], representing the total distance from the 0->1->2->....->i, that is, from 0 to I
Sumweight[i], representing the total weight of the former I Garbage
Suppose to pick up (J, I) the rubbish in this interval,
W (j,i) = Sumweight[i]-sumweight[j-1]; (j,i) Total weight of garbage in the interval
The robot's walking path is: 0->j->j+1-> ->i->0, the total distance of this section is:
Routedist (j, i) = Getdis (0, J) + Sumdis[i]-sumdis[j] + getdis (0, I)
Set F (i) to indicate the shortest distance from the previous I garbage, then the state transfer
f (i) = min{f (j-1) + routedist (j, i), 1<=j<=i && sumweight[i]-sumweight[j-1] >= C}
Code A
/**========================================== * is a solution for ACM/ICPC problem * * @source: uva-1169 robotru CK * @type: DP * @author: Shuangde * @blog: blog.csdn.net/shuangde800 * @email: zengshuangde@gmail.com *================== =========================*/#include <iostream> #include <cstdio> #include <algorithm> #include <
vector> #include <queue> #include <cmath> #include <cstring> using namespace std;
typedef long long Int64;
const int INF = 0X3F3F3F3F;
const int MAXN = 100010;
int C, n;
int SUMDIS[MAXN], SUMWEIGHT[MAXN];
int F[MAXN]; struct node{int x, Y, W;}
A[MAXN];
int Getdis (int i, int j) {return abs (a[i].x-a[j].x) + ABS (A[I].Y-A[J].Y);}
int main () {int ncase; scanf ("%d", &ncase); while (ncase--) {scanf ("%d%d", &c, &n); for (int i = 1; I <= n; ++i) {scanf ("%d%d%d", &a[i].x, &a[i].y,
&A[I].W);
Sumdis[i] = sumdis[i-1] + getdis (i, i-1); Sumweight[i] = Sumweight[i-1] + a[i].w; for (int i = 1; I <= n; ++i) {f[i] = INF; for (int j = i; j >= 1;--j) {int w = sumweight[i]-sumweight[j-1]
;
if (W > C) break;
int dis = Getdis (0, J) + Sumdis[i]-sumdis[j] + getdis (0, I);
F[i] = min (f[i], F[j-1]+dis);
} printf ("%d\n", F[n]);
if (ncase) Putchar (' \ n ');
return 0; }