HDU 5148 Cities
来源:互联网 发布:网络打印机如何设置ip 编辑:程序博客网 时间:2024/05/22 07:44
题意:
n(2000)个点的树 从中选出k(50)个点 要求 选出的点中等概率选出两个点 使得这两点的期望值最小 输出期望值乘k^2
思路:
我们将题目的要求化简 sum( sum( dis(i,j) ) ) / k^2 * k^2 其实就是求 sum( sum( dis(i,j) ) ) 其中i和j为任意选出的k个点
设dp[i][k]表示扫描完i为根的子树选出k个点的最小sum
那么转移方程就是 dp[father][k1+k2] = min( dp[father][k1]+dp[son][k2]+edge*k2*(k-k2)*2 ) 这个方程很好理解 注意式中的k2 我们利用它计算的原因是 son树内之选k2个点
代码书写的时候还要注意 dp过程中k1需要倒着for 这个做过01背包应该很好理解
代码:
#include<cstdio>#include<iostream>#include<cstring>#include<string>#include<algorithm>#include<map>#include<set>#include<vector>#include<queue>#include<cstdlib>#include<ctime>#include<cmath>using namespace std;typedef long long LL;#define N 2010#define M 60inline void scand(int &ret) { char c; ret = 0; while ((c = getchar()) < '0' || c > '9') ; while (c >= '0' && c <= '9') ret = ret * 10 + (c - '0'), c = getchar();}int t, n, m;LL dp[N][M];LL ans;int head[N], tot;struct edge { int v, w, next;} ed[N * 2];void add(int u, int v, int w) { ed[tot].v = v; ed[tot].w = w; ed[tot].next = head[u]; head[u] = tot++;}void update(LL &x, LL y) { if (y == -1) return; if (x == -1 || x > y) x = y;}void dfs(int u, int fa) { dp[u][1] = 0; for (int i = head[u]; ~i; i = ed[i].next) { int v = ed[i].v; if (v != fa) { dfs(v, u); LL w = ed[i].w; for (int k1 = m - 1; k1; k1--) { if (dp[u][k1] == -1) continue; for (int k2 = 1; k2 < m; k2++) { if (k2 + k1 > m || dp[v][k2] == -1) break; update(dp[u][k1 + k2], dp[u][k1] + dp[v][k2] + w * (m - k2) * k2 * 2); } } } } update(ans, dp[u][m]);}int main() { scand(t); while (t--) { scand(n); scand(m); memset(head, -1, sizeof (head)); tot = 0; for (int i = 2; i <= n; i++) { int u, v, w; scand(u); scand(v); scand(w); add(u, v, w); add(v, u, w); } memset(dp, -1, sizeof (dp)); ans = -1; dfs(1, 0); printf("%I64d\n", ans); } return 0;}
0 0
- HDU 5148 Cities
- hdu 5148 Cities dp
- HDU 5148 Cities
- hdu 5148 Cities(树形背包)
- hdu 5148 Cities(树形dp)
- hdu 5148Cities 树形dp
- HDU 5148(Cities-树上背包)
- HDU 5148 Cities 树形DP(背包)
- HDU Connections between cities
- Connections between cities HDU
- Connections between cities HDU
- hdu 3371 Connect the Cities
- Hdu-3371 Connect the Cities
- HDU 3371 Connect the Cities
- HDU-3371-Connect the Cities
- hdu 3371 Connect the Cities
- HDU 3371 Connect the Cities
- HDU 2874 Connections between cities
- 关于抽象类的另一个使用——适配模式
- linux中ftp提示--553 Could not create file
- myself reedit for 2440gdbinit file
- POJ 2484-A Funny Game(对称博弈)
- ios如何判断当前设备名称
- HDU 5148 Cities
- UML中的四大关系
- 可变数组与结构体
- 【.NET机房重构】——泛型集合
- 《数据结构》实验一: VC编程工具的灵活使用
- java面试中常用的排序算法
- 一秒钟Hold住Application.xml
- 设计模式之工厂
- ora-16433的处理思路