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 C (judge verdict: Accepted)

// AOJ 1318 Long Distance Taxi
// 2018.3.2 bal4u
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
// Hash table for string
#define HASHSIZ   35023
typedef struct { char *s; short id; } HASH;
HASH hash[HASHSIZ+2], *hashend = hash+HASHSIZ;
 
int insert(char *s, int id)
{
    unsigned long long i;
    char *p;
    HASH *tp;
 
    i = 0, p = s; while (*p) i = (i<<6) + *p++;
    tp = hash + (int)(i % HASHSIZ);
    while (tp->s != NULL) {
        if (!strcmp(tp->s, s)) return tp->id;
        if (++tp == hashend) tp = hash;
    }
    tp->s = s, tp->id = id;
    return -1;
}
 
#define QMAX 6002
typedef struct { int t; short s, c; } QUE;
QUE que[QMAX]; int qsize;
 
#define PARENT(i) ((i)>>1)
#define LEFT(i)   ((i)<<1)
#define RIGHT(i)  (((i)<<1)+1)
 
void min_heapify(int i)
{
    int l, r, min;
    QUE qt;
 
    l = LEFT(i), r = RIGHT(i);
    if (l < qsize && que[l].t < que[i].t) min = l; else min = i;
    if (r < qsize && que[r].t < que[min].t) min = r;
    if (min != i) {
        qt = que[i], que[i] = que[min], que[min] = qt;
        min_heapify(min);
    }
}
 
void deq()
{
    que[0] = que[--qsize];
    min_heapify(0);
}

void enq(short s, int t, short c)
{
    int i, min;
    QUE qt;
 
    i = qsize++;
    que[i].s = s, que[i].t = t, que[i].c = c;
    while (i > 0 && que[min = PARENT(i)].t > que[i].t) {
        qt = que[i], que[i] = que[min], que[min] = qt;
        i = min;
    }
}
 
//#define getchar_unlocked()  getchar()
int in()
{
    int n = 0;
    int c = getchar_unlocked();
    do n = 10*n + (c & 0xf), c = getchar_unlocked();
    while (c >= '0');
    return n;
}
 
void ins(char *buf)
{
    while ((*buf = getchar_unlocked()) > ' ') buf++;
    *buf = 0;
}
 
#define MAX 6002
short memo[3002*3];
short hi[MAX], *to[MAX], *dd[MAX];
char city[MAX][17]; int sz;
char LPG[MAX];
short mk[MAX][2002];
 
int dijkstra(int n, int cap, int start, int goal)
{
    int i, s, e, c, nc, d;
 
    memset(mk, -1, sizeof(mk));
    qsize = 0;
    enq(start, 0, cap);
    while (qsize) {
        s = que[0].s, d = que[0].t, c = que[0].c, deq();
        if (s == goal) return d;
 
        if (LPG[s]) c = cap;
		if (mk[s][c] >= 0) continue;
		for (i = c+1; i <= cap; i++) if (mk[s][i] >= 0 && mk[s][i] < d) break;
		if (i <= cap) continue;
		mk[s][c] = d;

        for (i = 0; i < hi[s]; i++) {
			if ((nc = c - dd[s][i]) < 0) continue;
            e = to[s][i];
            if (mk[e][nc] >= 0) continue;
			enq(e, d+dd[s][i], nc);
        }
    }
    return -1;
}
 
int main()
{
    int N, M, cap, i, j, a, b, c;
    char buf[20];
 
    while (N = in()) {
        for (a = 0; a < sz; a++) {
            if (hi[a]) free(to[a]), free(dd[a]);
        }
 
        M = in(), cap = 10*in();
        memset(hash, 0, sizeof(hash));
 
        ins(city[0]), ins(city[1]);
        insert(city[0], 0), insert(city[1], 1);
 
        memset(hi, 0, sizeof(hi));
        sz = 2;
        j = 0, i = N; while (i--) {
			ins(city[sz]), a = insert(city[sz], sz);
            if (a < 0) a = sz++;
            memo[j++] = a, hi[a]++;
            ins(city[sz]), b = insert(city[sz], sz);
            if (b < 0) b = sz++;
            memo[j++] = b, hi[b]++;
            memo[j++] = in();
        }
        for (a = 0; a < sz; a++) if (hi[a]) {
            to[a] = malloc(hi[a] << 1);
            dd[a] = malloc(hi[a] << 1);
        }
        memset(hi, 0, sz << 1);
        j = 0; while (N--) {
            a = memo[j++], b = memo[j++], c = memo[j++];
            i = hi[a]++, to[a][i] = b, dd[a][i] = c;
            i = hi[b]++, to[b][i] = a, dd[b][i] = c;
        }
        memset(LPG, 0, sz);
        for (i = 0; i < M; i++) {
            ins(buf), a = insert(buf, -1);
            if (a >= 0) LPG[a] = 1;
        }
 
        printf("%d\n", dijkstra(sz, cap, 1, 0));
    }
    return 0;
}