Problem:
Problem D: Long Distance Taxi

A taxi driver, Nakamura, was so delighted because he got a passenger who wanted to go to a
city thousands of kilometers away. However, he had a problem. As you may know, most taxis in
Japan run on liquefied petroleum gas (LPG) because it is cheaper than gasoline. There are more
than 50,000 gas stations in the country, but less than one percent of them sell LPG. Although
the LPG tank of his car was full, the tank capacity is limited and his car runs 10 kilometer per
liter, so he may not be able to get to the destination without filling the tank on the way. He
knew all the locations of LPG stations.
Your task is to write a program that finds the best way from the current location to the destination without running out of gas.

Input

The input consists of several datasets, and each dataset is in the following format.

N M cap

src dest

c1,1 c1,2 d1

c2,1 c2,2 d2

.

.

.

cN,1 cN,2 dN

s1

s2

.

.

.

sM

The first line of a dataset contains three integers (N, M, cap), where N is the number of roads
(1 ≤ N ≤ 3000),M is the number of LPG stations (1≤ M ≤ 300), and cap is the tank capacity
(1 ≤ cap ≤ 200) in liter. The next line contains the name of the current city (src) and the name
of the destination city (dest). The destination city is always different from the current city.
The following N lines describe roads that connect cities. The road i (1 ≤ i ≤ N) connects two
different cities ci,1 and ci,2 with an integer distance di (0 < di ≤ 2000) in kilometer, and he can
go from either city to the other. You can assume that no two different roads connect the same
pair of cities. The columns are separated by a single space. The next M lines (s1,s2,...,sM) indicate the names of the cities with LPG station.
You can assume that a city with LPG station has at least one road.

The name of a city has no more than 15 characters. Only English alphabet ('A' to 'Z' and 'a'
to 'z', case sensitive) is allowed for the name.

A line with three zeros terminates the input.

Output

For each dataset, output a line containing the length (in kilometer) of the shortest possible
journey from the current city to the destination city. If Nakamura cannot reach the destination,
output "-1" (without quotation marks). You must not output any other characters.
The actual tank capacity is usually a little bit larger than that on the specification sheet, so
you can assume that he can reach a city even when the remaining amount of the gas becomes
exactly zero. In addition, you can always fill the tank at the destination so you do not have to
worry about the return trip.

Sample Input

6 3 34
Tokyo Kyoto
Tokyo Niigata 335
Tokyo Shizuoka 174
Shizuoka Nagoya 176
Nagoya Kyoto 195
Toyama Niigata 215
Toyama Kyoto 296
Nagoya
Niigata
Toyama
6 3 30
Tokyo Kyoto
Tokyo Niigata 335
Tokyo Shizuoka 174
Shizuoka Nagoya 176
Nagoya Kyoto 195
Toyama Niigata 215
Toyama Kyoto 296
Nagoya
Niigata
Toyama
0 0 0

Output for the Sample Input

846
-1

Solution in JAVA (judge verdict: Memory Limit Exceeded)

import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.Set;

//Long Distance Taxi
public class Main{

	class R{
		int id;
		Set<E> adj;
		public R(int id) {
			this.id = id;
			adj = new HashSet<E>();
		}
	}
	class E{
		int t, d;
		public E(int t, int d) {
			this.t = t;
			this.d = d;
		}
	}

	R[] town;
	int ID, INF = 1<<29;
	long M = 10000, MM = 100000000;
	Map<String, R> ref;
	R get(String s){
		if(ref.containsKey(s))return ref.get(s);
		R r = new R(ID);
		ref.put(s, r);
		return town[ID++] = r;
	}

	Map<Integer, Integer>[] dist;
	@SuppressWarnings("unchecked")
	void run(){
		Scanner sc = new Scanner(System.in);
		for(;;){
			int n = sc.nextInt(), m = sc.nextInt(), cap = sc.nextInt();
			if((n|m|cap)==0)break;
			ref = new HashMap<String, R>();
			ID = 0;
			town = new R[2*n];
			R src = get(sc.next()), dest = get(sc.next());
			while(n--!=0){
				R s = get(sc.next()), t = get(sc.next());
				int d = sc.nextInt();
				s.adj.add(new E(t.id, d));
				t.adj.add(new E(s.id, d));
			}
			boolean[] gas = new boolean[ID];
			while(m--!=0){
				R r = get(sc.next());
				gas[r.id] = true;
			}
			dist = new HashMap[ID];
			for(int i=0;i<ID;i++)dist[i]=new HashMap<Integer, Integer>();
			dist[src.id].put(0, 0);
			PriorityQueue<Long> q = new PriorityQueue<Long>(ID, new Comparator<Long>() {
				public int compare(Long o1, Long o2) {
					return (int) Math.signum((o1%M)-(o2%M));
				}
			});
			q.add(src.id*MM);
			int res = INF;
			while(!q.isEmpty()){
				long a = q.poll();
				int v = (int) (a/MM), c = (int) ((a%MM)/M), time = (int) (a%M);
				if(v==dest.id){
					res = Math.min(res, time);
				}
				for(E e:town[v].adj){
					int w = time + e.d;
					if(10*cap<c+e.d)continue;
					if(gas[e.t]){
						if(!dist[e.t].containsKey(0)){
							dist[e.t].put(0, w);
							q.add(e.t*MM+w);
						}
						else if(w<dist[e.t].get(0)){
							dist[e.t].put(0, w);
							q.add(e.t*MM+w);
						}
					}
					else{
						if(!dist[e.t].containsKey(c+e.d)){
							dist[e.t].put(c+e.d, w);
							q.add(e.t*MM+(c+e.d)*M+w);
						}
						else if(w<dist[e.t].get(c+e.d)){
							dist[e.t].put(c+e.d, w);
							q.add(e.t*MM+(c+e.d)*M+w);
						}
					}
				}
			}
			System.out.println(res==INF?-1:res);
		}
	}

	public static void main(String[] args) {
		new Main().run();
	}
}