zoj 2027 Travelling Fee

本文介绍了一种特殊的最短路径问题解决方法,利用旅行公司提供的优惠策略——即最昂贵的一段行程免费,来计算从起始城市到目标城市的最低成本路径。

Description

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,然后再求一次起点到终点的最短路。至于最长的路是哪一条枚举所有情况就行了。

AC代码:

#include
#include
#include
#include
#include
#include
#include
#include
#define eps 1e-9
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
mapma;
struct edge
{
    int ed,next;
    int val;
}e[20000],tmp[20000];
int m,cnt,st,ed;
int dis[300],que[20000],vis[300],head[300];
void add(int x,int y,int z)
{
    e[cnt].ed=y;
    e[cnt].val=z;
    e[cnt].next=head[x];
    head[x]=cnt++;
}
int spfa(int k)
{
    int i,front=0,rear=0;

    for(i=0;idis[x]+tmp[i].val)
            {
                dis[y]=dis[x]+tmp[i].val;
                if(!vis[y])
                {
                    que[rear++]=y;
                    vis[y]=1;
                }
            }
        }
    }
    return dis[ed];

}
int main()
{
    char s1[200],s2[200];
    int i,num,val;

    while(~scanf("%s%s",s1,s2))
    {
        num=1;cnt=0;
        ma.clear();     //map给各点编号
        memset(head,-1,sizeof(head));
        ma[s1]=num++;ma[s2]=num++;
        st=ma[s1],ed=ma[s2];
        scanf("%d",&m);
        for(i=1;i<=m;i++)
        {
            scanf("%s%s%d",s1,s2,&val);
            if(!ma[s1]) ma[s1]=num++;
            if(!ma[s2]) ma[s2]=num++;
            add(ma[s1],ma[s2],val);
           // printf("%d-> %d  %d\n",ma[s1],ma[s2],val);
        }

        int mi=INF;
        for(i=0;i

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值