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

BZOJ 2435 道路修建 NOI2011 树形DP

时间:2016-01-11 22:13:30      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:

  一看到这道题觉得很,打了递归树形DP后RE了一组,后来发现必须非递归(BFS)

递归版本84分:

技术分享
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int N,point[1000003],next[2000003],v[2000003],c[2000003],cnt=0,f[1000003];
bool p[1000003];
long long sum=0;
void insect(int x,int y,int z){next[cnt]=point[x];point[x]=cnt;v[cnt]=y;c[cnt]=z;cnt++;}
void dfs(int x)
{
    int i;
    f[x]=1;
    for (i=point[x];i!=-1;i=next[i])
     if (p[v[i]]==0)
         {
             p[v[i]]=1;
            dfs(v[i]);
            f[x]+=f[v[i]];
            sum+=(1LL*c[i]*abs(2*f[v[i]]-N));
        }
}
int main()
{
    int i,x,y,z;
    memset(point,-1,sizeof(point));
    memset(next,-1,sizeof(next));
    memset(v,0,sizeof(v));
    memset(p,0,sizeof(p));
    memset(c,0,sizeof(c));
    memset(f,0,sizeof(0));
    scanf("%d\n",&N);
    for (i=1;i<N;++i)
    {
        scanf("%d %d %d\n",&x,&y,&z);
        insect(x,y,z); insect(y,x,z);
    }p[1]=1;dfs(1);
    printf("%lld\n",sum);
    return 0;
}
View Code

BFS版本AC:

#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int N,point[1000003],next[2000003],v[2000003],c[2000003],cnt=0,f[1000003],s[1000003],fa[1000003];
int table[1000003],num=0,du[1000003];
bool p[1000003];
long long sum=0;
void insect(int x,int y,int z){next[cnt]=point[x];point[x]=cnt;v[cnt]=y;c[cnt]=z;cnt++;}
void dfs(int x)
{
    int now,i,tmp=0;
    queue<int>q;
    for (i=1;i<=N;++i)
    {
        if (du[i]==1)
         q.push(i),f[i]=1;
        f[i]=1;
    }
    while (!q.empty())
    {
        now=q.front(); q.pop(); p[now]=1; tmp=now;
        for (i=point[now];i!=-1;i=next[i])
         if (p[v[i]]==0)
         {
             f[v[i]]+=f[now];
             du[v[i]]--;
             if (du[v[i]]==1)
             q.push(v[i]);
         }
    }
    memset(p,0,sizeof(p));
    p[tmp]=1; q.push(tmp);
    while (!q.empty())
    {
        now=q.front(); q.pop();
        for (i=point[now];i!=-1;i=next[i])
         if (p[v[i]]==0)
         {
             p[v[i]]=1;
             sum+=(1LL*c[i]*abs(2*f[v[i]]-N));
             q.push(v[i]);
         }
    }
    printf("%lld\n",sum);
}
int main()
{
    int i,x,y,z;
    memset(point,-1,sizeof(point));
    memset(next,-1,sizeof(next));
    memset(du,0,sizeof(du));
    memset(v,0,sizeof(v));
    memset(p,0,sizeof(p));
    memset(c,0,sizeof(c));
    memset(f,0,sizeof(0));
    scanf("%d\n",&N);
    for (i=1;i<N;++i)
    {
        scanf("%d %d %d\n",&x,&y,&z); du[x]++; du[y]++;
        insect(x,y,z); insect(y,x,z);
    }dfs(1);
    return 0;
}

 

BZOJ 2435 道路修建 NOI2011 树形DP

标签:

原文地址:http://www.cnblogs.com/abclzr/p/5122499.html

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