1 Day Passport
Time limit8sMemory limit512 MB
Find the cheapest combination of unlimited-ride company passes and per-ride fares to travel from station S to T within H hours, where every train departs only on the hour.
- Level
Hard8 of 10
- Topics
- Shortest path, Bit manipulation, Dynamic programming, Graph
- Solved
- No attempts yet
Problem
D, a university student living in some country, is a huge fan of the national guitarist A and plans to attend a live concert held downtown this summer. But since D lives in a remote region, he was very worried about the travel cost. Then he learned about the "1Day Passport" sold cheaply by an organization called JAG.
JAG (Journey Administrative Group) oversees several railway companies in D's country. JAG has many stations nationwide and maintains lines connecting them. Each line is managed by one of the companies belonging to JAG and connects two stations in both directions with no intermediate station. Each line also has a fixed fare and travel time (the same in either direction). JAG's timetable is simple: trains arrive at and depart from stations at 0 minutes past every hour. Each JAG station is designed very efficiently, so the time to transfer between lines can be ignored. The travel cost is the simple sum of the fares.
The 1Day Passport is an unlimited-ride pass that JAG started selling to escape its recent financial trouble. JAG sells several kinds of 1Day Passports. Each passport can be bought at the price JAG sets. A passport also lists some company names belonging to JAG, and all lines managed by those companies can be ridden unlimited times for one day (at no extra charge). Any number of passports can be bought, and multiple passports can be used together. However, when passing through a line managed by a company not written on any passport, its fare must be paid as usual.
D wants to use 1Day Passports well to reach the concert venue while spending as little money as possible. He also dislikes spending extra money on lodging, so he wants to reach the venue within hours, which is one day in this country. But he is not good at calculation, so he asks you, his university friend who is good at programming, for help. For D's sake, write the following program.
Given JAG's line information and 1Day Passport information, write a program to find the minimum cost (minimum of the sum of passport prices and fares) to travel from D's nearest station to the concert venue's nearest station within hours. If he cannot arrive within hours, or there is no route to the concert venue, output -1.
Input
The input consists of multiple data sets, and the number of data sets in one input is at most 150. The format of each data set is as follows.
...
...
...
...
All input values are integers.
First, JAG's line information is given. () is the number of stations, () is the number of lines, () is the hours in one day, and () is the number of companies belonging to JAG. Stations are numbered . Companies are numbered . Then lines of line information follow. and () are the two stations connected by the -th line. () is the fare of the -th line, () is the travel time of the line, and () is the company managing the line. There are no two or more lines connecting the same pair of stations.
The next line gives , D's nearest station, and , the station nearest to A's concert venue (). and are different values.
Next, 1Day Passport information is given. () is the number of kinds of 1Day Passports. Then lines of 1Day Passport information follow. () and () are the number of companies written on the -th 1Day Passport and the passport price, respectively. On the same line, the company numbers written on the -th passport are given (). No two 1Day Passports with the same set of companies are given.
The end of the input is indicated by a line with . This data set must not be processed.
Output
For each data set, output the result on one line. That is, if a route exists from D's nearest station to the station nearest to the concert venue and he can arrive within hours, output the minimum fare needed; otherwise output -1.