Hdu 4362 Dragon Ball
Dragon BallTime Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 2204 Accepted Submission (s): 770
Problem DescriptionSean has got a Treasure map which shows when and where the dragon bils will appear. some dragon bils will appear in a line at the same time for each period. since the time you got one of them, the other dragon ball will disappear so he can only and must get one Dragon ball in each period. digging out one ball he will lose some energy. sean will lose | x-y | energy when he move from x to y. suppose Sean has enough time to get any drogan ball he want in each period. we want to know the minimum energy sean will lose to get all period's dragon ball.
InputIn the first line a number T indicate the number of test cases. then for each case the first line contain 3 numbers m, n, x (1 <= m <= 50, 1 <= n <= 1000), indicate m period Dragon ball will appear, n dragon bils for every period, x is the initial location of sean. then two m * n matrix. for the first matrix, the number in I row and J column indicate the location of J-th Dragon ball in I th period. for the second matrix the number in I row and J column indicate the energy sean will lose for J-th Dragon ball in I-th period.
OutputFor each case print a number means the minimum energy sean will lose.
Sample Input
13 2 52 34 11 31 11 34 2
Sample Output
8
AuthorFZU
Source2012 Multi-University Training Contest 7
Question:
M cycles, with n dragons in each cycle, starting at the position x, the energy consumed from the x position to the y position is the absolute value of the difference between the two + the energy value to be absorbed by the Dragon Ball in the y position. Each cycle must be in the position of a Dragon Ball.
Question:
It is easy to think of the Dynamic Programming equation a [I] [k]. dp = min (a [I] [k]. dp, (a [I-1] [j]. dp + | a [I] [k]. x-a [I-1] [j]. x | + a [I] [k]. (v), so that it will time out to directly use the complex degree O (m * n). optimization is required to remove the absolute value. There are two situations:
1. a [I] [k]. x> = a [I-1] [j]. x (the position of the First Periodic Dragon Ball is smaller than that of the next periodic Dragon Ball)
A [I-1] [j]. dp-a [I-1] [j]. x + a [I] [k]. x + a [I] [k]. v, Set mini = a [I-1] [j]. dp-a [I-1] [j]. x now a [I] [k]. dp = mini + a [I] [k]. x + a [I] [k]. v pairs a [I] [k]. dp, a [I] [k]. x and a [I] [k]. v is fixed in all a [I] [k]. x> = a [I-1] [j]. in array x, you only need to find the smallest mini. In this case, you only need to traverse j for all j, and do not need to traverse j for every k, because of the next K value, its I-1 Line x value is less than the current x value, only the majority on the basis of the first, the first minimum only need to be compared with the next more can be.
2. a [I] [k]. x <= a [I-1] [j]. x (the position of the Dragon Ball in the previous cycle is greater than that in the next cycle)
A [I-1] [j]. dp + a [I-1] [j]. x-a [I] [k]. x + a [I] [k]. v, Set mini = a [I-1] [j]. dp-a [I-1] [j]. x. In this case, you only need to find the smallest mini in the position of all the dragon beads in the previous cycle greater than that in the next cycle.
Code:
# Include
# Include
# Include
# Include using namespace std; const int inf = 1999999999; struct node {int x; int v; int dp;} a [55] [1100]; int bbs (int x) {if (x <0) x =-x; return x;} bool cmp (node l, node r) {return l. x
= A [I-1] [j]. x) {mini = min (mini, (a [I-1] [j]. dp-a [I-1] [j]. x); j ++;} a [I] [k]. dp = min (a [I] [k]. dp, (mini + a [I] [k]. x + a [I] [k]. v);} // The Position of the Dragon Ball in the previous cycle is greater than that in the previous cycle j = n; mini = inf; for (int k = n; k> = 1; k --) {while (j> = 1 & a [I-1] [j]. x> = a [I] [k]. x) {mini = min (mini, (a [I-1] [j]. dp + a [I-1] [j]. x); j --;} a [I] [k]. dp = min (a [I] [k]. dp, (mini-a [I] [k]. x + a [I] [k]. v) ;}} int ans = inf; for (int I = 1; I <= n; I ++) {ans = min (ans, a [m] [I]. dp);} printf ("% d \ n", ans);} return 0 ;}