Question 1086: Dynamic Planning of Minimum Cost

Source: Internet
Author: User
Description:
There are n railway stations on a certain line, and there are three distance routes: L1, L2, and L3. The prices are C1, C2, and C3. their relations are as follows: 0 <S <= L1 c1l1 <S <= L2 c2l2 <S <= L3 C3 input <L1 <L2 <L3 <10 ^ 9,0 <C1 <C2 <c3 <10 ^ 9. The distance between each two sites cannot exceed L3. When the distance between the two stations to be moved is greater than L3, you can choose to get off from the middle station and then buy a ticket and then get on the bus. Therefore, the passenger will buy at least two tickets throughout the process. Now we will give you a L1, L2, L3, C1, C2, C3. Then there is the value of a B, which is the starting station and terminal of the passenger journey respectively. Then enter N, N for the total number of railway stations on the line, and then enter an integer in the N-1, representing respectively from the first station on the line, to 2nd stations, 3rd stations, ......, The distance between the nth station. According to the input, the minimum cost of the output passengers from Station A to Station B.

 

Input:
Enter data in the following format: L1 L2 L3 C1 C2 C3A BNA [2] a [3]… A [n]

 

Output:
There may be multiple groups of test data. For each group of data, output the minimum cost of passengers from Station A to Station B based on input.

 

Sample input:
1 2 3 1 2 31 222
Sample output:
2
 
  
Idea: the first thing that comes to mind is dynamic planning. Recursion can also be used, but it is best to use an array to save the Intermediate Value, otherwise it will time out.
 
  
Package Tsinghua;

Import java. Io. bufferedinputstream;
Import java. util. collections;

Minimum Public class call fee: 1086 {

Static long l [] = new long [3];
Static long P [] = new long [3];
Static int station [];
Static Long Dist [];
Public static void main (string [] ARGs ){
S = new second (New bufferedinputstream (system. In ));
While (S. hasnextlong ()){
For (INT I = 0; I <6; I ++ ){
If (I> 2)
P [I-3] = S. nextlong ();
Else
L [I] = S. nextlong ();
}
Int A = S. nextint ()-1;
Int B = S. nextint ()-1;
Int n = S. nextint ();
Station = new int [N];
Dist = new long [B-A + 1];
For (INT I = 1; I <n; I ++)
Station [I] = S. nextint ();
F (a, B );
If (B-a> 0)
System. Out. println (Dist [B-A]);
If (B =)
System. Out. println (0 );
}
}
Static void F (int A, int B ){
For (INTI = a + 1; I <= B; I ++) {// minimum cost of cyclic computing from the start point to each station

Int COUNT = 0;
Int x = I;
While (x-count-1> = A & station [x]-station [x-count-1] <= L [2]) // calculates the number of sites in front of this site that do not exceed L3

Count ++;
Long min = long. max_value;
For (Int J = 0; j <count; j ++) {// calculate the minimum cost to this site in sequence. The idea of Practical Dynamic Planning.
Min = math. Min (Min, DIST [i-j-1-a] + getp (station [I]-station [i-j-1]);
}
Dist [I-A] = min;
}
}

Static long getp (long Len ){
If (LEN = 0)
Return 0;
Else if (LEN <= L [0])
Return P [0];
Else if (LEN <= L [1])
Return P [1];
Else
Return P [2];
}

}


 
 

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.