CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • All Contests
    • June Long 2012
    • May Cook-Off
    • May Long 2012
  • DISCUSS
    • Forums
    • Blog
    • Wiki
    • Facebook
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Tutorials
    • Long Contest Ranks
    • Short Contest Ranks
    • Event Calendar
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Practice(hard) » Reaching the Rig

Reaching the Rig

Problem code: REACHRIG

  • Submit
  • All Submissions

Oil excavation is a common phenomenon in the sea. At every excavation site a rig is setup to extract the oil. The only mode to reach a rig is by a boat. Men who would like to reach a rig use special purpose boats which can run on crude fuel available at the rigs and the shore. These boats can run a mile for every unit of fuel they consume. Each man on the boat consumes a unit of food for every mile he travels. Boats have a limited load capacity. The net weight, including the weight of fuel, food and the men, on boat should never exceed its load capacity. The weight of one unit of fuel is same as the weight of one unit of food. However, as the rigs are far from the shore in the sea it is usually a very long sail. The capacity of the boat available for fuel may not be sufficient to reach the destination rig directly. In such cases, the men may goto a near by rig to load required fuel. The men can also store food at the rigs as well as at the shore for later consumption. They must buy all the food they require to reach the destination rig at the shore only. And all the men who started should be together throughout the sail.

You should now write a program which can help these men decide the minimum amount of food they require to reach the destination rig. The positions of rigs and the boat's starting point are indicated as co-ordinates of a two dimensional plane with x and y axises of 1 mile units. Food at shore is available only in whole units.

Assume that the food and fuel are consumed continuously, that is if you travel a partial mile the number of units of food and fuel consumed is equal to portion of mile traveled..

Input

The first line contains the number of test cases (An integer <= 100).

Each test case comprises of multiple lines containing list of integers as described below.

The first line of a test case has a list of integers terminated by -1. The first integer is the capacity of the boat. The integers following it up to -1 are the weights of men who need to reach the destination rig.

Lines following will have two integers separated by a space, each line indicating the x and y coordinates of a coordinate pair respectively. The first co-ordinate pair indicates the position of the boat's starting point. The last coordinate pair indicates the position of the destination rigs. All other points indicate the position of various rigs in the sea in a random order. The input for this test case will be terminated by a coordinate pair (0,0) and this should not be processed.

Output

For each test case, your program should print the minimum amount of food required to reach the destination on a separate line. If it is not possible to reach destination, then the program should print -1.

Example

Input:
3
250 20 30 -1 
10 -20 
-10 5 
30 15 
15 35 
0 0
300 15 20 30 -1 
10 -20 
-10 5 
30 15 
-30 45 
0 0
150 20 30 -1 
1 1 
100 100 
0 0 

Output:
111
807
-1

Author: admin
Date Added: 26-01-2010
Time Limit: 2 - 10 sec
Source Limit: 50000 Bytes
Languages: ADA, ASM, BASH, C, C99 strict, CAML, CLPS, CPP 4.0.0-8, CPP 4.3.2, CS2, D, ERL, FORT, HASK, ICON, JAR, JAVA, LISP clisp, LISP sbcl, LUA, NEM, NICE, PAS fpc, PAS gpc, PERL, PHP, PIKE, PYTH, PYTH 3.1.2, RUBY, SCALA, SCM guile, SCM qobi, ST


  • Submit

Comments

  • Login or Register to post a comment.

I posted a few solutions to

mahul_bh @ 1 Feb 2010 02:54 PM

I posted a few solutions to this problem. I am getting a runtime error. In the mysubmissions page, the tooltip shows runtime error(OTHER), while in the home page, the tooltip shows runtime error (SIGSEGV). It would be helpful if could determine the actual error. I have used the math library function pow(). Could that be a reason for error?

Is there an upper bound on

mahul_bh @ 1 Feb 2010 02:57 PM

Is there an upper bound on the number of points/men in a test case? If yes, what is the upper bound?

Can the admin please confirm

Anshul Agra @ 8 Feb 2010 10:23 AM

Can the admin please confirm if solution to 2nd test case is indeed 807 ? If it is, then can you please review the question statement?

either the question is not

lovemayur2003 @ 8 Feb 2010 03:38 PM

either the question is not clear or check the 2nd test case..

@ Anshul, Jaideep: The 2nd

mahul_bh @ 16 Feb 2010 05:12 PM

@ Anshul, Jaideep: The 2nd case is perfectly all right as per the test case.

Can it be confirmed whether there is an upper bound on the number of points/men? Or should I design my code so that it can handle any number of points?

Hello All, I am confused over

mcg @ 11 Mar 2010 01:52 PM

Hello All,

I am confused over this statement in problem:

"The men can also store food at the rigs as well as at the shore for later consumption. They must buy all the food they require to reach the destination rig at the shore only."

If men need to buy all food at starting shore only then whats the point in hacing food available at rigs?

Or is it that I have understood it wrongly.

Please help.

 

Manish

The boat has a limited

triplem @ 11 Mar 2010 03:42 PM

The boat has a limited capacity. If you store some food at a rig then you can go back to the shore and fill the boat up again, then make use of the extra food at the rig later.

OK Stephen...u mean to say

mcg @ 11 Mar 2010 04:04 PM

OK Stephen...u mean to say that by 'store' it means storing not in boat but at rig itself....

Yes.

triplem @ 11 Mar 2010 04:28 PM

Yes.

Guys, I have one more

mcg @ 11 Mar 2010 08:37 PM

Guys,

I have one more doubt.

Is return trip also to be taken into consideration?If its so, then I think answers for first and second cases are not in line.Answer for first case seems to be for one sided trip only while answer for second case is for both sided?

Does anyone agree with me?

What about the initial amount

checksum @ 20 Mar 2010 02:30 PM

What about the initial amount of food and fuel? For example, in the first case the weight of the two men adds upto 50kg. So for the remaning 200kg can we automatically assume that there is 100 units of food and fuel each?

@Admin @Mahul

navinp @ 26 Mar 2010 08:52 PM

@Admin

@Mahul Bhattacharya,

You get 807 by doing 32 *3*2 <- 3 round trips, depositing 43*3 food at  point (-10 5) , then another trip to point (-10,5) 32*3 + 6 food.  Total food consumed is 32*7 + 6 + 43*3 = 807 but i think this is wrong.

You can't deposit 43 food in a round trip . You are forgetting the weight of fuel. The total food deposited at point (-10,5) is 11 for each roundtrip because you need to store 32 fuel in the boat at shore.

Can you please clarify ?

the output is

navinp @ 2 Apr 2010 04:46 PM

the output is correct.

http://discussed.codechef.com/showthread.php?p=3082#post3082

@navin p your outputs are

codegambler @ 23 Sep 2010 11:33 PM
@navin p your outputs are correct but it don't contain any such case where a code can give WA. so make some good test cases.

@admin can you please take a

kushal154 @ 2 Oct 2010 07:07 PM

@admin can you please take a look at my solution...i am getting WA. Is there anything wrong with the logic or is it just rounding off values issues? If possible, can you also provide a particular test case?

SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:

Programming Competition Fetching successful submissions
Directi Go for Gold

HELP

Program should read from standard input and write to standard output. After you submit a solution you can see your results by clicking on the [My Submissions] tab on the problem page. Below are the possible results:

 

  • Accepted Your program ran successfully and gave a correct answer. If there is a score for the problem, this will be displayed in parenthesis next to the checkmark.
  • Time Limit Exceeded Your program was compiled successfully, but it didn't stop before time limit. Try optimizing your approach.
  • Wrong Answer Your program compiled and ran succesfully but the output did not match the expected output.
  • Runtime Error Your code compiled and ran but encountered an error. The most common reasons are using too much memory or dividing by zero. For the specific error codes see the help section.
  • Compilation Error Your code was unable to compile. When you see this icon, click on it for more information.
  • If you are still having problems, see a sample solution here.

CodeChef is a global programming communityCodeChef hosts online programming competitions
CodeChef is a non-commercial competitive programming community
  • About CodeChef
  • About Directi
  • CEO's Corner
  • C-Programming
  • Programming Languages
  • Contact Us
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
In order to report copyright violations of any kind, send in an email to copyright@codechef.com
CodeChef a product of Directi
The time now is:
CodeChef - A Platform for Aspiring Programmers

CodeChef was created as a platform to help programmers make it big in the world of algorithms, computer programming and programming contests. At CodeChef we work hard to revive the geek in you by hosting a programming contest at the start of the month and another smaller programming challenge in the middle of the month. We also aim to have training sessions and discussions related to algorithms, binary search, technicalities like array size and the likes. Apart from providing a platform for programming competitions, CodeChef also has various algorithm tutorials and forum discussions to help those who are new to the world of computer programming.

Practice Section - A Place to hone your 'Computer Programming Skills'

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our programming contest judge accepts solutions in over 35+ programming languages. Preparing for coding contests were never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming challenges that take place through-out the month on CodeChef.

Compete - Monthly Programming Contests and Cook-offs

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly coding contest and the shorter format Cook-off coding contest. Put yourself up for recognition and win great prizes. Our programming contests have prizes worth up to Rs.20,000 and $700lots more CodeChef goodies up for grabs.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be a part of CodeChef's Forums and interact with all our programmers - they love helping out other programmers and sharing their ideas. Have discussions around binary search, array size, branch-and-bound, Dijkstra's algorithm, Encryption algorithm and more by visiting the CodeChef Forums and Wiki section.

CodeChef Community

As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. You can also host a coding contest for your institute on CodeChef, organize an algorithm event and be a guest author on our blog.

Go For Gold

The Go for Gold Initiative was launched about a year after CodeChef was incepted, to help prepare Indian students for the ACM ICPC World Finals competition. In the run up to the ACM ICPC competition, the Go for Gold initiative uses CodeChef as a platform to train students for the ACM ICPC competition via multiple warm up contests. As an added incentive the Go for Gold initiative is also offering over Rs.8 lacs to the Indian team that beats the 29th position at the ACM ICPC world finals. Find out more about the Go for Gold and the ACM ICPC competition here.

Domain Name Registration, Web hosting, and Website Design provided by BigRock.com