Long Distance Taxi |
Time Limit:30000 ms,Special Time Limit:75000 ms,Memory Limit:65536KB |
Total submit users:11,Accepted users:7 |
Problem 12523:No special judgement |
Problem description |
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 limit 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, 1C1, 2D1 C2, 1C2, 2D2 ≤ CN, 1CN, 2DN S1 S2 ≤ SM The first line of a dataset contains three integers (N,M,Cap), WhereNIs the number of roads (1 ≤N≤ 3000 ),MIs The number of LPG stations (1 ≤M≤ 300), andCapIs 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 followingNLines describe roads that connect cities. The roadI(1 ≤I≤N) Connects two different citiesCI, 1 andCI, 2 with an integer distanceDI (0 <DI≤2000) in kilometer, and he can go from either city to Other. You can assume that no two different roads connect the same pair of cities. The columns are separated by a single space. The nextMLines (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'ZAnd'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 34Tokyo KyotoTokyo Niigata 335Tokyo Shizuoka 174Shizuoka Nagoya 176Nagoya Kyoto 195Toyama Niigata 215Toyama Kyoto 296NagoyaNiigataToyama6 3 30Tokyo KyotoTokyo Niigata 335Tokyo Shizuoka 174Shizuoka Nagoya 176Nagoya Kyoto 195Toyama Niigata 215Toyama Kyoto 296NagoyaNiigataToyama0 0 0 |
Sample Output |
846-1 |
Interesting questions ,,
A journey problem. There are N roads, M gas stations, and the fuel tank has capacity (that is, the farthest distance to run without fuel ). Ask the shortest path from S to T.
Ideas:
Use S and T as gas stations to calculate spfa for these points and obtain a feasible subgraph. Perform spfa on the subgraph.
Runtime error: one and a half days. Give up...
#include<iostream>#include<queue>#include<cstdio>#include<cstdlib>#include<map>#define INF 0x3f3f3f3f#define MAXN 21111using namespace std;struct Edge{ int v,len,next;}E[MAXN];int ptr[MAXN],sta[MAXN],ptr2[MAXN];int dis[MAXN];int N,M,D,Edgenum;int S,T,Gnum;void addEdge( int u,int v,int len,int *G ){ E[Edgenum].v=v; E[Edgenum].len=len; E[Edgenum].next=G[u]; G[u]=Edgenum++;}void init(){ Edgenum=0; memset( ptr,-1,sizeof(ptr) ); memset( ptr2,-1,sizeof(ptr2) ); char str1[111],str2[111]; map<string,int> map; scanf( "%s %s",&str1,&str2 ); map[str1]=S=1; map[str2]=T=2; int rec=3,len; for( int i=0;i<N;i++ ) { scanf( "%s %s %d",&str1,&str2,&len ); if( map.find(str1)==map.end() ) map[str1]=rec++; if( map.find(str2)==map.end() ) map[str2]=rec++; addEdge( map[str1],map[str2],len,ptr ); addEdge( map[str2],map[str1],len,ptr ); } Gnum=0; for( int i=0;i<M;i++ ) { scanf( "%s",&str1 ); sta[Gnum++]=map[str1]; } sta[Gnum++]=S; sta[Gnum++]=T;}void spfa( int src,int *G ){ bool vis[MAXN]; queue<int> queue; memset(dis,0x3f,sizeof(dis)); memset(vis,0,sizeof(vis)); dis[src]=0; queue.push(src); while( !queue.empty() ) { int cur=queue.front(); queue.pop(); vis[src]=false; for( int i=G[cur];i!=-1;i=E[i].next ) { if( dis[E[i].v]>dis[cur]+E[i].len ) { dis[E[i].v]=dis[cur]+E[i].len; if( !vis[E[i].v] ) { queue.push(E[i].v); vis[E[i].v]=true; } } } }}int work(){ int ret; for( int i=0;i<Gnum;i++ ){ spfa( sta[i],ptr ); for( int j=0;j<Gnum;j++ ) { if( dis[sta[j]]<=D ) { addEdge( sta[i],sta[j],dis[sta[j]],ptr2 ); addEdge( sta[j],sta[i],dis[sta[j]],ptr2 ); } }}spfa(S,ptr2);return dis[T];}int main(){ //freopen("test.in","r",stdin ); //freopen("test.out","w",stdout ); while( scanf("%d %d %d",&N,&M,&D)!=EOF ) { D*=10; if( !N&&!M&&!D ) break; init(); int ans=work(); if( ans==INF ) ans=-1; printf( "%d\n",ans ); } return 0;}