Samball is going to travel in the coming vacation. Now it's time to make a plan. After choosing the destination city, the next step is to determine the travel route. As this poor guy has just experienced a tragic lost of money, he really has limited amount of money to spend. He wants to find the most costless route. Samball has just learned that the travel company will carry out a discount strategy during the vacation: the most expensive flight connecting two cities along the route will be free. This is really a big news.
Now given the source and destination cities, and the costs of all the flights, you are to calculate the minimum cost. It is assumed that the flights Samball selects will not have any cycles and the destination is reachable from the source.
Input
The input contains several test cases, each begins with a line containing names of the source city and the destination city. The next line contains an integer m (<=100), the number of flights, and then m lines follow, each contains names of the source city
and the destination city of the flight and the corresponding cost. City names are composed of not more than 10 uppercase letters. Costs are integers between 0 to 10000 inclusively.
Process to the end of file.
Output
For each test case, output the minimum cost in a single line.
Sample Input
HANGZHOU BEIJING
2
HANGZHOU SHANGHAI 100
SHANGHAI BEIJING 200
Sample Output
100
單源最短路,最長的那條邊可以跳過。枚舉每條邊就行了
#include"stdio.h" #include"string.h" #include"vector" #include"queue" #include"iostream" #include"algorithm" using namespace std; #define N 205 const int inf=(int)1e10; int g[N][N],dis[N]; bool mark[N]; int n,ans; void inti() { int i,j; for(i=0;idis[i]) { min=dis[i]; s=i; } } if(s==0) break; sum+=min; mark[s]=1; if(s==t) break; for(i=0;i dis[s]+g[s][i]) dis[i]=g[s][i]; } } ans=ans