Ultraviolet A fill

Source: Internet
Author: User

The questions are as follows:

Problem dfill

 

There are three jugs with a volume of A, B and C liters. (A, B, and C are positive integers not greater than 200 ). the first and the secondjug are initially empty, while the third

Is completely filled with water. it is allowed to pour waterfrom one jug into another until either the first one is empty or the second oneis full. this operation can be performed med zero, one or more times.

 

You are to write a program that computes the least totalamount of water that needs to be poured; so that at least one of the jugscontains exactly D liters of water (D is a positive integer not greater than200 ). if it is not possible to measure d liters this way your program shouldfind a smaller amount of water d' <D which is closest to D and for which d' liters cocould be produced. when d 'is found, your program shocould compute theleast total amount of poured water needed to produce d' liters in at least oneof the jugs.

 

Input

The first line of input contains the number of test cases. in the next t lines, t test cases follow. each test case is given in one lineof input containing four space separated integers-A, B, C and D.

Output

The output consists of two integers separated by a singlespace. the first integer equals the least total amount (the sum of all watersyou pour from one jug to another) of poured water. the second integer equals D, if d liters of water cocould be produced by such transformations, or equals theclosest smaller value d' that your program has found.

 

Sample Input

Sample output

2

2 3 4 2

96 97 199 62

2 2

9859 62

 

Problem Source: Bulgarian National Olympus IAD in informatics2003

Problem submitter: Ivaylo riskov

Problem Solution: Ivaylo riskov, sadrul Habib Chowdhury


But it is different from the classic. This question requires that the total amount of water poured out by the specified amount of water should be the least. If the specified amount of water cannot be poured out, find the value closest to the specified amount of water (less ), and find the corresponding minimum amount of water. However, we still use BFS and VIS [] [] to record the status, but there are two points to note: 1. Use the sum [] array to record the total inverted volume of the current node, use the V [] array to record the total inverted water volume corresponding to the current cup depth (for later search) 2. When the current status is accessed (in my code, it is also vis [] []! =-1) does not immediately pruning, because if the total amount of water corresponding to the current code is less than that in the previous state, it is still necessary to continue BFs, because this wa is used several times. 3. After BFS is complete, Judge from D until you find the accessed point (V [I]! =-1) output. Pay attention to these points and use BFS normally.

The AC code is as follows:


Ultraviolet A fill

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.