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)

#include<iostream>
#include<cstdio>
#include<map>
#include<vector>
#include<algorithm>
#include<queue>
#define INF 500000000
using namespace std;

typedef pair<int,int> P;

int dis[6100];
vector<P> g[6100];
int g2[310][310];
map<string,int> id;
vector<int> gas;
int n,m,cap,d;
string a,b;
bool isg[6100];

int main(){
  ios_base::sync_with_stdio(false);
  while(cin >> n >> m >> cap,n||m||cap){
    id.clear(); gas.clear();
    for(int i=0;i<6100;i++)g[i].clear();
    
    cin >> a >> b;
    id[a] = id.size(); id[b] = id.size();
    gas.push_back(1); gas.push_back(2);

    for(int i=0;i<n;i++){
      cin >> a >> b >> d;
      if(id.find(a) == id.end())id[a] = id.size();
      if(id.find(b) == id.end())id[b] = id.size();
      g[id[a]].push_back(P(d,id[b]));
      g[id[b]].push_back(P(d,id[a]));
    }
    for(int i=0;i<=id.size();i++)isg[i] = false;
    isg[1] = isg[2] = true;

    for(int i=0;i<m;i++){
      cin >> a;
      gas.push_back(id[a]);
      isg[id[a]] = true;
    }

    for(int i=0;i<gas.size();i++){
      for(int j=0;j<gas.size();j++)g2[i][j] = INF;
      g2[i][i] = 0;
    }

    for(int i=0;i<gas.size();i++){
      for(int j=0;j<=id.size();j++)dis[j] = INF;
      dis[gas[i]] = 0;
      priority_queue<P,vector<P>,greater<P> > q;
      q.push(P(0,gas[i]));
      int cnt = 1;
      while(q.size()){
	if(cnt==gas.size())break;
	P p = q.top();q.pop();
	int dist = p.first;
	int pos = p.second;
	
	for(int x=0;x<g[pos].size();x++){
	  int tdis = dist + g[pos][x].first;
	  int nxt = g[pos][x].second;
	  if(dis[nxt] > tdis){
	    dis[nxt] = tdis;
	    q.push(P(tdis,nxt));
	    if(isg[nxt])cnt++;
	  }
	}
      }
      for(int j=0;j<gas.size();j++){
	if(cap*10<dis[gas[j]])continue;
	g2[i][j] = dis[gas[j]];	
      }
    }

    for(int k=0;k<gas.size();k++)
      for(int i=0;i<gas.size();i++)
	for(int j=0;j<gas.size();j++)
	  g2[i][j] = min(g2[i][j],g2[i][k] + g2[k][j]);

    if(g2[0][1] == INF)cout << -1 << endl;
    else cout << g2[0][1] << endl;
  }
}