程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> zoj 2027 Travelling Fee (最短路變形)

zoj 2027 Travelling Fee (最短路變形)

編輯:C++入門知識

zoj 2027 Travelling Fee (最短路變形)


Travelling Fee

Time Limit: 2 Seconds Memory Limit: 65536 KB

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;idis[s]+g[s][i])
                dis[i]=g[s][i];
        }
    }
    ans=ans

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved