Travelling Fee(Dijlstra——最短路问题变型)

题目连接:less

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2027this

题目:url

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.spa

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..net


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. code


Output

For each test case, output the minimum cost in a single line.blog


Sample Inputci

HANGZHOU BEIJING
2
HANGZHOU SHANGHAI 100
SHANGHAI BEIJING 200get


Sample Outputinput

100

 1 /*
 2 问题 题目自己不难,关键是理解题意,求起始城市到目标城市的最少花费,也即最短路,很容易陷入的误区是先求一条最短路,再找出该条最短路上  3 最大花费并减去,要明白,这样找的最短路没错,可是减去最大花费以后是不能保证总体花费最小的  4 因此解题思路是  5 处理数据成邻接矩阵存储数据;因为免去一条花费最高的边,索性枚举每一条边使其为0,计算m次最短路,得出最小的那一个便可  6 */ 
 7 #include<stdio.h>
 8 #include<string.h>
 9 struct Edge{  10     char from[15],to[15];  11     int cost;  12 }edge[110];  13 struct City{  14     char name[15];  15     int num;  16 }city[220];  17 const int inf=99999999;  18 int map[110][110],citynum;  19 char source[15],destin[15];  20 int dis[110],vis[110],path[110];  21 int startnum,endnum;  22 
 23 int ret_citynum(char temp[]);  24 int Dijkstra();  25 int main()  26 {  27     int m,i,j;  28     while(scanf("%s%s",source,destin) != EOF)  29  {  30         scanf("%d",&m);  31         for(i=1;i<=m;i++){  32             scanf("%s%s%d",edge[i].from,edge[i].to,&edge[i].cost);  33  }  34         citynum=0;  35         memset(map,-1,sizeof(map));  36         for(i=1;i<=m;i++){  37             map[ret_citynum(edge[i].from)][ret_citynum(edge[i].to)]=edge[i].cost;  38  }  39         for(i=1;i<=citynum;i++){  40             for(j=1;j<=citynum;j++){  41                 if(map[i][j]==-1)  42                     map[i][j]=inf;  43  }  44  }  45         /*for(i=1;i<=citynum;i++){  46  printf("%s编号为%d\n",city[i].name,city[i].num);  47  }  48  for(i=1;i<=citynum;i++){  49  for(j=1;j<=citynum;j++){  50  printf("%8d",map[i][j]);  51  }  52  printf("\n");  53  }*/
 54         startnum=ret_citynum(source);  55         endnum=ret_citynum(destin);  56         
 57         int ans=inf,temp;  58         for(i=1;i<=m;i++){  59             map[ret_citynum(edge[i].from)][ret_citynum(edge[i].to)]=0;  60             temp=Dijkstra();  61             if(temp < ans)  62                 ans = temp;  63             map[ret_citynum(edge[i].from)][ret_citynum(edge[i].to)]=edge[i].cost;  64  }  65         printf("%d\n",ans);  66  }  67 }  68 int Dijkstra()  69 {  70     int i,j;  71     memset(vis,0,sizeof(vis));  72     for(i=1;i<=citynum;i++){  73         if(i != startnum)  74             dis[i]=inf;  75         else
 76             dis[startnum]=0;  77  }  78     for(i=1;i<=citynum;i++){  79         int x,min=inf;  80         for(j=1;j<=citynum;j++){  81             if(!vis[j] && dis[j] <= min)  82                 min=dis[x=j];  83  }  84         vis[x]=1;  85         for(j=1;j<=citynum;j++){  86             if(dis[j] > dis[x] + map[x][j])  87                 dis[j] = dis[x] + map[x][j];  88  }  89  }  90     return dis[endnum];  91 }  92 int ret_citynum(char temp[])  93 {  94     int i;  95     for(i=1;i<=citynum;i++){  96         if(strcmp(temp,city[i].name)==0)  97             return i;  98  }  99     
100     citynum++; 101  strcpy(city[citynum].name,temp); 102     city[citynum].num=citynum; 103     return citynum; 104 }
相关文章
相关标签/搜索