zoj 2027 Travelling Fee 求a->b的最少花费(去除路上的最大花费后的费用)

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


 

 //

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
const int inf=(1<<28);
map<string,int> q;
int n;
int a[110][110];//a[i][j]表示从i到j的最少花费(减去p[i][j]后的)
int p[110][110];//p[i][j]表示i->j最少花费路上的最大花费
char st[110],ed[110];
char str1[100],str2[100];
int main()
{
    while(scanf("%s%s",st,ed)==2)
    {
        int e;scanf("%d",&e);
        n=0;
        q.clear();
        memset(a,-1,sizeof(a));
        while(e--)
        {
            int w;
            scanf("%s%s%d",str1,str2,&w);
            if(q[str1]==0) q[str1]=++n;
            if(q[str2]==0) q[str2]=++n;
            a[q[str1]][q[str2]]=w;
        }
        for(int i=1;i<=n;i++)
        {
            a[i][i]=0;
            for(int j=1;j<=n;j++)
            {
                if(a[i][j]!=-1) p[i][j]=a[i][j],a[i][j]=0;
            }
        }
        for(int k=1;k<=n;k++)
        {
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=n;j++)
                {
                    if(a[i][k]!=-1&&a[k][j]!=-1)
                    {
                        int tmp=a[i][k]+a[k][j]+min(p[i][k],p[k][j]);
                        if(a[i][j]==-1)
                        {
                            a[i][j]=tmp;
                            p[i][j]=max(p[i][k],p[k][j]);
                        }
                        else if(a[i][j]>tmp)
                        {
                            a[i][j]=tmp;
                            p[i][j]=max(p[i][k],p[k][j]);
                        }
                    }
                }
            }
        }
        printf("%d\n",a[q[st]][q[ed]]);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值