ZOJ:2027 Travelling Fee

求起点到终点的某条路径长度减去其中一条边的最小值。

依次删掉每条边,然后求起点到终点的最短路,最小的那个就是答案。

还有就是map要记得清空,我在这里WA了一次。。

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
#include<algorithm>
using namespace std;
map<string,int> mp;
struct Edge
{
    int from,to,cost;
    void set(int a,int b,int c)
    {
        from=a;
        to=b;
        cost=c;
    }
};
int main()
{
    string st,ed;
    while(cin>>st>>ed)
    {
        mp.clear();
        int m,n=0,N=0;
        cin>>m;
        Edge p[205];
        string a,b;
        int w;
        for(int i=0; i<m; ++i)
        {
            cin>>a>>b>>w;
            if(!mp[a]) mp[a]=++n;
            if(!mp[b]) mp[b]=++n;
            int aa=mp[a],bb=mp[b];
            p[N++].set(aa,bb,w);
            p[N++].set(bb,aa,w);
        }
        int dist[105];
        memset(dist,0x7f,sizeof(dist));
        int ans=dist[0];
        int las=mp[ed];
        for(int i=0; i<N; ++i)
        {
            int tmp=p[i].cost;
            p[i].cost=0;
            memset(dist,0x7f,sizeof(dist));
            dist[mp[st]]=0;
            while(1)
            {
                bool update=false;
                for(int j=0; j<N; ++j)
                {
                    int to=p[j].to,from=p[j].from,v=p[j].cost;
                    if(dist[to]>dist[from]+v)
                    {
                        dist[to]=dist[from]+v;
                        update=true;
                    }
                }
                if(!update) break;
            }
            ans=min(dist[las],ans);
            p[i].cost=tmp;
        }
        printf("%d\n",ans);
    }
    return 0;
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值