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
    • Wiki
    • Forums
    • Blog
    • Facebook
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
    • Event Calendar
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Tutorials
    • Long Contest Ranks
    • Short Contest Ranks
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Compete » October Cook-off » Chef Travel Routes

Chef Travel Routes

Problem code: TRAVELER

  • All Submissions

All submissions for this problem are available.

Chef likes to travel very much. He plans some travel routes and wants to know their lengths. He hired you to make these calculations. But be careful, some of the routes are incorrect. There may be some misspelling in city names or there will be no road between some two consecutive cities in the route. Also note that Chef hates to visit the same city twice during his travel. Even the last city should differ from the first. Two consecutive cities in the route should also be different. So you need to check these conditions for the given routes too.

You will be given the list of all cities and all roads between them with their lengths. All roads are one-way. Also you will be given the list of all travel routes that Chef plans. For each route you should check whether it is correct and find its length in this case.

Input

The first line contains positive integer N, the number of cities. The second line contains space separated list of N strings, city names. All city names are distinct.

The third line contains non-negative integer M, the number of available roads. Each of the next M lines describes one road and contains names C1 and C2 of two cities followed by the positive integer D, the length of the one-way road that connects C1 with C2. It is guaranteed that C1 and C2 will be correct names of two different cities from the list of N cities given in the second line of the input file. For each pair of different cities there is at most one road in each direction and each road will be described exactly once in the input file.

Next line contains positive integer T, the number of travel routes planned by the Chef. Each of the next T lines contains positive integer K followed by K strings, names of cities of the current route. Cities are given in order in which Chef will visit them during his travel.

All strings in the input file composed only of lowercase, uppercase letters of the English alphabet and hyphens. Each string is non-empty and has length at most 20. If some line of the input file contains more then one element than consecutive elements of this line are separated by exactly one space. Each line of the input file has no leading or trailing spaces.

Output

For each travel route from the input file output a single line containing word ERROR if the route is incorrect and its length otherwise.

Constraints

1 <= N <= 50
0 <= M <= N * (N - 1)
1 <= D <= 20000
1 <= T <= 50
1 <= K <= 50
1 <= length of each string <= 20

Example

Input:
5
Donetsk Kiev New-York Miami Hollywood
9
Donetsk Kiev 560
Kiev New-York 7507
New-York Miami 1764
Miami Hollywood 28
Hollywood Miami 30
Miami New-York 1764
Kiev Donetsk 550
Hollywood New-York 1736
New-York Hollywood 1738
13
5 Donetsk Kiev New-York Miami Hollywood
5 Hollywood Miami New-York Kiev Donetsk
3 Donetsk Kiev Donetsk
2 Kyiv New-York
3 New-York Hollywood Miami
2 New-York Miami
3 Hollywood New-York Miami
4 Donetsk Kiev Miami Hollywood
2 Donetsk Hollywood
1 Donetsk
2 Mumbai Deli
6 Donetsk Kiev New-York Miami Hollywood New-York
2 Miami Miami

Output:
9859
ERROR
ERROR
ERROR
1768
1764
3500
ERROR
ERROR
0
ERROR
ERROR
ERROR

Explanation

The 2nd route is incorrect since there is no road from New-York to Kiev. Note however that inverse road from Kiev to New-York exists.
The 3rd route is incorrect since the first city coincides with the last one.
The 4th route is incorrect since there is no city with name Kyiv (Probably Chef means Kiev but he misspells this word).
The 8th route is incorrect since there is no road from Miami to Kiev.
The 9th route is incorrect since there is no road from Donetsk to Hollywood.
The 10th route is correct. Note that a route composed of exactly one city is always correct provided that city name is written correctly.
The 11th route is incorrect since there is no cities with names Mumbai and Deli. (Probably Chef is not so good in geography :))
The 12th route is incorrect since city New-York is visited twice.

Finally the 13th route is incorrect since we have equal consecutive cities.


Author: anton_lunyov
Date Added: 17-10-2011
Time Limit: 2 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.

there are multiple test cases

theeporithirumugam @ 23 Oct 2011 10:03 PM
there are multiple test cases ?

No. There is only one test

anton_adm @ 23 Oct 2011 10:06 PM
No. There is only one test case in the input file.

Is "mumbai" considered as a

mtk @ 23 Oct 2011 10:32 PM
Is "mumbai" considered as a mis-spelling for "Mumbai" ? please clarify?

City names are

david_adm @ 23 Oct 2011 10:34 PM
City names are case-sensitive, so "mumbai" and "Mumbai" are different.

What should be the output in

NR @ 23 Oct 2011 11:09 PM
What should be the output in the case : n=1 ,delhi , m=0 t=1 1 delhi

@NR See the following comment

anton_adm @ 23 Oct 2011 11:13 PM
@NR See the following comment in the Explanation section: "The 10th route is correct. Note that a route composed of exactly one city is always correct provided that city name is written correctly. "

Can the chef visit the same

pdwd @ 23 Oct 2011 11:17 PM
Can the chef visit the same city 3 times?

@pdwd No

anton_adm @ 23 Oct 2011 11:21 PM
@pdwd No

"Each of the next T lines

anector @ 23 Oct 2011 11:32 PM
"Each of the next T lines contains positive integer K followed by K strings, names of cities of the current route. Cities are given in order in which Chef will visit them during his travel." - is that true??? I mean, is that written in a *single* line??

@anector Yes it is true.

anton_adm @ 23 Oct 2011 11:36 PM
@anector Yes it is true.

whts the difference between a

gaurav_saxena2000 @ 23 Oct 2011 11:43 PM
whts the difference between a city visited twice and two cities visited consecutively ??

@gaurav_saxena2000: Chef

david_adm @ 23 Oct 2011 11:51 PM
@gaurav_saxena2000: Chef can't visit the same city twice, whether it be twice in a row, or whether he visits it once then comes back later.

Can anyone tell whats wrong

hemant.adil @ 24 Oct 2011 12:53 AM
Can anyone tell whats wrong with my code?? For test case it is working... my submission id is http://www.codechef.com/viewsolution/711852

@hemant.adil : When only one

logic_max @ 24 Oct 2011 01:47 AM
@hemant.adil : When only one city is given, which is not correct name; your solution gives 0 instead of ERROR.

Can somebody explain me plz,

pdwd @ 24 Oct 2011 02:12 AM
Can somebody explain me plz, why this code: http://ideone.com/gGHy7 gets WA?

@logic_max : Thanks :)

hemant.adil @ 24 Oct 2011 10:32 PM
@logic_max : Thanks :)

Is the time limit for each of

rama3572 @ 27 Oct 2011 09:45 PM
Is the time limit for each of the T cases or for entire set of T-cases?

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 computer programming. At CodeChef we work hard to revive the geek in you by hosting programming contests on a monthly basis. We also aim to have training sessions and events related to online programming for programmers around the world. Apart from providing a platform for programming competitions, CodeChef also has various 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 judge accepts solutions in over 35+ programming languages. Online programming was 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 competitions 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 programming contests and the shorter format Cook-off programming contests. Put yourself up for recognition and win great prizes. Prizes worth up to Rs.20,000 and $700 are up for grabs every month along with lots more CodeChef goodies.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be part of CodeChefs Forums and interact with all our programmers love helping out other programmers and share their ideas.

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. Be a part of the CodeChef community through CodeChef meetups and techtalks. You can also host a programming contest for your institute on CodeChef 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