水库 (树形dp)

R国有n座城市和n-1条长度为1的双向道路,每条双向道路连接两座城市,城市之间均相互连通。现在你需要维护R国的供水系统。你可以在一些城市修建水库,在第i个城市修建水库需要每年c_i的维护费用。对于没有修建水库的城市,如果离它最近的水库的距离为d,那么需要每年t_i的运输费用来保证该城市的用水需求。保证t_i严格递增。你的任务是计算出每年所需要的最小花费。对于10%的数据,\(n<=5\)。对于30%的数据,\(n<=20\)。对于另外40%的数据,\(t_i=i\)。对于100%的数据,\(n<=1000\),\(c_i,t_i<=100000\)。

这可能算是我做的第一道树形dp?i表示以i为根的子树,j表示i的供水依赖于j。k为i的子节点。\(dp[i][j]=t[dis[now][j]]+c[j]+\sum min(dp[k][j]-c[j],best[k])\)。也就是说对于i的子树,要么i和k共用一个水库,要么用的水库不一样。如果是共用水库,说明水库不用重复建,那么建造水库的成本可以省掉。

但是我在思考过程中发现这样一种情况:

这个情况。。hjq大神说可以证明不存在。因为既然j也选了,t也选了,i一定是哪个更近选哪个。所以i一定不会选j。(被自己蠢哭了)

#include <cstdio>
#include <algorithm> const int maxn=1005, INF=1e9; class Graph{
public:
struct Edge{
int to, next; Graph *belong;
void set(int x, int y, Graph *g){
to=x; next=y; belong=g; }
Edge& operator ++(){
return *this=belong->edge[next]; }
inline int operator *(){ return to; }
};
void addedge(int x, int y){
edge[++cntedge].set(y, fir[x], this);
fir[x]=cntedge;
}
inline Edge& getlink(int x){ return edge[fir[x]]; }
private:
int cntedge, fir[maxn];
Edge edge[maxn*2];
}; int n, c[maxn], t[maxn];
int f[maxn][maxn], best[maxn];
int dis[maxn][maxn];
Graph g; void get_dis(int now, int step, int source, int pre){
dis[now][source]=dis[source][now]=step;
Graph::Edge e=g.getlink(now);
for (; *e; ++e) if (*e!=pre)
get_dis(*e, step+1, source, now);
} void dfs(int now, int par){
Graph::Edge e=g.getlink(now);
for (int j=1; j<=n; ++j) f[now][j]=t[dis[now][j]]+c[j];
for (; *e; ++e){
if (*e!=par) dfs(*e, now);
else continue;
for (int j=1; j<=n; ++j)
f[now][j]+=std::min(f[*e][j]-c[j], best[*e]);
}
for (int j=1; j<=n; ++j)
best[now]=std::min(best[now], f[now][j]);
} int main(){
scanf("%d", &n);
for (int i=1; i<=n; ++i) scanf("%d", &c[i]);
for (int i=1; i<=n; ++i) scanf("%d", &t[i]);
int x, y;
for (int i=1; i<n; ++i){
scanf("%d%d", &x, &y);
g.addedge(x, y); g.addedge(y, x);
}
for (int i=1; i<=n; ++i) get_dis(i, 0, i, 0);
std::fill(best, best+maxn, INF);
dfs(1, 0);
printf("%d\n", best[1]);
return 0;
}

水库(树形dp)的更多相关文章

  1. Fire (poj 2152 树形dp)

    Fire (poj 2152 树形dp) 给定一棵n个结点的树(1<n<=1000).现在要选择某些点,使得整棵树都被覆盖到.当选择第i个点的时候,可以覆盖和它距离在d[i]之内的结点,同 ...

  2. poj3417 LCA + 树形dp

    Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4478   Accepted: 1292 Descripti ...

  3. COGS 2532. [HZOI 2016]树之美 树形dp

    可以发现这道题的数据范围有些奇怪,为毛n辣么大,而k只有10 我们从树形dp的角度来考虑这个问题. 如果我们设f[x][k]表示与x距离为k的点的数量,那么我们可以O(1)回答一个询问 可是这样的话d ...

  4. 【BZOJ-4726】Sabota? 树形DP

    4726: [POI2017]Sabota? Time Limit: 20 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 128  Solved ...

  5. 树形DP+DFS序+树状数组 HDOJ 5293 Tree chain problem(树链问题)

    题目链接 题意: 有n个点的一棵树.其中树上有m条已知的链,每条链有一个权值.从中选出任意个不相交的链使得链的权值和最大. 思路: 树形DP.设dp[i]表示i的子树下的最优权值和,sum[i]表示不 ...

  6. 树形DP

    切题ing!!!!! HDU  2196 Anniversary party 经典树形DP,以前写的太搓了,终于学会简单写法了.... #include <iostream> #inclu ...

  7. BZOJ 2286 消耗战 (虚树+树形DP)

    给出一个n节点的无向树,每条边都有一个边权,给出m个询问,每个询问询问ki个点,问切掉一些边后使得这些顶点无法与顶点1连接.最少的边权和是多少.(n<=250000,sigma(ki)<= ...

  8. POJ2342 树形dp

    原题:http://poj.org/problem?id=2342 树形dp入门题. 我们让dp[i][0]表示第i个人不去,dp[i][1]表示第i个人去 ,根据题意我们可以很容易的得到如下递推公式 ...

  9. hdu1561 The more, The Better (树形dp+背包)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1561 思路:树形dp+01背包 //看注释可以懂 用vector建树更简单. 代码: #i ...

随机推荐

  1. 未测试 Delphi读写UTF-8、Unicode格式文本文件

    // UTF-8文件写入函数 procedure SaveUTFFile(const FileName: string; S: string; WriteHeader: Boolean = True) ...

  2. c++能过,g++过不了

    可能原因: 1.  输出double类型数据时,不能用%lf,应该用%f(详见 关于输出用%lf和%f的问题 ) double n=100; 代码1:错误 c++--------accepted g+ ...

  3. html设置编码

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

  4. (转)python调取C/C++的dll生成方法

    本文针对Windows平台下,python调取C/C++的dll文件. 1.如果使用C语言,代码如下,文件名为test.c. __declspec(dllexport) int sum(int a,i ...

  5. jspsmartupload 文件上传让input数据和文件上传同时提交

    一.使用原因: 文件上传时,表单的属性中必须要有multipart/form-data,如以下例子: <form name="form_post" class="a ...

  6. linux 故障:df -h统计磁盘空间占用太多,但又du -h找不到大的文件

    用lsof / | grep -i delete 从根目录定位打开的被删除的文件 如果定位到某文件占用空间很大 主要是因为我们在删除这个日志文件的时候是用rm -rf *.log这样的命令删除的,删除 ...

  7. opensource mcu

    1 OpenVCS - Open Source Video Conferencing Server it is used as Multipoint Control Unit (MCU) manage ...

  8. 如何生成HLS协议的M3U8文件

    什么是HLS协议: HLS(Http Live Streaming)是由Apple公司定义的用于实时流传输的协议,HLS基于HTTP协议实现,传输内容包括两部分,一是M3U8描述文件,二是TS媒体文件 ...

  9. bzoj 3752: Hack 预处理+暴力dfs

    题目大意: 定义字符串的hash值\(h = \sum_{i=0}^{n-1}p^{n-i-1}s_i\) 现在给定K个长度不超过L的字符串S,对于每个字符串S,求字典序最小长度不超过L的字符串T使得 ...

  10. for循环中的条件执行循序

    问题: public class Main { public static void main(String[] args) { int i,n,length = 0; for(i=1;length& ...