TSOC '15 Contest 1 #5 - Giant Ants

View as PDF

Submit solution

Points: 10 (partial)
Time limit: 1.0s
Memory limit: 64M

Problem type
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig

After getting out from the Building of Secrets, BMP and MSA have uncovered the secret cheesecake formula. Unfortunately, the security at the factory is very angry with them. Wanting to avoid social interaction with the security guards, BMP and MSA must get away from the cheesecake factory and get back home safely.

The city BMP and MSA live in represents the worst urban planning you have ever seen, with N intersections and M highways, going all over the place and taking up lots of valuable building land. However, that is not your concern right now, BMP and MSA has bribed you with DMOJ points to help them get home safely!

In an attempt to stop them, security has released their newest invention: the Giant Ant Dispenser on W intersections. The dispenser will constantly dispense an infinite number of ants which will eat BMP and MSA in an instant. Obviously, BMP and MSA has instructed you to avoid all routes where you will run into the giant ants. Luckily, the giant ants will only travel along roads, but since there are an infinite number of ants, they will split off in all directions (but only along roads of course). In addition, ants are quite slow, as they only travel at 0.25 roads per second (meaning that every 4th turn, they will fully move along 1 road) while BMP's car travels at 1 road/second.

Just in case your program can't find a safe way home for BMP and MSA because of either ants blocking their route or because there is no path that will get them home safely, they have a backup plan of sacrificing bobhob314 and begging for mercy from the guards. But only if there is no safe way home (not because they want to save bobhob314 of course, but just in case they have to sacrifice him at some later time).


BMP and MSA always move immediately before the giant ants do. So if BMP and MSA are currently on an intersection that's about to be over run by ants the next turn, they are safe as long as they're moving to a safe intersection.

However, if BMP and MSA go to an intersection that's about to be overrun by giant ants right before they arrive, they get eaten!

Input Specification

The first line will contain N (2 \le N \le 100) and M (0 \le M \le 30).

The next M lines will have X and Y (1 \le X, Y \le N) representing a road between the 2 intersections.

The next line will have W (0 \le W \le 15).

The next W lines will contain W_i, showing that intersection i contains a giant ant dispenser.

Output Specification

Output a single integer, the length of the shortest path for BMP and MSA to get home from 1 to N while avoiding the ants.

If this is not possible, output sacrifice bobhob314.

Sample Input 1

8 7
1 2
2 3
3 4
4 5
5 6
6 7
6 8

Sample Output 1

sacrifice bobhob314

Explanation for Sample Output 1

Assume that the orange represents the intersections where the ants are, and the green is the location of BMP and MSA's car.

Initially there is a clear path from 1 to N.

However after 4 turns the ants spread from intersection 7 to intersection 6.

And blocks the path.

Sample Input 2

6 5
1 2
2 3
3 4
4 6
4 5

Sample Output 2


Explanation for Sample Output 2

Initial setup.

After 3 turns.

However, BMP and MSA's car moves before the ants, narrowly escaping the ants and getting home just before getting eaten.


  • -1
    Dan13llljws  commented on Dec. 11, 2019, 3:12 p.m. edited

    What happens if there is no way home and ants cannot get you as well

  • -66
    bobhob314  commented on Feb. 23, 2015, 4:16 p.m.

    This comment is hidden due to too much negative feedback. Click here to view it.

    • 76
      bobhob314  commented on March 8, 2015, 2:06 p.m.

      ok u guys r right i disliked myself 2

  • 2
    Butane  commented on Feb. 23, 2015, 3:49 p.m.

    I do not know why I am getting a value error :(

    • 6
      FatalEagle  commented on Feb. 23, 2015, 3:57 p.m.

      The test data was bad. It's been fixed, and constraints are updated for more accuracy.

      To future problem setters, at least one test case should be close to a maxtest. Misleading constraints are against DMOJ's quality standards.