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 }
相關文章
相關標籤/搜索