码迷,mamicode.com
首页 > 其他好文 > 详细

NOIP 2015 BZOJ 4326 运输计划 (树链剖分+二分)

时间:2015-12-05 12:48:58      阅读:662      评论:0      收藏:0      [点我收藏+]

标签:

题目大意:
给一棵带有边权的树,有m条路径,可以使一条边的权值为0,求最远路径的最小值。
题解:
因为题目的数据点给的很明确 因此可以打n*n的去骗前五十分。另外m=1时可以特判另外打个程序骗60分。
60分程序:
#include<cstdio>
#include<algorithm>
int dep[100001],fa[100001],last[200001],next[200001],e[200001],val[200001],cost[100001],tot,a,b,vv,u[10000],v[10000],n,m,ans[10000];
int ma[3001][3001];
void dfs(int x)
{
    dep[x]=dep[fa[x]]+1;
    for (int i=last[x];i;i=next[i])
    {
        int vv=e[i];
        if (vv==fa[x])continue;
        cost[vv]=val[i];
        fa[vv]=x;
        dfs(vv);
    }
}
void add(int x,int y,int z){next[++tot]=last[x];last[x]=tot;e[tot]=y;val[tot]=z;}
int jisuan(int i,int x,int y)
{
    int tot=0;
    if (dep[x]<dep[y])std::swap(x,y);
    while (dep[x]>dep[y])
    {
        tot+=cost[x];
        ma[x][i]=1;
        x=fa[x];
    }
    while (x!=y)
    {
        tot+=cost[x]+cost[y];
        ma[x][i]=1;ma[y][i]=1;
        x=fa[x];y=fa[y];
    }
    return tot;
}
void work1()//m=1时的情况
{
    int Maxx=0;long long tot=0;
    if (dep[u[1]]<dep[v[1]])std::swap(u[1],v[1]);
    while (dep[u[1]]>dep[v[1]])
    {
        tot+=cost[u[1]];
        Maxx=std::max(Maxx,cost[u[1]]);
        u[1]=fa[u[1]];
    }
    while (u[1]!=v[1])
    {
        tot+=cost[u[1]]+cost[v[1]];
        Maxx=std::max(Maxx,cost[u[1]]);
        Maxx=std::max(Maxx,cost[v[1]]);
        u[1]=fa[u[1]];v[1]=fa[v[1]];
    }
    printf("%lld",tot-Maxx);
}
void work2()//n*n
{
    int Minn=-1;
    for (int i=1;i<=m;i++)ans[i]=jisuan(i,u[i],v[i]),Minn=std::max(Minn,ans[i]);
    for (int i=2;i<=n;i++)
    {
        int Maxx=0;
        for (int j=1;j<=m;j++)
        Maxx=std::max(Maxx,ans[j]-cost[i]*ma[i][j]);
        if (Minn==-1||Minn>Maxx)Minn=Maxx;
    }
    printf("%d",Minn);
}
int main()
{
    freopen("transport.in","r",stdin);
    freopen("transport.out","w",stdout);
    scanf("%d%d",&n,&m);
    for (int i=1;i<=n-1;i++)
    {
        scanf("%d%d%d",&a,&b,&vv);
        add(a,b,vv);
        add(b,a,vv);
    }
    fa[1]=1;
    dfs(1);
    for (int i=1;i<=m;i++)scanf("%d%d",&u[i],&v[i]);
    if (m==1)work1();else work2();
}

 

那么剩下40分呢?
最远的最短显然是二分权值
于是使用树链剖分给每个点打上记号顺便可以求求lca
二分check时
开始时借鉴了LXF队长的思想:用线段树给每个大于mid的路径的都打上标记,假设有s个,然后nlogn释放所有标记,o(n)查找一个点被s条路径经过且最大
然后95分跪于最后一个点 将近2s
所以就想到了差分 既然要释放标记不如直接差分
最后一个点卡时过
优化一:读入优化
优化二:记忆化 同一个s的话所能求到的最大点是一定的
#include<cstdio>
#include<algorithm>
#include<cstring> 
const int maxn=664144;
struct range {
    int x,y,len;
}d[maxn];
bool cmp(range x,range y){return x.len>y.len;}
int root,n,m,x,y,tot,next[maxn],last[maxn],e[maxn],dep[maxn],fa[maxn],size[maxn],son[maxn],top[maxn],pos[maxn];
int pos2[maxn],val[maxn],cost[maxn],MAX[maxn],dist[maxn],s[maxn],node,ans,q[maxn];
int read()//优化之一
{
    int x=0,f=1;char ch=getchar();
    while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();}
    while(ch>=0&&ch<=9){x=x*10+ch-0;ch=getchar();}
    return x*f;
}
void add(int x,int y,int v)
{
    next[++tot]=last[x];
    last[x]=tot;
    val[tot]=v;
    e[tot]=y;
}
void dfs1(int x)
{
    dep[x]=dep[fa[x]]+1;
    size[x]=1;
    for (int i=last[x];i;i=next[i])
    {
        int v=e[i];
        if (v==fa[x])continue;
        fa[v]=x;
        dist[v]=dist[x]+val[i];
        cost[v]=val[i];
        dfs1(v);
        size[x]+=size[v];
        if (size[v]>size[son[x]])son[x]=v;
    }
}
void dfs2(int x,int tp)//树链剖分
{
    top[x]=tp;pos[x]=++node;pos2[node]=cost[x];
    if (son[x]==0)return;
    dfs2(son[x],tp);
    for (int i=last[x];i;i=next[i])
    {
        int v=e[i];
        if (v==fa[x]||v==son[x])continue;
        dfs2(v,v);
    }
}
int query(int x,int y)//lca
{
    int a=x,b=y;
    while (top[a]!=top[b])
    {
        if (dep[top[a]]<dep[top[b]])std::swap(a,b);
        a=fa[top[a]];
    }
    if (dep[a]<dep[b])return a;else return b;
} 
void work(int x,int y)
{
    int a=x,b=y;
    while (top[a]!=top[b])
    {
        if (dep[top[a]]<dep[top[b]])std::swap(a,b);
        s[pos[top[a]]]+=1;s[pos[a]+1]-=1;
        a=fa[top[a]];
    }
    if (a==b)return;
    if (dep[a]>dep[b]) std::swap(a,b);
    s[pos[a]+1]+=1;s[pos[b]+1]-=1;;
} 
int check(int mid)
{
    int sz=0;
    while (d[sz+1].len>mid)sz++;
    if (q[sz]!=0)return q[sz];//优化之一 记忆化
    std::memset(s,0,sizeof(s));
    for (int i=1;i<=sz;i++)work(d[i].x,d[i].y);
    int Maxx=0,tott=0;
    for (int i=1;i<=n;i++){tott+=s[i];if (tott==(sz))Maxx=std::max(Maxx,pos2[i]);}
    q[sz]=Maxx;
    return Maxx;
}
int main()
{
    n=read();m=read();
    for (int i=1;i<n;i++)
    {
        int v;
        x=read();y=read();v=read();
        add(y,x,v);add(x,y,v);
    }
    dfs1(1);
    dfs2(1,1);
    for (int i=1;i<=m;i++)
    {
        d[i].x=read();d[i].y=read();
        d[i].len=dist[d[i].x]+dist[d[i].y]-2*dist[query(d[i].x,d[i].y)];
    }
    std::sort(d+1,d+1+m,cmp);
    int l=0,r=d[1].len;
    while (l<=r)
    {
        int mid=(l+r)>>1;
        if (d[1].len-check(mid)>mid)
        {
            l=mid+1;
        }else r=mid-1,ans=mid;
    }
    printf("%d",ans);
    return 0;    
}

ps:今年怎么都是暴力题

 

NOIP 2015 BZOJ 4326 运输计划 (树链剖分+二分)

标签:

原文地址:http://www.cnblogs.com/wkingG/p/5021197.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!