zoukankan      html  css  js  c++  java
  • 1371. Cargo Agency 夜

    http://acm.timus.ru/problem.aspx?space=1&num=1371

    树形DP  不过一遍DFS 就能可以

    需要注意的 是 N=50000 时 需要用 long long  或者 unsigned int

    还有超栈的话 需要 自己可一个较大的栈区

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<vector>
    #include<queue>
    #include<map>
    #include<stack>
    #include<algorithm>
    
    using namespace std;
    #pragma comment(linker,"/STACK:1000000000,1000000000")
    
    #define LL long long
    
    const int INF=0x3f3f3f3f;
    const int N=50005;
    struct node
    {
        int next;
        LL numnode;
        LL selft;
        LL ans;
    }head[N];
    int I;
    struct tt
    {
        int j,next,t;
    }side[N*2];
    void build(int i,int j,int t)
    {
        side[I].j=j;
        side[I].t=t;
        side[I].next=head[i].next;
        head[i].next=I++;
    }
    void dfs(int x,int pre)
    {
        head[x].selft=0;
        head[x].numnode=0;
        head[x].ans=0;
        for(int t=head[x].next;t!=-1;t=side[t].next)
        {
            int l=side[t].j;
            if(l!=pre)
            {
                dfs(l,x);
                LL w=head[l].numnode*(LL)(side[t].t)+head[l].selft;
                head[x].ans+=head[l].ans;
                head[x].ans+=(head[x].numnode*w);
                head[x].ans+=(head[x].selft*head[l].numnode);
                head[x].selft+=w;
                head[x].numnode+=head[l].numnode;
            }
        }
        ++head[x].numnode;
        head[x].ans+=head[x].selft;
    }
    int main()
    {
        //freopen("data.txt","r",stdin);
        unsigned int n;
        while(cin>>n)
        {
            for(unsigned int i=1;i<=n;++i)
            head[i].next=-1;
            I=0;
            for(unsigned int i=1;i<n;++i)
            {
                int l,r,t;
                scanf("%d %d %d",&l,&r,&t);
                build(l,r,t);
                build(r,l,t);
            }
            dfs(1,-1);
            double temp=(n*(n-1)/2);
            printf("%.4lf\n",double(head[1].ans)/temp);
        }
        return 0;
    }
    
  • 相关阅读:
    利用for循环 修改精灵图背景位置
    添加列表项 避免浏览器反复渲染 Fragment
    向元素添加属性名和属性值
    分割文本节点
    查询、回显 基本功能
    获取注释
    合并文本节点
    Node(节点)的4个操作方法
    setTimeout与setInterval
    javascript循环
  • 原文地址:https://www.cnblogs.com/liulangye/p/2714031.html
Copyright © 2011-2022 走看看