题目链接:
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2027
题目:
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
/*
问题 题目本身不难,关键是理解题意,求起始城市到目标城市的最少花费,也即最短路,很容易陷入的误区是先求一条最短路,再找出该条最短路上
最大花费并减去,要明白,这样找的最短路没错,但是减去最大花费之后是不能保证整体花费最小的
所以解题思路是
处理数据成邻接矩阵存储数据;由于免去一条花费最高的边,索性枚举每一条边使其为0,计算m次最短路,得出最小的那一个即可
*/
#include<stdio.h>
#include<string.h>
struct Edge{
char from[],to[];
int cost;
}edge[];
struct City{
char name[];
int num;
}city[];
const int inf=;
int map[][],citynum;
char source[],destin[];
int dis[],vis[],path[];
int startnum,endnum; int ret_citynum(char temp[]);
int Dijkstra();
int main()
{
int m,i,j;
while(scanf("%s%s",source,destin) != EOF)
{
scanf("%d",&m);
for(i=;i<=m;i++){
scanf("%s%s%d",edge[i].from,edge[i].to,&edge[i].cost);
}
citynum=;
memset(map,-,sizeof(map));
for(i=;i<=m;i++){
map[ret_citynum(edge[i].from)][ret_citynum(edge[i].to)]=edge[i].cost;
}
for(i=;i<=citynum;i++){
for(j=;j<=citynum;j++){
if(map[i][j]==-)
map[i][j]=inf;
}
}
/*for(i=1;i<=citynum;i++){
printf("%s编号为%d\n",city[i].name,city[i].num);
}
for(i=1;i<=citynum;i++){
for(j=1;j<=citynum;j++){
printf("%8d",map[i][j]);
}
printf("\n");
}*/
startnum=ret_citynum(source);
endnum=ret_citynum(destin); int ans=inf,temp;
for(i=;i<=m;i++){
map[ret_citynum(edge[i].from)][ret_citynum(edge[i].to)]=;
temp=Dijkstra();
if(temp < ans)
ans = temp;
map[ret_citynum(edge[i].from)][ret_citynum(edge[i].to)]=edge[i].cost;
}
printf("%d\n",ans);
}
}
int Dijkstra()
{
int i,j;
memset(vis,,sizeof(vis));
for(i=;i<=citynum;i++){
if(i != startnum)
dis[i]=inf;
else
dis[startnum]=;
}
for(i=;i<=citynum;i++){
int x,min=inf;
for(j=;j<=citynum;j++){
if(!vis[j] && dis[j] <= min)
min=dis[x=j];
}
vis[x]=;
for(j=;j<=citynum;j++){
if(dis[j] > dis[x] + map[x][j])
dis[j] = dis[x] + map[x][j];
}
}
return dis[endnum];
}
int ret_citynum(char temp[])
{
int i;
for(i=;i<=citynum;i++){
if(strcmp(temp,city[i].name)==)
return i;
} citynum++;
strcpy(city[citynum].name,temp);
city[citynum].num=citynum;
return citynum;
}