NOJ1859 3D DP

Source: Internet
Author: User
Tags cmath

NOJ1859 3D DP

Description

There are many divisions before and after the big Gobi cross-country competition, and a fleet also has multiple vehicles. A fleet can only use one vehicle in one segment, but the vehicle can be changed at any time when it arrives at the transfer station (points separated by segments. Different cars have different performances in different divisions. If the rules do not limit the number of replicas, it is clear that the best cars can be used in each segment, but unfortunately, the number of replicas is limited, therefore, it is very important to arrange a reasonable car replacement strategy. We believe that the team can choose any type of vehicle to start the competition, not counting the number of car changes.
Now we will show the performance of each vehicle in the N divisions from the start point to the end point (the time required to finish the current division). Calculate the shortest time required to finish all the divisions.

Input

The first line contains three positive integers: N, M, and Q, represents the number of divisions, the type of vehicles used by the fleet, and the maximum number of vehicle replacements (1 ≤ N ≤ limit, 1 ≤ M ≤ 10, 1 ≤ Q ≤ 100 ).
In the next M line, N positive integers in each line. The number of j in line I indicates the performance of the car in the j Division (the time required for the car to finish this division). The maximum time allowed for each division is no more than 1000.

Output

The output line contains an integer, indicating the shortest completion time of the team within the rule.


Input example

3 2 1
1 4 1
2 2 3

Output example

5



Solutions

Three-dimensional DP dp [I] [j] [k] array represents the I-segment track. After j-cars are replaced, the last car selected is the minimum time of k-cars.

Suppose we have now considered the first (I-1) section of the track and now consider the I section of the track.

D [I] [j] [k] = d [I-1] [j] + speed [I] [k];

D [I] [j] [k] = min (d [I-1] [J-1] [x]) + speed [I] [k]; (x is not equal to k)

Combining two formulas, d [I] [j] [k] = min (d [I-1] [j] [k], d [I-1] [J-1] [x] (x not equal to k) + speed [I] [k]

The final answer should be min (d [number of tracks] [x] [y]).


Next, consider initializing ........

It can be seen that the (I, j) state is composed of (I-1, j) and (I-1, J-1) these two statuses are introduced, so the outer loop traversal I contains loop traversal j and then traversing k in it.

So we need to initialize the I = 1 and j = 0 .....

For details, see the code.


# Include
 
  
# Include
  
   
# Include
   
    
# Include # define INF 1000000010 const int max_saidao = 10010; const int max_che = 15; const int max_huanche = 105; using namespace std; int speed [max_saidao] [max_che]; // speed [I] [j] indicates the speed of the j-car on the I-th track. int dp [max_saidao] [max_huanche] [max_che]; // dp [I] [j] [k] indicates that the last car used when I switched to the first track j times is k's shortest time int main () {int saidao, che, huanche; scanf ("% d", & saidao, & che, & huanche); for (int I = 1; I <= che; I ++) {for (int j = 1; j <= Saidao; j ++) scanf ("% d", & speed [j] [I]) ;}for (int I = 0; I <= saidao; I ++) {for (int j = 0; j <= huanche; j ++) {for (int k = 0; k <= che; k ++) dp [I] [j] [k] = INF ;}// initialize dp [1] [0] [x] for (int I = 1; I <= che; I ++) dp [1] [0] [I] = speed [1] [I]; // initialize dp [x] [0] [x] for (int I = 1; I <= che; I ++) {// car I, for (int j = 2; j <= saidao; j ++) {dp [j] [0] [I] = dp [J-1] [0] [I] + speed [j] [I] ;}// d P for (int I = 2; I <= saidao; I ++) {for (int j = 1; j <= huanche & j <I; j ++) {for (int k = 1; k <= che; k ++) {int _ min = INF; for (int p = 1; p <= che; p ++) {if (p! = K) if (_ min> dp [I-1] [J-1] [p]) _ min = dp [I-1] [J-1] [p];} if (_ min> dp [I-1] [j] [k]) _ min = dp [I-1] [j] [k]; dp [I] [j] [k] = _ min + speed [I] [k] ;}} // output the smallest int _ min = INF in dp [saidao] [x] [y]; for (int I = 0; I <= huanche; I ++) {for (int j = 1; j <= che; j ++) {if (_ min> dp [saidao] [I] [j]) _ min = dp [saidao] [I] [j] ;}} printf ("% d \ n", _ min); return 0 ;}
   
  
 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.