Petrol
Time limit2sMemory limit512 MB
Given a weighted graph with some marked stations, answer queries asking whether a tanker of capacity b can travel from station x to station y, refuelling only at stations.
- Level
Hard8 of 10
- Topics
- Graph, Shortest path, Union-find, Sorting
- Solved
- No attempts yet
Problem
Byteasar works in the logistics department of Byteoil, the petroleum company of Byteotia. His job is to plan fuel deliveries to petrol stations.
Byteotia has intersections, numbered from to , and two-way roads, each connecting a pair of intersections. Some intersections have a Byteoil petrol station.
The Byteoil transport fleet consists of tankers with fuel tanks of various capacities. A tanker burns litre of petrol per kilometre travelled, so a tanker whose tank holds litres can cover at most kilometres without refuelling. Drivers cannot use the fuel carried as cargo, but they can fill the tank free of charge at any Byteoil petrol station.
Byteasar's work consists of answering the following query over and over: can a tanker with a tank of capacity litres drive from the petrol station at intersection to the petrol station at intersection ? A tanker with a tank of capacity litres cannot drive more than kilometres without passing a Byteoil petrol station. Every trip starts at an intersection with a Byteoil petrol station and ends at an intersection with a Byteoil petrol station.
Help Byteasar answer his logistic queries automatically.
Input
The first line contains three integers , and (, ): the number of intersections, the number of petrol stations and the number of roads in Byteotia. The second line contains pairwise distinct integers (), the intersections with a Byteoil station.
The next lines describe the roads. The -th of these lines contains three integers , and (, , ): the -th road is kilometres long and connects intersection with intersection . Each pair of intersections is connected by at most one road.
The next line contains one integer (), the number of queries. Each of the following lines describes one query. The -th of these lines contains three integers , and (, , ), asking whether a tanker with a tank of capacity litres can drive from the petrol station at intersection to the petrol station at intersection . Both intersections and have a Byteoil petrol station.
Output
Print exactly lines. The -th line contains the single word TAK (yes) if a tanker with a tank of capacity litres can drive from intersection to intersection , and NIE (no) otherwise.