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



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


Sample output:
846
-1

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop
inputs = lambda:map(int, raw_input().split())
while 1:
    n, m, cap = inputs()
    if n==0:
        break
    src, dest = raw_input().split()
    G = {}
    LPG = set()
    for i in xrange(n):
        c1, c2, d = raw_input().split()
        d = int(d)
        G.setdefault(c1, {})[c2] = d
        G.setdefault(c2, {})[c1] = d
    for i in xrange(m):
        s = raw_input()
        LPG.add(s)
    cap *= 10

    ans = {}
    dist = {(src, cap): 0}
    que = [(0, src, cap)]
    ans[src] = 0
    while que:
        co, v, rest = heappop(que)
        if dist[v, rest] < co:
            continue
        if v in LPG:
            rest = cap
        for t, d in G[v].items():
            if rest < d:
                continue
            n_key = (t, cap-d)
            if n_key not in dist:
                dist[n_key] = co+d
                heappush(que, (co+d, t, cap-d))
                ans[t] = min(ans.get(t, 10**18), co+d)
    print ans.get(dest, -1)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop
inputs = lambda:map(int, raw_input().split())
while 1:
    n, m, cap = inputs()
    if n==0:
        break
    src, dest = raw_input().split()
    G = {}
    f = set([src, dest])
    for i in xrange(n):
        c1, c2, d = raw_input().split()
        d = int(d)
        G.setdefault(c1, {})[c2] = d
        G.setdefault(c2, {})[c1] = d
    for i in xrange(m):
        s = raw_input()
        f.add(s)
    cap *= 10

    def dijkstra(src, G):
        dist = {src: 0}
        que = [(0, src)]
        while que:
            co, v = heappop(que)
            if dist[v] < co:
                continue
            for t, d in G.get(v, {}).items():
                if t not in dist:
                    dist[t] = co+d
                    heappush(que, (co+d, t))
        return dist

    H = {}
    def ss(u):
        for v, cost in dijkstra(u, G).items():
            if v in f and cost <= cap:
                H.setdefault(u, {})[v] = cost
                H.setdefault(v, {})[u] = cost

    for e in f: ss(e)

    ans = dijkstra(src, H)
    print ans.get(dest, -1)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop
n, m, cap = map(int, raw_input().split())
src, dest = raw_input().split()
G = {}
for i in xrange(n):
    c1, c2, d = raw_input().split()
    d = int(d)
    G.setdefault(c1, {})[c2] = d
    G.setdefault(c2, {})[c1] = d
S = {raw_input() for i in xrange(m)} | {src, dest}

INF = 10**18
cap *= 10

def dijkstra(s, G):
    dist = {s: 0}
    que = [(0, s)]
    while que:
        co, v = heappop(que)
        if dist.get(v, INF) < co:
            continue
        for t, cost in G[v].items():
            if co+cost < dist.get(t, INF):
                dist[t] = co+cost
                heappush(que, (co+cost, t))
    return dist

H = {}
for s in S:
    dist = dijkstra(s, G)
    for k, v in dist.items():
        if k in S and v <= cap:
            H.setdefault(s, {})[k] = v
dist = dijkstra(src, H)
print dist.get(dest, -1)

Submission status: Accepted

Solution code:
from heapq import heappush, heappop
while 1:
    n, m, cap = map(int, raw_input().split())
    if n==0:
        break
    src, dest = raw_input().split()
    G = {}
    for i in xrange(n):
        c1, c2, d = raw_input().split()
        d = int(d)
        G.setdefault(c1, {})[c2] = d
        G.setdefault(c2, {})[c1] = d
    S = {raw_input() for i in xrange(m)} | {src, dest}

    INF = 10**18
    cap *= 10

    def dijkstra(s, G):
        dist = {s: 0}
        que = [(0, s)]
        while que:
            co, v = heappop(que)
            if dist.get(v, INF) < co:
                continue
            for t, cost in G[v].items():
                if co+cost < dist.get(t, INF):
                    dist[t] = co+cost
                    heappush(que, (co+cost, t))
        return dist

    H = {}
    for s in S:
        dist = dijkstra(s, G)
        for k, v in dist.items():
            if k in S and v <= cap:
                H.setdefault(s, {})[k] = v
    dist = dijkstra(src, H)
    print dist.get(dest, -1)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**13
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    rr = []

    def f(n,m,cap):
        cap *= 10
        s,t = LS()
        e = collections.defaultdict(list)
        for _ in range(n):
            a,b,c = LS()
            c = int(c)
            e[a].append((b,c))
            e[b].append((a,c))
        cs = set([S() for _ in range(m)])

        def search(s,t):
            d = collections.defaultdict(lambda: inf)
            d[(s,cap)] = 0
            q = []
            heapq.heappush(q, (0, (s,cap)))
            v = collections.defaultdict(bool)
            while len(q):
                k, u = heapq.heappop(q)
                if v[u]:
                    continue
                v[u] = True
                if u[0] == t:
                    return k

                for uv, ud in e[u[0]]:
                    uc = u[1] - ud
                    if uc < 0:
                        continue
                    if uv in cs:
                        uc = cap
                    uv = (uv, uc)
                    if v[uv]:
                        continue
                    vd = k + ud
                    if d[uv] > vd:
                        d[uv] = vd
                        heapq.heappush(q, (vd, uv))

            return None

        r = search(s,t)
        if r is None:
            return -1

        return r

    while 1:
        n,m,l = LI()
        if n == 0:
            break
        rr.append(f(n,m,l))
        # print('rr', rr[-1])

    return '\n'.join(map(str,rr))


print(main())