ZOJ Problem Set - 2027

来源:互联网 发布:武汉富士康2018java 编辑:程序博客网 时间:2024/06/09 03:52
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


修改路径上的一条最大边权为0的最短路。很显然是dp,也就是floyd的算法上加点附加信息。
每次加入点k,最优解一定是adj[i][k]+adj[k][j]+min(maxx[i][k],maxx[k][j]),也就是去掉
max(maxx[i][k],maxx[k][j])这条边,否则如果还有更优解,那么adj[i][k]和adj[k][j]一定有一个不是最优解,矛盾。所以只要维护maxx[i][j]即可。注意初始化为inf。

代码:
#include<cstdio>#include<iostream>#include<cstring>#include<map>using namespace std;string src,dst,f1,f2;map<string,int> mp;const int inf=0x3f3f3f3f;int adj[210][210],maxx[210][210];int main(){    int n,w;    while(cin>>src>>dst){        memset(adj,0x3f,sizeof adj);        memset(maxx,0x3f,sizeof maxx);        mp.clear();        scanf("%d",&n);        int tot=0;        for(int i=0;i<n;i++){            cin>>f1>>f2>>w;            if(!mp.count(f1)) mp[f1]=tot++;            if(!mp.count(f2)) mp[f2]=tot++;            maxx[mp[f1]][mp[f2]]=w;            adj[mp[f1]][mp[f2]]=0;        }        int t1=mp[src],t2=mp[dst];        int ans=adj[t1][t2];        for(int k=0;k<tot;k++)            for(int i=0;i<tot;i++)                for(int j=0;j<tot;j++){                    int tmp=max(maxx[i][k],maxx[k][j]);                    if(tmp==inf) continue;                    if(maxx[i][k]>maxx[k][j]) maxx[i][j]=maxx[i][k];                    else maxx[i][j]=maxx[k][j];                    tmp=maxx[i][k]+maxx[k][j]-maxx[i][j];                    adj[i][j]=min(adj[i][j],adj[i][k]+adj[k][j]+tmp);                    if(i==t1&&j==t2) ans=min(ans,adj[i][j]);                }        printf("%d\n",ans);    }return 0;}



0 0
原创粉丝点击