Run a single-source shortest path from Jinseo's house, find the nearest type A and type B house, and report the closer type (A wins ties).
Medium4Shortest pathGraphHeapDynamic programmingInterviewNo attempts yetTime limit1sMemory limit256 MBJinseo loves animals. He keeps K animals that hop "chonggang chonggang" and K animals that hop "jjakpol jjakpol". He has found a job in another city and moves out tomorrow.
His new roommate Ilho dislikes animals, so Jinseo plans to leave the animals, one per house, at houses nearby.
There are two types of houses where Jinseo can leave an animal: type A houses and type B houses.
As it happens, the number of chonggang animals, the number of jjakpol animals, the number of type A houses and the number of type B houses are all equal to K.

Jinseo wants animals of the same kind to stay in houses of the same type. Either all chonggang animals go to type A houses and all jjakpol animals go to type B houses, or the other way around.
Jinseo likes the chonggang animals a little more. When every animal leaves its house at the same moment and travels along the roads to Jinseo's house, he wants the first animal to arrive to be a chonggang animal. All animals move at the same speed, so the first to arrive is the one whose house has the smallest shortest-path distance to Jinseo's house.
You are given a map with Jinseo's new house, the type A houses, the type B houses and houses of neither type. Determine whether the chonggang animals should live in type A houses or type B houses, and find the shortest-path distance between Jinseo's house and the house of the chonggang animal that arrives first.
If the shortest distance from Jinseo's house to the nearest type A house equals the shortest distance to the nearest type B house, the chonggang animals live in type A houses.
The first line contains the number of houses N and the number of roads M, separated by a space. (3≤N≤5000, 3≤M≤20000) The houses are numbered 1 to N.
The second line contains J, the number of Jinseo's house. (1≤J≤N)
The third line contains K, the number of animals of each kind. (1≤K, 2K≤N)
The fourth line contains the numbers of the K type A houses, separated by spaces.
The fifth line contains the numbers of the K type B houses, separated by spaces. No house is both type A and type B.
Each of the next M lines contains three integers X, Y and Z (1≤X,Y≤N, 1≤Z≤100), meaning there is a two-way road of length Z between house X and house Y.
If at least one type A or type B house is reachable from Jinseo's house, print on the first line the type of house the chonggang animals should live in (A or B), and on the second line the shortest distance from Jinseo's house to the nearest house of that type.
If only type A houses are reachable, print A and then the distance on the next line. If only type B houses are reachable, print B and then the distance on the next line. If no type A house and no type B house is reachable from Jinseo's house, print only -1 on a single line.