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 » Compete » December 2011 Long Contest » Birthday Gift

Birthday Gift

Problem code: CHEFGIFT

  • All Submissions

All submissions for this problem are available.

It's our Chef's Birthday next week. His friends want to gift him something very nice and useful. They know how much Chef likes to go out on trips. So they decide to gift him a caravan i.e a travel trailer. As is clear from the description, a caravan is towed behind a car.


All the friends have chipped in a few dollars and now they have D dollars in total to buy a caravan. But there's one more thing they need to worry about. Caravans are sold outside their city. They will have to travel through various toll booths to buy one and bring it back. And they will have to pay some amount from their total D when they cross these toll booths. Luckily, they will not have to pay any toll for their own car(well, being Chef's friend has a lot of perks). But they will have to pay the toll for the caravan. They don't want to be spending too much on their tolls. Needless to say, they can't skip the toll booths.

 

There are m different roads between the Chef's city and the place where the caravans are sold. Each road takes a different route. There are n toll booths on every road. But an interesting observation is that the toll for a caravan need not be same for every road AND it also need not be same at every toll booth on the same road.

 

The friends want to gift him the most expensive caravan(which they can afford) and want to spend as little on tolls as they can. So m of the friends decide to travel on each of the roads individually. And once they buy a caravan, any one of them can start towing it behind his car. Also, a friend can move the caravan to some other friend's car i.e. to some other road. This is done so as to minimize the total toll. But there's another thing to be taken care of here. The caravan is not easy to move. So, there are mechanics available on every road who can do this. And there's a fixed amount they will charge to move the caravan from one road to another. This amount depends on the source and destination road between which the caravan is moved. But, there's a small restriction on the movement of the caravan. If the caravan has crossed the ith toll booth on a road, it can only be moved to a location before the (i+1)th booth on the other road. Initially, the caravan can be taken on any road.

 

Chef's friends will not want to buy an expensive caravan and find out later that they are out of money to pay for the toll. Nor do they want to end up buying a less expensive caravan and realize they could have bought a more expensive one. So they have collected all the details and want you to help them. Your task is to tell them the maximum amount they can spend on buying a caravan assuming they have D dollars initially and they have sufficient amount left to pay for the caravan toll.

Input:

First line of input contains T, number of tests.

Each test begins with a line containing 3 space separated integers: D, n and m: total amount of money, the number of toll booths on every road and the number of roads respectively.

Then follow m lines. Each line consists of n integers describing every road in order. The ith number in a line denotes the toll for the caravan at the ith toll booth on the respective road.

Then follows an mxm matrix, C, where the jth number on the ith row i.e. C[i][j] signifies the cost of moving the caravan from the ith road to the jth road. Ofcourse, C[i][i]=0.

Output:

Output a single integer: the maximum amount they will be able to spend on a caravan. If they don't have enough money to pay for the toll, print "-1" instead.

Constraints:

1<=T<=30
0<=D<=20000
1<=n,m<=100
0<=toll<=200
0<=C[i][j]<=100

Input:
3
40 5 4
10 9 8 7 6
8 7 6 9 12
16 1 1 1 1
7 6 5 4 3
0 6 17 7
8 0 19 2
9 9 0 0
12 4 18 0
10 2 2
5 6
6 7
0 0
0 0
12 3 2
3 4 5
5 7 2
0 5
2 0
Output:
20
-1
0


Author: vamsi_kavala
Date Added: 25-07-2011
Time Limit: 1 sec
Source Limit: 50000 Bytes
Languages: ADA, ASM, BASH, BF, C, C99 strict, CAML, CLOJ, CLPS, CPP 4.0.0-8, CPP 4.3.2, CS2, D, ERL, F#, FORT, GO, HASK, ICK, ICON, JAR, JAVA, JS, LISP clisp, LISP sbcl, LUA, NEM, NICE, PAS fpc, PAS gpc, PERL, PERL6, PHP, PIKE, PRLG, PYTH, PYTH 3.1.2, RUBY, SCALA, SCM guile, SCM qobi, ST, TCL, TEXT, WSPC


  • Submit

Comments

  • Login or Register to post a comment.

Hi. This is my first contest

onigiri @ 1 Dec 2011 06:27 PM
Hi. This is my first contest on with CHEF :-) I have a problem. I use C#. I think I solved correctly, but system returns run-time error. I tested in my local, and I found that if n or m is large, Standard Input does not work well. It seems that If the number of letters in one row exceed about 256, the input is cut. In other words, I think C# cannot read a row which has more than 256 letters. Are there any same C# coder? If it is only for me, I'm sorry for my stupid question.

It seems that C[2][2] = 1 in

lyrically @ 1 Dec 2011 07:47 PM
It seems that C[2][2] = 1 in the third case in the example.

My code is working fine with

comp11 @ 1 Dec 2011 11:39 PM
My code is working fine with the input given in the qn. Can u plz suggest some specific inputs for which the code can fail if not taken care of.

for my code some times it is

vamsi_algo @ 2 Dec 2011 12:18 AM
for my code some times it is giving wrong answer and some times time limit exceeded and my code is working crct for given inputs can any body post different inputs

the C matrix in the 2nd test

dsun @ 2 Dec 2011 07:44 AM
the C matrix in the 2nd test case has non-zero diagonal, could there be a problem in the test case generation? 0 0 0 1

@lyrically & dsun: Sorry for

vamsi_adm @ 2 Dec 2011 08:22 AM
@lyrically & dsun: Sorry for that. It's a typo. But the test data for the problem fits the description given in the statement. You need not worry about that. The error here will be rectified soon. @karthikabinav: Please do not discuss your algorithm/strategy here. And you should not ask for hints or any more test cases. It's against the rules. Please abide by them.

@nesamani025: you are not

admin @ 3 Dec 2011 03:16 PM

@nesamani025: you are not supposed to ask such help.

@jimmy valentine: Please do

admin @ 3 Dec 2011 03:17 PM

@jimmy valentine: Please do not ask for test cases during a contest.

"But an interesting

enot @ 3 Dec 2011 04:03 PM
"But an interesting observation is that the toll for a caravan need not be same for every road AND it also need not be same at every toll booth on the same road." I will be glad, if you explain it to me a little more.

are the cost for the toll

faisalnitt @ 3 Dec 2011 09:21 PM
are the cost for the toll booth given in order of coming from city to shop or vice-versa

@faisalnitt: tolls are given

cyberax @ 3 Dec 2011 11:34 PM
@faisalnitt: tolls are given from the shop to the Chef's city. otherwise the sample output would be wrong.

What should be the output for

jimmy valentine @ 4 Dec 2011 03:04 AM
What should be the output for : 1 0 1 1 0 0 Should it be 0 or -1 ?

@jimmyvalentine: if cost is

cyberax @ 4 Dec 2011 05:26 AM
@jimmyvalentine: if cost is 0, the road is free. so obviously the answer is 0.

For the 3rd test case ......

NR @ 4 Dec 2011 06:11 AM
For the 3rd test case ...... the trailer may begin at 2nd road, thus a toll of 2 then it transfers to road 1 after paying 2 for transfer, after then it pays 4 and 3 at tolls ... thus a total of 2+2+4+3 = 11 .... Thus answer should be 1 .... Can someone plz explain the test case .....

@NR: please, look at

karolis_kusas @ 4 Dec 2011 03:40 PM
@NR: please, look at faisalnitt's and cyberax's comments :)

working fine for all three

rmagon @ 5 Dec 2011 12:36 AM
working fine for all three given test cases, checked all boundary conditions.. still giving wrong answer :|

same here...it's giving wrong

jimmy valentine @ 5 Dec 2011 01:26 AM
same here...it's giving wrong answer. @admin - help please.

admin please answer to @enot

abdukodir @ 5 Dec 2011 06:57 PM
admin please answer to @enot 's comment, I didn't understand this part too !

@admin , in the first sample

gaurav_saxena2000 @ 6 Dec 2011 12:41 PM
@admin , in the first sample caravan can take the path of 1 toll on track 3 and at the last toll it can be moved to track 4 toll 0 so a total of 1+1+1+1+7 = 11 then answer should be 29? could you please explain this a bit.

@gaurav_saxena2000, For first

vinayak garg @ 6 Dec 2011 01:14 PM
@gaurav_saxena2000, For first test case cost = 16 + 1 + 1 +1 + 1 = 20. In your path cost will be 7 + 18 + 1 + 1 + 1 + 1. You have not included the cost of moving caravan from one road to another.

Can a extra line at the end

vinayak garg @ 6 Dec 2011 01:30 PM
Can a extra line at the end lead to wrong answer?

@vinayak garg : thanks for

gaurav_saxena2000 @ 6 Dec 2011 02:07 PM
@vinayak garg : thanks for help, actually I was starting from end I thought that is the order of road. I am also getting wa :(

anybody please explain me

abdukodir @ 6 Dec 2011 08:28 PM
anybody please explain me what does mean "But an interesting observation is that the toll for a caravan need not be same for every road AND it also need not be same at every toll booth on the same road." Code is working for all my tests but I'm getting wrong answer. I think it gives some spacial information !!!

@saayaa & blackbird : i am

gaurav_saxena2000 @ 6 Dec 2011 10:15 PM
@saayaa & blackbird : i am getting the same output still wa :(. @abdukodir : its just saying that for a specific frnd i out of m there would be different toll for each of the toll gate eg in sample n=5 m=4 so for each of the frnd there will be different toll in all the 5 gates and also for a sigle road/frnd the toll rate will differ for each gate you can see the sample1 as an example.

Everything is

phoebus1988 @ 7 Dec 2011 02:05 AM
Everything is correct...output is exactly matching the output for test cases...Still "codechef" is giving me wrong answer!!!What's the problem??

The problem uses a standard

casy @ 7 Dec 2011 07:38 AM
The problem uses a standard algorithm ... verified for so many test cases and always got correct results .... still getting WA ... The code is running correctly for around 15 test cases and then giving some wrong answer .... really frustating ... is there any problem with the output or input format ??

"If the caravan has crossed

the123abhishek @ 8 Dec 2011 12:47 AM
"If the caravan has crossed the ith toll booth on a road, it can only be moved to a location before the (i+1)th booth on the other road." Suppose the caravan has to move from the ith toll booth on road r1 to ith toll booth on road r2. If the most economical path for that goes through another road, say r3, then can the caravan move from r1 to r3 and then to r2. Please explain.

Yeah, I have the same

polarbear @ 8 Dec 2011 02:36 PM
Yeah, I have the same question as Abhishek. Also the question doesn't specify the order in which the toll fares are given (from the chef's city to the place where caravans are sold or the other way round). Since C(i,j) is not necessarily symmetric, order is important.

Admin Please check this

manoharsingh23 @ 8 Dec 2011 03:11 PM
Admin Please check this solution. I am sure there is no error in my solution but still its giving wrong ans..... http://www.codechef.com/viewsolution/750710

@polarbear: tolls are given

cyberax @ 9 Dec 2011 02:35 AM
@polarbear: tolls are given from the shop to the Chef's city. otherwise the sample output would be wrong.

@pranjalsahu: I guess yes,

mr777 @ 10 Dec 2011 04:45 PM
@pranjalsahu: I guess yes, you can move your carvan from ith road to jth many times but only if it's economical.

I think we have to find most

gragluhar @ 10 Dec 2011 05:20 PM
I think we have to find most economical path going through all toll booths 1...n and choosing a road(from 1...m) for all of 1...n(can start from any one of them for minimizing the overall cost) and taking cost of transferring into account. And we can transfer from ith toll booth of one road to "before" (i+1)th toll booth on any other road(Does that mean <=(i+1) or <(i+1) booth, I tried both). Am I missing some information? I tried many times but getting WA :( . Please help.

Are there any empty lines

mr777 @ 10 Dec 2011 08:57 PM
Are there any empty lines between test cases? Kindly tell me how is the sample input? Thanks

Can somebody please clarify

gragluhar @ 10 Dec 2011 10:02 PM
Can somebody please clarify my doubt. Please help. Thanks.

@gragluhar: Let's say you've

mr777 @ 10 Dec 2011 10:05 PM
@gragluhar: Let's say you've crossed toll 1, now you can only go to toll 2 of any road. Happy coding :)

Well, I've tried that, but

gragluhar @ 10 Dec 2011 10:48 PM
Well, I've tried that, but I'm getting wrong answer with that. It seemed like a straightforward problem. But I couldn't quite find a way to work through this problem. I think I'm missing something in the specification of the problem. Let me rephrase what I understand from problem, we have 1...n toll booths for 1...m roads, we start from any road(whichever suits better for max. answer), and at any point of time from ith toll booth, it can move to (i+1)th toll booth for any road with cost of road transfer into account. Am I correct? Thanks for any help.

Yes, you are correct.

mr777 @ 10 Dec 2011 11:20 PM
Yes, you are correct.

can m have a value of zero in

bodmas @ 11 Dec 2011 12:28 AM
can m have a value of zero in the input file?

@bodmas: According to the

mr777 @ 11 Dec 2011 01:25 AM
@bodmas: According to the constraints, NO

@Admins: Can you kindly

mr777 @ 11 Dec 2011 04:27 PM
@Admins: Can you kindly provide me with the test cases you applied on algorithm? I want to check mine as it generated run time error in this case. Kindly tell me the link to get test cases you applied. Thanks

@Admins: I want to challenge

mr777 @ 11 Dec 2011 04:35 PM
@Admins: I want to challenge the successful submitted solutions. Is there any option to do this?

Can anyone please explain why

dark_rider96 @ 12 Dec 2011 03:58 AM
Can anyone please explain why we can't use a variant of dijkstras ... and why most of the people have gone with DP/Floyd Warshall... Any help would be appreciated

i keep getting wrong

dark_rider96 @ 12 Dec 2011 04:00 AM
i keep getting wrong answer... i have used djikstras to find least cost path to the destination .

Admin/Anyone, As per my

prabutdr @ 13 Dec 2011 06:17 AM
Admin/Anyone, As per my understanding following test case should return 36 as result (1+1+1+1 = 4, 40-4 =36). But successful submitted problems were returning 37. Could anyone pls clarify this? 40 4 3 80 0 1 10 85 1 5 1 1 70 10 10 0 0 0 0 0 0 2 0 0

@prabutdr: I've seen the

mr777 @ 13 Dec 2011 11:14 AM
@prabutdr: I've seen the submitted solutions and some of them are wrong. I don't know what is the judging criteria. I also wrote to admins but no one still responded. I hope they will reply us in this regard.

i am getting a runtime

abhi_iit_10691 @ 14 Dec 2011 09:06 PM
i am getting a runtime error...i dont think memory usage or out of bound array access is a problem? can anybody suggest what problem can there be...

SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:

Programming Competition Fetching successful submissions
Directi Go for Gold
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