The Hero
Time limit1sMemory limit128 MB
Find the shortest sailing time from island 1 to island n, waiting on islands to dodge trap intervals that forbid presence on active days.
- Level
Medium7 of 10
- Topics
- Shortest path, Intervals
- Solved
- No attempts yet
Problem
Byteotheus, the most famous hero of Byteotia, has won another battle. While his crew loads the ship with the spoils, he sits in his cabin and plans the way back to his home island, The Bitaca. That is not an easy task. Many gods envy his popularity and would gladly take him down a peg. A few of them favour him, the goddess Bythena above all. It was Bythena who sent Byteotheus a dream last night to warn him about the dangers ahead.
The Byteonian Sea has islands, numbered from 1 to . The ship is now at island 1 and its destination is The Bitaca, island . Some pairs of islands are joined by one-way sea routes, numbered from 1 to . Route leads from island to island and takes exactly days. At most 10 routes start at any single island.
If the ship leaves island along route at dawn on day , it reaches island at dawn on day . The ship may stay at any island as long as it likes. Before reaching the next island it cannot leave the chosen route, and it cannot stay at sea longer than the route takes. The earliest moment Byteotheus can leave island 1 is dawn on day 1.
Bythena's warning was precise. She gave Byteotheus the exact list of the traps the gods have prepared. Each trap sits on one island and is active during one period. Trap is on island and is active from day through day , day included. If the ship is on an island while a trap there is active, nobody survives. The Bitaca has no traps, and no trap on island 1 is active on day 1.
If the ship reaches an island at dawn on day and leaves it at dawn on day , it counts as being on that island on every day from to , both included. A trap on that island active on any of those days kills the crew.
Byteotheus wants a way home that avoids every trap, and he also wants to know how much longer the traps make his voyage. Find the smallest number of days he needs to reach The Bitaca safely.
Input
The first line contains two integers and (, ), the number of islands and the number of sea routes. Each of the next lines describes one route with three integers , , (, , ), meaning that route leads from island to island and takes days. All routes are one way. At most 10 routes start at any single island.
The next line contains one integer (), the number of traps. Each of the next lines describes one trap with three integers , , (, ), meaning that trap is on island and is active from day through day . If , then .
Output
If no route avoids every trap, print NIE on a single line. That is Polish for no. Otherwise print one integer , the smallest number of days the voyage takes. The ship then reaches The Bitaca at dawn on day .
Hint
In the first example Byteotheus leaves island 1 at dawn on day 1 and reaches island 2 on day 4. He waits one day, sails for island 3 and arrives on day 6, then turns back to island 2 at once. On day 8 he leaves island 2 for island 4, arrives on day 10, and reaches The Bitaca on day 11.