CF 70E Information Reform
来源:互联网 发布:五子棋最强软件 编辑:程序博客网 时间:2024/05/17 23:52
树形DP
因为博主蒟蒻变懒了,以后就只写一些不好想的(丧)题题解,而不会啥题解都写了吧。。。
注意到每一个点都要找一个点和自己对应,并产生代价,于是可以记f[i][j]表示i强制对应j时i子树的最小答案(包括建j的k代价),再记一个best[i]表示j取到best[i]时,f[i][j]最小
f[i][j] = Sigma { min( f[s][j] - k, f[s][best[s]] ) }
这样第一问就做完了233333
考虑第二问,如果以1为根进行DP的话,1的对应点肯定就是best[i],对于一个儿子s,s的对应点要么是best[s]要么是best[i],比较一下即可。
#include<cstdio>#include<cstring>#include<algorithm>#define N 185using namespace std;int f[N][N], best[N], dis[N][N], cost[N], n, k, d[N][N], ans[N];void floyd(){ for(int i = 1; i <= n; i++) dis[i][i] = 0; for(int k = 1; k <= n; k++) for(int i = 1; i <= n; i++) for(int j = 1; j <= n ;j++) dis[i][j] = min(dis[i][j], dis[i][k]+dis[k][j]);}void dp(int x, int fa){ for(int y = 1; y <= n; y++)if(d[x][y]) if(y!=fa)dp(y,x); for(int i = 1, minn = 1<<30; i <= n; i++) { f[x][i] = cost[dis[x][i]] + k; for(int y = 1; y <= n; y++)if(d[x][y]) if(y!=fa)f[x][i] += min(f[y][i]-k, f[y][best[y]]); if(f[x][i] < minn) { minn = f[x][i]; best[x] = i; } }}void make(int x, int fa){ for(int y = 1; y <= n; y++)if(d[x][y]) if(y!=fa) { if(f[y][best[y]] < f[y][ans[x]]-k) ans[y] = best[y]; else ans[y] = ans[x]; make(y,x); }}int main(){ scanf("%d%d",&n,&k); for(int i = 1; i < n; i++) scanf("%d",&cost[i]); memset(dis,63,sizeof(dis)); for(int i = 1, a, b; i < n; i++) { scanf("%d%d",&a,&b); d[a][b] = d[b][a] = true; dis[a][b] = dis[b][a] = 1; } floyd(); dp(1,0); ans[1] = best[1]; make(1,0); printf("%d\n",f[1][best[1]]); for(int i = 1; i <= n; i++) printf("%d ",ans[i]);}
0 0
- CF 70E Information Reform
- cf - #346 E-New Reform
- Codeforces70E Information Reform
- E. One-Way Reform
- [CF 732F]Tourist Reform
- codeforces 659 E New Reform
- codeforces 659E New Reform
- CodeForces 659E New Reform
- codeforce 659E New Reform
- codeforces 659E New Reform
- codeforces 659E New Reform
- CodeForces 659E New Reform
- cf-e
- Codeforces 659E New Reform【DFS】
- Codeforces--659E--New Reform(深搜好题)
- CodeForces 659E New Reform (DFS)
- Codeforces 723E.One-Way Reform
- Codeforces 659E New Reform【思维+Dfs】
- 转:基于http方式的git服务器搭建
- spring多数据源3
- RedisDesktopManager连接不上redis
- Spring Boot Junit单元测试
- 完全理解Python迭代对象、迭代器、生成器
- CF 70E Information Reform
- Java中关于字符串对象一些易错的点
- RunTime交换方法的实现
- xcode 8 widget 真机调试
- array_key_exists() 与 isset() 的区别
- JSP九大内置对象的作用和用法总结
- jar包、模板模式
- Android开发方法数超过64k(65k)解决办法
- 已启动程序,再启动通知原程序显示