Hexer
Time limit1sMemory limit128 MB
Find the shortest walk from town 1 to town n where each road can be used only after collecting swords for all monster kinds on it.
- Level
Medium7 of 10
- Topics
- Shortest path, Graph, Bit manipulation, Dynamic programming
- Solved
- No attempts yet
Problem
Byteasar has become a hexer, a slayer of monsters, and now he must return to his hometown Byteburg. The road home leads through a land full of beasts. Luckily its inhabitants, forced to fight monsters for centuries, have mastered blacksmithing: they can forge special swords that are very effective against particular kinds of beasts.
The land is vast: it has many towns connected by many roads. The roads never cross outside towns (some of them are underground passages).
Byteasar knows, for every road, which kinds of monster he may meet on it and how long it takes to walk it. He also knows which towns have blacksmiths and which kinds of monster each blacksmith's swords are effective against. To walk down a road, Byteasar must already own a sword effective against every kind of monster that may appear on that road; a sword is obtained by visiting a town whose blacksmith forges it. He may pass through any town or road as many times as he likes, and he can carry any number of swords.
Byteasar starts in town carrying no sword and wants to reach Byteburg (town ) as fast as possible. Find the minimum total walking time, or report that Byteburg cannot be reached.
Input
The first line contains four integers , , , (, , , ): the number of towns, the number of roads, the number of monster kinds, and the number of blacksmiths. Towns are numbered to ; town is the start and town is Byteburg. Monster kinds are numbered to .
The next lines describe the blacksmiths, one per line. Each line contains , , and then integers (, , ): the town where the blacksmith lives, how many monster kinds his swords are effective against, and those monster kinds in increasing order. A town may contain more than one blacksmith.
The following lines describe the roads, one per line. Each line contains , , , , and then integers (, , , ): the two towns the road connects, the time to walk it (the same in both directions), how many monster kinds may appear on it, and those monster kinds in increasing order. No two roads connect the same pair of towns.
Output
Print one integer: the minimum total time needed to reach Byteburg. If reaching Byteburg is impossible, print .
Hint
In the sample, Byteasar first walks to town and obtains a sword effective against monster kind , walks back to town , then goes to town , and finally reaches Byteburg (town ). The total time is .
If, on every route to Byteburg, some road requires a sword that Byteasar can never obtain (because no reachable blacksmith forges it), then Byteburg cannot be reached and the answer is .