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: Wrong Answer)

#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f

#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cctype>
#include <utility>
 
using namespace std;
 
typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P > PP;

class Node {
public:
  int to;
  int dist;
  Node() {}
  Node(int _t,int _d) : to(_t),dist(_d){}
};

bool is_LPG[6001];
bool dp[6001][2005]; //dp[city_id][remaining_LPG] = dist;
static int gCity_idx = 0;

int get_city_id(map<string,int>& cities,const string& city){
  if(cities.find(city) == cities.end()){
    cities[city] = gCity_idx++;
  }
  return cities[city];
}

class State {
public:
  int city_id;
  int dist;
  int remaining_LPG;
  State(int _city_id,int _dist,int _con){
    city_id = _city_id;
    dist = _dist;
    remaining_LPG = _con;
  }

  bool operator <(const State& s) const{
    return (dist < s.dist);
  }

  bool operator >(const State& s) const{
    return (dist < s.dist);
  }
};

int main(){
  int total_roads;
  int total_LPG_stations;
  int capacity;
  while(~scanf("%d %d %d",
	       &total_roads,
	       &total_LPG_stations,
	       &capacity)){
    if(total_roads == 0
       && total_LPG_stations == 0
       && capacity == 0) break;
    gCity_idx = 0;

    map<string,int> cities;
    vector<vector<Node> >  nodes(6001);
    memset(is_LPG,false,sizeof(is_LPG));
    memset(dp,false,sizeof(dp));

    string src,dst;
    cin >> src >> dst;
    get_city_id(cities,src);
    get_city_id(cities,dst);

    for(int road_idx = 0; road_idx < total_roads; road_idx++){
      string from_str,to_str;
      int dist;

      cin >> from_str >> to_str >> dist;
      int from_num = get_city_id(cities,from_str);
      int to_num = get_city_id(cities,to_str);

      nodes[from_num].push_back(Node(to_num,dist));
      nodes[to_num].push_back(Node(from_num,dist));
    }

    for(int LPG_idx = 0; LPG_idx < total_LPG_stations; LPG_idx++){
      string city;
      cin >> city;
      is_LPG[get_city_id(cities,city)] = true;
    }

    priority_queue<State,vector<State>, greater<State> > que;
    que.push(State(get_city_id(cities,src),0,10*capacity));

    int dst_id = get_city_id(cities,dst);

    int res = INF;
    while(!que.empty()){
      State s = que.top();
      que.pop();

      if(s.city_id == dst_id){
	res = s.dist;
	goto found;
      }
      dp[s.city_id][s.remaining_LPG] = true;

      for(int to_idx=0;to_idx<nodes[s.city_id].size();to_idx++){
	int to = nodes[s.city_id][to_idx].to;

	int dist = nodes[s.city_id][to_idx].dist;
	if(s.remaining_LPG < dist) continue;

	int next_remaining_LPG = (is_LPG[to] ? 10 * capacity : s.remaining_LPG - dist);
	if(dp[to][next_remaining_LPG]) continue;

	que.push(State(to,s.dist + dist,next_remaining_LPG));
      }
    }
  found:;
    printf("%d\n",res >= INF ? -1 : res);
  }
  return 0;
}