题意:给你一棵树,让你求一点,使该点到其余各点的距离之和最小。如果这样的点有多个,则按升序依次输出。

树型dp

#include <cstdio>
#include <cstring>
#include <vector>
#include <set>
using namespace std;
const int maxn=50010;
typedef __int64 LL;
vector<int>tree[maxn];// to save the relation
LL f[maxn],g[maxn],dp[maxn];//f[u] u as root to all his sons' distance g[u]the number of u'sons
set<int> myqueue;
void dfs(int u,int pa){
/* 以1为根,所有的子树到他们的子节点的和 */
    if(tree[u].size() == 1 && u != 1){
        g[u]=1;
        f[u]=0;
        return;
    }
    for(int v=0;v < tree[u].size();v++){
        if(tree[u][v]!=pa){
            dfs(tree[u][v],u);
            g[u]+=g[tree[u][v]];//the son's sons' number
            f[u]+=f[tree[u][v]]+g[tree[u][v]];
        }
    }
    g[u]++;// himself
}
void dfs2(int u,int pa){// to sum the way from his father
//再考虑从父节点来的
    if(tree[u].size()==1 && u!=1){
        dp[u]=dp[pa]+g[1]-(g[u]<<1);
        return;
    }
    for(int v=0;v<tree[u].size();v++){
        if(tree[u][v]!= pa){
//到某节点的距离的和=该节点子树的距离和(在dfs1中获得)+从父亲那一支子树获得的和(此时,父节点那一支看成子树)。dp[儿子]=dp[父节点]-dp[儿子]-g[儿子](儿子到父节点这条路被减了子树的子节点数的次数)    + dp[儿子]+(g[1]-g[儿子])(父树上的所有节点)
            dp[tree[u][v]]=dp[u]+g[1]-(g[tree[u][v]]<<1);
            dfs2(tree[u][v],u);//先算了之后再跑子树
        }
    }
}
int main(){
    int t;
    int n,I,R,a,b;
    scanf("%d",&t);
    LL mmin;
    while(t--){
        scanf("%d%d%d",&n,&I,&R);

        for(int i=0;i<=n;i++){
            tree[i].clear();
        }
        for(int i=2;i<=n;i++){
            scanf("%d%d",&a,&b);
            tree[a].push_back(b);
            tree[b].push_back(a);
        }
//        for(int i=0;i<=n;i++){
//            for(int j=0;j<tree[i].size();j++){
//                printf("%d ",tree[i][j]);
//            }
//            printf("\n");
//        }
        memset(f,0,sizeof(f));
        memset(g,0,sizeof(g));
        dfs(1,-1);
        dp[1]=f[1];
        dfs2(1,-1);
        mmin=dp[1];
        myqueue.clear();
        myqueue.insert(1);
        for(int i=2;i<=n;i++){
            if(dp[i]<mmin){
                mmin=dp[i];
                myqueue.clear();
                myqueue.insert(i);
            }
            if(dp[i]==mmin) myqueue.insert(i);
        }
        //warning : output long long should be I64d
        printf("%I64d\n",I*I*R*mmin);//use set not num but the op
            for(set<int>::iterator it=myqueue.begin();it!=myqueue.end();++it){
                printf("%d ",*it);
            }
        printf("\n\n");
    }
}

  

Power Station POJ 4045的更多相关文章

  1. Power Network (poj 1459 网络流)

    Language: Default Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 23407   ...

  2. Power Network - poj 1459 (最大流 Edmonds-Karp算法)

      Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 24788   Accepted: 12922 Description A ...

  3. Building a Space Station POJ - 2031

    Building a Space Station POJ - 2031 You are a member of the space station engineering team, and are ...

  4. Power Strings POJ - 2406

    Power Strings POJ - 2406 时限: 3000MS   内存: 65536KB   64位IO格式: %I64d & %I64u 提交 状态 已开启划词翻译 问题描述 Gi ...

  5. poj - 4045 - Power Station

    题意:一棵有n个结点的树,要取其中的一个结点,使得该结点到其他所有结点的距离和dis最小,即损耗I * I * R * dis最小,输出最小损耗和该结点(有多个的话按结点编号从小到大输出)(3 < ...

  6. ( KMP 求循环节的个数)Power Strings -- poj -- 2406

    链接: http://poj.org/problem?id=2406 Power Strings Time Limit:3000MS     Memory Limit:65536KB     64bi ...

  7. (最小生成树) Building a Space Station -- POJ -- 2031

    链接: http://poj.org/problem?id=2031 Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6011 ...

  8. Building a Space Station POJ 2031 【最小生成树 prim】

    http://poj.org/problem?id=2031 Description You are a member of the space station engineering team, a ...

  9. Power Strings (poj 2406 KMP)

    Language: Default Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 33205   ...

随机推荐

  1. sphinx 占用大量内存

    http://www.coreseek.com/forum/2_1847_0.html(转) 刚开始没改下面2句时,内存占用比较多,在生成index的时候就占用了! 下面是解决方法: 每个索引中写上 ...

  2. nginx gzip filter模块分析

    API:http://refspecs.linuxbase.org/LSB_3.1.1/LSB-Core-generic/LSB-Core-generic/zlib-deflateinit2.html ...

  3. php字符串首字母转换大小写的实例分享

    php中对字符串首字母进行大小写转换的例子. in: 后端程序首字母变大写:ucwords() <?php $foo = 'hello world!'; $foo = ucwords($foo) ...

  4. 增加p()函数,方便开发中对变量打印调试

    在开发的过程中,我们经常要输出一下变量看看得到的结果是什么,我们就要根据变量的类型选择使用echo,print_r或者var_dump,brophp框架中的p()函数会自动根据变量类型选择用什么方法为 ...

  5. shell 实现类似php的require_once函数

    config.sh #/bin/bash require_once() { #File the true path ,To prevent a symbolic link local realpath ...

  6. 【NHibernate】应用层面需要掌握的知识汇总

    休息接待区 欢迎加入NHibernate中文社区!在讨论中寻找乐趣!在问题中寻找答案! 旅途站点路线 第一站:熟悉NHibernate NHibernate之旅(1):开篇有益 第二站:接触NHibe ...

  7. Lua基础之coroutine(协程)

    概括:1.创建协程2.coroutine的函数3.coroutine的基本流程4.yield对coroutine流程的干预5.resume, function()以及yield之间的参数传递和返回值传 ...

  8. android应用activity中调出输入法后界面调整问题的解决

    在自己写的一个小应用中发现一个问题,当调出输入法后界面最下方的一个按钮被挤到了输入法的上面,这样很不美观,所以找了一下解决办法记录如下: 在AndroidManifest.xml文件中找到对应的act ...

  9. Android Studio 单刷《第一行代码》系列 03 —— Activity 基础

    前情提要(Previously) 本系列将使用 Android Studio 将<第一行代码>(书中讲解案例使用Eclipse)刷一遍,旨在为想入坑 Android 开发,并选择 Andr ...

  10. 用CSS截断字符串的两种实用方法

    方法一: 复制代码 代码如下: <div style="width:300px; overflow:hidden; text-overflow:ellipsis; white-spac ...