Uva10201-Adventures in Moving-Part IV (01 backpack)

Source: Internet
Author: User

Uva10201-Adventures in Moving-Part IV (01 backpack)

 

A car is about to walk D distance, then it has a l fuel tank, and there is a 100L at the beginning. Now you will be given a gas station along the way, and the price of each litre of oil at this gas station requires that the oil should be greater than or equal to L at the end of the end, and ask you the minimum fuel fee. If you cannot reach the target location, output Impossible.

 

Solution: First, you have to arrive at the gas station first, and then choose not to refuel this gas station. If you choose to refuel, you have to add more or less oil. Dp [j] [I] indicates that there is jL oil at the gas station I, dp [j] [I] = MIn (dp [j + d [I] [l] [l ]); d [I] [l] represents the distance between the I-th gas station and the l-th gas station, which means that when the gas station arrives, the gas station from which the fuel is added should be the least expensive. Then there is the question of how much oil is added at the gas station.

If they all arrive at the location D from 0, add a gas station to the two locations and use price to indicate whether the gas station actually exists. Only when it actually exists can it be refueled. Initial Condition dp [0] [100] = 0;

 

Code:

 

# Include
 
  
# Include
  
   
Const int n= 205; const int INF = 0x3f3f3f3f; int N; int gas [n] [2]; int f [N] [N]; int d [N] [N]; char str [N]; int Min (const int a, const int B) {return a <B? A: B;} void init () {for (int I = 0; I <n; I ++) for (int j = I; j <n; j ++) d [I] [j] = gas [j] [0]-gas [I] [0]; for (int I = 0; I <n; I ++) for (int j = 0; j <= 200; j ++) f [I] [j] = INF; f [0] [100] = 0 ;} int main () {int t; int D; scanf (% d, & t); while (t --) {scanf (% d % * c, & D ); n = 1; gas [0] [0] = 0; while (gets (str )! = NULL & str [0]! = '') {Sscanf (str, % d, & gas [n] [0], & gas [n] [1]); n ++ ;} /* for (int I = 0; I <n; I ++) printf (% d, gas [I] [0], gas [I] [1]); */if (gas [n-1] [0]! = D) {gas [n] [0] = D; gas [n] [1] = 0; n ++;} init (); for (int I = 1; I <n; I ++) {for (int j = 0; j <= 200; j ++) {for (int l = I-1; l> = 0; l --) {// which method is used to reach the most economical if (j + d [l] [I] <= 200) f [I] [j] = Min (f [I] [j], f [l] [j + d [l] [I]); elsebreak ;} if (gas [I] [1] & f [I] [j]! = INF) {// Add more or less oil (the premise can be reached, and the gas station is true) for (int k = j + 1; k <= 200; k ++) f [I] [k] = f [I] [j] + (k-j) * gas [I] [1] ;}} int ans = INF; for (int I = 100; I <= 200; I ++) ans = Min (ans, f [n-1] [I]); if (ans! = INF) printf (% d, ans); elseprintf (Impossible); if (t) printf ();} 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.