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
————————————————————集训15.6的分割线————————————————————
前言:真是一道好题呀!如果采用最暴力的方法,也是正确解法。(枚举删除哪一条边,跑n次SPFA)但是有一个优秀得多的解法,跑一次就够了。
思路:朴素地跑一次Dijsktra,dis数组的值定义成:该路径去除最大边的权值和。需要一个MAX数组来记录该路径上的最大边。另外还可以采用heap优化的Dijkstra。学习一下。
把dis的值还有该点存入优先队列。每次松弛的时候,该点的dis出队优先级改变,push进去即可。pop某点之后,该点有可能重新进队列,怎样防止点的重复松弛呢?设置一个永久标记,下次出队直接continue即可。
P.S. 还有一种方法,Floyd+DP,这样复杂度提高了但是任意起点和终点之间的最短路都求出来了。
dp[2][N][N],第一维表示有这条边、没有这条边。动态转移方程:
dp[1][i][j] = min(dp[0][i][k] + dp[1][k][j], dp[1][i][k] + dp[0][k][j])
具体可以学习 沐阳 的博客——http://www.cnblogs.com/Lyush/archive/2013/03/10/2952378.html
代码如下:
/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <climits>
#include <iostream>
#define INF 0x3f3f3f3f
using namespace std;
/****************************************/
const int N = 105;
map <string, int> city;
int m, cnt, tot, dis[N], head[N];
bool done[N];
int MAX[N];
struct Edge {
int to, w;
int next;
}edge[N];
struct Heap {
int d, u;
bool operator < (const Heap &tmp) const {
return d > tmp.d;
}//重载'<',维护最小堆
};
int MAP(string &s)
{
if(!city.count(s)) {
city[s] = cnt++;
}
return city[s];
}
void add(int u, int v, int w)
{
edge[tot].to = v; edge[tot].w = w;
edge[tot].next = head[u]; head[u] = tot++;
}
void Dijkstra(int st)
{
memset(done, 0, sizeof(done));
memset(dis, 0x3f, sizeof(dis));
memset(MAX, 0, sizeof(MAX));
dis[st] = 0;
priority_queue <Heap> Q;
Q.push((Heap){0, st});
while(!Q.empty()) {
Heap t = Q.top(); Q.pop();
int u = t.u;
if(done[u]) continue;
done[u] = true;
for(int i = head[u]; i != -1; i = edge[i].next) {
int v = edge[i].to, w = edge[i].w;
int tmp;
if(MAX[u] < w) {
tmp = dis[u] + MAX[u];
MAX[v] = w;
}
else {
tmp = dis[u] + w;
MAX[v] = MAX[u];
}
if(dis[v] > tmp) {
dis[v] = tmp;
Q.push((Heap){tmp, v});
}
}
}
}
int main()
{
#ifdef J_Sure
freopen("000.in", "r", stdin);
// freopen(".out", "w", stdout);
#endif
string s1, s2;
int cnt = 0;
while(cin >> s1 >> s2) {
int st = MAP(s1), ed = MAP(s2);
memset(head, -1, sizeof(head));
tot = 0;
scanf("%d", &m);
int u, v, w;
for(int i = 0; i < m; i++) {
cin >> s1 >> s2;
u = MAP(s1); v = MAP(s2);
scanf("%d", &w);
add(u, v, w);
}
Dijkstra(st);
printf("%d\n", dis[ed]);
}
return 0;
}