1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <vector>
#define FILE_IN(__fname) freopen(__fname, "r", stdin) #define FILE_OUT(__fname) freopen(__fname, "w", stdout) #define rap(a,s,t,i) for (int a = s; a <= t; a += i) #define basketball(a,t,s,i) for (int a = t; a > s; a -= i) #define countdown(s) while (s --> 0) #define IMPROVE_IO() std::ios::sync_with_stdio(false)
using std::cin; using std::cout; using std::endl;
typedef long long int lli;
int getint() { int x; scanf("%d", &x); return x; } lli getll() { long long int x; scanf("%lld", &x); return x; }
const int MAXN = 2000 + 10; const int MAXK = 2000 + 10;
struct Edge { int v; lli w; Edge(int v = 0, lli w = 0) : v(v), w(w) {} };
std::vector<Edge> head[MAXN];
int n, m, size[MAXN]; lli dp[MAXN][MAXK]; bool vis[MAXN][MAXK];
void DFS(int root = 1, int father = 0) { size[root] = 1; vis[root][0] = vis[root][1] = true; for (int i = 0, siz = (int) head[root].size(); i < siz; ++i) { int next = head[root][i].v; if (next == father) continue; DFS(next, root); size[root] += size[next]; } for (int i = 0, siz = (int) head[root].size(); i < siz; ++i) { int next = head[root][i].v; if (next == father) continue; lli weight = head[root][i].w; for (int j = std::min(m, size[root]); j >= 0; --j) { int up = std::min(j, size[next]); for (int k = 0; k <= up; ++k) { if (!vis[root][j - k]) continue; lli val = k * (m - k) * weight + (size[next] - k) * ((n - m) - (size[next] - k)) * weight; dp[root][j] = std::max(dp[root][j], dp[root][j - k] + dp[next][k] + val); vis[root][j] = true; } } } }
int main() { n = getint(); m = getint(); rap (i, 1, n - 1, 1) { int prev = getint(), next = getint(), weight = getint(); head[prev].push_back(Edge(next, weight)); head[next].push_back(Edge(prev, weight)); } DFS(); printf("%lld\n", dp[1][m]); return 0; }
|