zoukankan      html  css  js  c++  java
  • 商务旅行

    商务旅行

    题目描述:
    某首都城市的商人要经常到各城镇去做生意,他们按自己的路线去做,目的是为了更好的节约时间。
    假设有N个城镇,首都编号为1,商人从首都出发,其他各城镇之间都有道路连接,任意两个城镇之间如果有直连道路,在他们之间行驶需要花费单位时间。该国公路网络发达,从首都出发能到达任意一个城镇,并且公路网络不会存在环。
    你的任务是帮助该商人计算一下他的最短旅行时间。
    输入描述:
    输入文件中的第一行有一个整数N,1<=n<=30 000,为城镇的数目。下面N-1行,每行由两个整数a 和b (1<=a, b<=n; a<>b)组成,表示城镇a和城镇b有公路连接。在第N+1行为一个整数M,下面的M行,每行有该商人需要顺次经过的各城镇编号。
    输出描述:
    在输出文件中输出该商人旅行的最短时间。
    样例输入:
    5
    1 2
    1 5
    3 5
    4 5
    4
    1
    3
    2
    5
    样例输出:
    7

    #include<iostream>
    using namespace std;
    const int maxn=30010;
    struct node
    {
        int to;
        int next;
    }e[maxn*2];
    int n,m,ans,tot,f[maxn][30],deep[maxn],head[maxn];
    void edd_edge(int u,int v)
    {
        tot++;
        e[tot].to=v;
        e[tot].next=head[u];
        head[u]=tot;
    }
    void build(int u)
    {
        for(int i=head[u];i;i=e[i].next)
        if(!f[e[i].to][0])
        {
            f[e[i].to][0]=u;
            deep[e[i].to]=deep[u]+1;
            build(e[i].to);
        }
    
    }
    void init()
    {
        for(int j=1;j<=20;j++)
          for(int i=1;i<=n;i++)
          f[i][j]=f[f[i][j-1]][j-1];
    }
    int lca(int a,int b)
    {
        if(deep[a]<deep[b])
        swap(a,b);
        if(deep[a]!=deep[b])
        {
            int d=deep[a]-deep[b];
            for(int i=0;d;i++)
            {
                if(d&1)
                a=f[a][i];
                d>>=1;
            }
        }
        if(a==b)
        return a;
        for(int i=20;i>=0;i--)
        if(f[a][i]!=f[b][i])
        {
            a=f[a][i];
            b=f[b][i];
        }
        return f[a][0];
    }
    int main()
    {
        int x,y;
        cin>>n;
        for(int i=1;i<=n-1;i++)
        {
            cin>>x>>y;
            edd_edge(x,y);
            edd_edge(y,x);
        }
        f[1][0]=1;
        deep[1]=0;
        build(1);
        init();
        cin>>m;
        int start=1,end;
        for(int i=1;i<=m;i++)
        {
            cin>>end;
            int d=lca(start,end);
            ans+=deep[start]+deep[end]-2*deep[d];
            start=end;
        }
        cout<<ans;
        return 0;
    }
  • 相关阅读:
    DataGridView编辑后立即更新到数据库的两种方法
    DataTable 转换成 Json的3种方法
    C# 应用程序配置文件App.Config和web.config
    C#中使用JsonConvert解析JSON
    C#JsonConvert.DeserializeObject反序列化json字符
    WIN10远程桌面、常用命令
    control[控制面板]的参数
    win10企业版变成win10专业版的设置教程
    DLL加密
    微信小程序顶部tab
  • 原文地址:https://www.cnblogs.com/cax1165/p/6071006.html
Copyright © 2011-2022 走看看