zoukankan      html  css  js  c++  java
  • BZOJ4890 & 洛谷3761:[TJOI2017]城市——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=4890

    https://www.luogu.org/problemnew/show/P3761

    从加里敦大学城市规划专业毕业的小明来到了一个地区城市规划局工作。这个地区一共有n座城市,n-1条高速公路,保证了任意两运城市之间都可以通过高速公路相互可达,但是通过一条高速公路需要收取一定的交通费用。

    小明对这个地区深入研究后,觉得这个地区的交通费用太贵。小明想彻底改造这个地区,但是由于上司给他的资源有限,因而小明现在只能对一条高速公路进行改造,改造的方式就是去掉一条高速公路,并且重新修建一条一样的高速公路(即交通费用一样),使得这个地区的两个城市之间的最大交通费用最小(即使得交通费用最大的两座城市之间的交通费用最小),并且保证修建完之后任意两座城市相互可达。如果你是小明,你怎么解决这个问题?

    前置技能:树直径,树半径(代码的树半径是我自己yy的请无视orz)

    复杂度显然是$O(n^2)$的,这就使我们支持枚举每条边求出答案。

    设拆完路之后的两棵树为$A,B$,那么就有三种情况:

    最长路在$A$中

    最长路在$B$中

    最长路端点分别在$A,B$中

    前两者求树直径即可,至于最后一个我们就需要考虑一个问题,到底要把边加在哪里才能使这种情况的长度最小。

    显然是要加在树半径最小的两个点上(懒得证了)。

    于是切了(顺便复习了怎么求树直径和半径,我觉得我多半是废了基本技能都不会了。)

    #include<map>
    #include<cmath>
    #include<stack>
    #include<queue>
    #include<cstdio>
    #include<cctype>
    #include<vector>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    const int N=5005;
    const int INF=1e9;
    inline int read(){
        int X=0,w=0;char ch=0;
        while(!isdigit(ch)){w|=ch=='-';ch=getchar();}
        while(isdigit(ch))X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
        return w?-X:X;
    }
    struct node{
        int w,to,nxt;
    }e[N*2];
    int n,cnt,head[N],f[N][3];
    bool vis[N];
    inline void add(int u,int v,int w){
        e[++cnt].to=v;e[cnt].w=w;e[cnt].nxt=head[u];head[u]=cnt;
    }
    int ans;
    void dfs1(int u,int fa){
        for(int i=head[u];i;i=e[i].nxt){
            int v=e[i].to,w=e[i].w;
            if(vis[v]||fa==v)continue;
            dfs1(v,u);
            if(f[u][0]<f[v][0]+w){
                f[u][1]=f[u][0];
                f[u][0]=f[v][0]+w;
            }else if(f[u][1]<f[v][0]+w)
                f[u][1]=f[v][0]+w;
        }
        ans=max(ans,f[u][0]+f[u][1]);
    }
    void dfs2(int u,int fa){
        for(int i=head[u];i;i=e[i].nxt){
            int v=e[i].to,w=e[i].w;
            if(vis[v]||fa==v)continue;
            f[v][2]=f[u][2]+w;
            if(f[u][0]-w!=f[v][0])f[v][2]=max(f[v][2],f[u][0]+w);
            else f[v][2]=max(f[v][2],f[u][1]+w);
            dfs2(v,u);
        }
        ans=min(ans,max(f[u][0],f[u][2]));
    }
    int main(){
        n=read();
        for(int i=1;i<n;i++){
            int u=read(),v=read(),w=read();
            add(u,v,w);add(v,u,w);
        }
        int sum=INF;
        for(int i=1;i<=cnt;i+=2){
            int u=e[i].to,v=e[i+1].to,w=e[i].w;
            int w1,w2,tmp=0;
            vis[v]=1;
            memset(f,0,sizeof(f));
            ans=0;dfs1(u,0);tmp=max(tmp,ans);
            ans=INF;dfs2(u,0);w1=ans;
            vis[v]=0;vis[u]=1;
            ans=0;dfs1(v,0);tmp=max(tmp,ans);
            ans=INF;dfs2(v,0);w2=ans;
            vis[u]=0;
            tmp=max(tmp,w1+w2+w);
            sum=min(sum,tmp);
        }
        printf("%d
    ",sum);
        return 0;
    }

    +++++++++++++++++++++++++++++++++++++++++++

    +本文作者:luyouqi233。               +

    +欢迎访问我的博客:http://www.cnblogs.com/luyouqi233/+

    +++++++++++++++++++++++++++++++++++++++++++

  • 相关阅读:
    random模块的讲解
    函数的商城代码练习
    python文件作业
    函数的学习
    三元表达式和列表生成式
    jQuery 遍历方法
    CSS font属性综合写法
    JQuery 添加节点
    Bootstrap 响应式中的兼容
    jQuery 中的attr和prop的区别
  • 原文地址:https://www.cnblogs.com/luyouqi233/p/9194825.html
Copyright © 2011-2022 走看看