bzoj1827[Usaco2010 Mar]gather 奶牛大集会 树上DP
来源:互联网 发布:男穿紧身牛仔裤知乎 编辑:程序博客网 时间:2024/05/22 17:20
。。挺经典的问题了。
在树上找一个点,让其他所有点到达这个点的距离最小。。
一开始并不知道怎么列出dp式。。
然后发现直接贪心就可以了,一开始先算出1的答案,然后一个个往下移动,代价是固定的。
这种想法我自己都想到过这次没想到有点不应该,可能是因为思考时间不够吧。想题有点急躁了,要注意。
#include<cstdio>#include<algorithm>#include<cstring>#include<iostream>#include<vector>#define fo(i,a,b) for(int i=a;i<=b;i++)#define fd(i,a,b) for(int i=a;i>=b;i--)using namespace std;const int N=1e5+5;typedef long long ll;ll ans,c[N],dis[N],size[N];vector<int> e[N],w[N];int n;ll dfs(int x,int fa){ ll ans=dis[x]*c[x]; size[x]=c[x]; for(int i=0;i<e[x].size();i++) { int y=e[x][i],v=w[x][i]; if(y==fa)continue; dis[y]=dis[x]+v; ans+=dfs(y,x); size[x]+=size[y]; } return ans;}void move(int x,int fa){ for(int i=0;i<e[x].size();i++) { int y=e[x][i],v=w[x][i]; if(y==fa)continue; if(size[1]-2*size[y]<0) { ans=ans+(size[1]-2*size[y])*v; move(y,x); } }}int main(){ scanf("%d",&n); fo(i,1,n)scanf("%lld",&c[i]); fo(i,1,n-1) { int u,v,x; scanf("%d%d%d",&u,&v,&x); e[u].push_back(v); e[v].push_back(u); w[u].push_back(x); w[v].push_back(x); } ans=dfs(1,0); move(1,0); printf("%lld\n",ans); return 0;}
0 0
- bzoj1827[Usaco2010 Mar]gather 奶牛大集会 树上DP
- BZOJ1827 [Usaco2010 Mar]gather 奶牛大集会
- BZOJ1827 [Usaco2010 Mar]gather 奶牛大集会
- 【bzoj1827】[Usaco2010 Mar]gather 奶牛大集会 树形dp+贪心
- 【bzoj1827】[Usaco2010 Mar]gather 奶牛大集会 贪心 树规
- [BZOJ1827][Usaco2010 Mar]gather 奶牛大集会(树形dp+维护)
- [BZOJ1827][Usaco2010 Mar]gather 奶牛大集会(树形dp+维护)
- 1827: [Usaco2010 Mar]gather 奶牛大集会
- [树Dfs][Usaco2010 Mar]gather奶牛大集会
- 【BZOJ 1827】 [Usaco2010 Mar]gather 奶牛大集会
- bzoj 1827: [Usaco2010 Mar]gather 奶牛大集会
- BZOJ 1827: [Usaco2010 Mar]gather 奶牛大集会
- bzoj 1827: [Usaco2010 Mar]gather 奶牛大集会
- 【树形DP/搜索】BZOJ 1827: [Usaco2010 Mar]gather 奶牛大集会
- BZOJ1827 奶牛大集会
- 【BZOJ1827】奶牛大集会(树形递推)
- 【奶牛大集会】解题报告
- 树形问题--奶牛大集会
- 909422229_JavaWeb对列表数据整体打包压缩下载
- KMP算法
- [ActiveMQ]消息中间件基本概念及安装
- VS 调试动态库
- 拖动改变Div大小
- bzoj1827[Usaco2010 Mar]gather 奶牛大集会 树上DP
- 程序员技术怀旧_晒晒那些过去的经典
- css元素水平垂直居中的十种方法
- OpenWrt:路由器上的Linux
- 【design pattern】结构型模式之—装饰者模式(Decorator)
- JZOJ 2113——括号问题
- 20170516_C++ 虚函数表解析
- 理解volatile
- 让 ESP32 连接到你的 WiFi 热点