ZOJ 2027 Travelling Fee

枚举+最短路

题意是说出发地 和 目的地 之间有一条边是免费的。问你最小费用。

误区:求出最短路-路径中的最大边。(有些其他边免费之后,可能最短路就变了)

正确思路:枚举每条边,将其费用设为0.然后求最短路。找费用最小。

这是无向图,至于地名可以用map映射。

#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<stack>
#include<iostream>
#include<list>
#include<set>
#include<cmath>
#define INF 0x7fffffff
#define eps 1e-6
#define LL long long
using namespace std;
int n,m;
map<string,int>city;
struct lx
{
    int v,len;
};
vector<lx>g[501];
int x,y,ans;

struct edge
{
    int u,v;
};

void SPFA(edge flag)
{
    queue<int>q;
    bool vis[501];
    int dis[501];
    for(int i=0; i<501; i++)
        dis[i]=INF,vis[i]=0;
    dis[x]=0;
    vis[x]=1;
    q.push(x);

    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=0;
        for(int j=0; j<g[u].size(); j++)
        {
            int v=g[u][j].v;
            int len=g[u][j].len;
            if((u==flag.u&&v==flag.v)||(u==flag.v&&v==flag.u))
            {
                len=0;
            }
            if(dis[v]>dis[u]+len)
            {
                dis[v]=dis[u]+len;
                if(!vis[v])
                {
                    vis[v]=1;
                    q.push(v);
                }
            }

        }
    }
    ans=min(ans,dis[y]);
}
int main()
{
    char a[11],b[11];

    while(scanf("%s%s",a,b)!=EOF)
    {
        city.clear();
        for(int i=0; i<501; i++)
            g[i].clear();

        int u,v,len,cot=1;
        x=city[a];
        if(x==0)
        {
            city[a]=cot++;
            x=cot-1;
        }
        y=city[b];
        if(y==0)
        {
            city[b]=cot++;
            y=cot-1;
        }

        scanf("%d",&m);
        queue<edge>que;
        for(int i=0; i<m; i++)
        {
            scanf("%s%s%d",a,b,&len);
            u=city[a];
            if(u==0)
            {
                city[a]=cot++;
                u=cot-1;
            }
            v=city[b];
            if(v==0)
            {
                city[b]=cot++;
                v=cot-1;
            }

            lx now;
            now.len=len;
            now.v=v,g[u].push_back(now);
            now.v=u,g[v].push_back(now);
            edge tmp;

            tmp.u=u,tmp.v=v;
            que.push(tmp);
        }
        ans=INF;
        while(!que.empty())
        {
            edge tmp=que.front();que.pop();
            SPFA(tmp);
        }
        printf("%d\n",ans);
    }
}
时间: 2025-01-31 06:09:36

ZOJ 2027 Travelling Fee的相关文章

zoj 2027 Travelling Fee (最短路变形)

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 ha

ZOJ1027 Travelling Fee(DP+SPFA)

给一张有向无环图,边都有花费,从某点到某点走的那条路径上的那一条花费最多的边可以省掉,问从起点到终点的最少花费的多少, 往DP想的话,就可以写出这个状态dp[u][mx],表示到达u点已经省掉的花费为mx的最少花费. 用SPFA更新转移方程..或者理解成队列+我为人人的转移..其实这题这样子也能解有环图. 看了别人博客,发现还有三种解法: 枚举每一条边作为省掉的边,n次SPFA.这方法简洁,可惜想不出= = 跑Dijkstra,根据记录到每一点时的最长边更新,正确性不懂.. Floyd+DP:加

zoj2027Travelling Fee(Spfa+枚举)

题目链接: huangjing 题意: 给了起始和终点城市,然后给了若干对城市和距离,然后从起点到终点最小的费用,但是有一个新优惠,那就是费用最大的两个城市之间可以免费. 思路: 最开始以为求了最短路然后减去最大的费用即可.但是想了一组样例就知道是错的. 比如1--->2---->3 然后有直接1----->3,那么如果按刚才的思路,那么最小费用就是2 2     5                  8 所以思路是错的..然后在cp的帮助下得知,既然不知道哪条是最大的费用路,那么我们就

(floyd+DP) zoj 3027

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 ha

hdu3001 Travelling 旅行商问题 状态压缩DP

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3001 Travelling Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4642    Accepted Submission(s): 1531 Problem Description After coding so many days,

hdu 3001 Travelling (bfs+状态压缩)

Travelling Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3905    Accepted Submission(s): 1234 Problem Description After coding so many days,Mr Acmer wants to have a good rest.So travelling is

Travelling

Description After coding so many days,Mr Acmer wants to have a good rest.So travelling is the best choice!He has decided to visit n cities(he insists on seeing all the cities!And he does not mind which city being his start station because superman ca

状压DP [HDU 3001] Travelling

Travelling Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4166    Accepted Submission(s): 1339 Problem Description After coding so many days,Mr Acmer wants to have a good rest.So travelling is

hdu3001——Travelling 三进制TSP, 状态压缩

Travelling Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4106    Accepted Submission(s): 1310 Problem Description After coding so many days,Mr Acmer wants to have a good rest.So travelling is