Factories Gym - 102222G(2018宁夏邀请赛暨2019银川icpc网络预选赛)
第一场icpc网络赛,出的去年宁夏邀请赛原题。c题还没有读完就有ak的了。。(滑稽)
Byteland has nn cities numbered from 11 to nn, and n−1n−1 bi-directional roads connecting them. For each pair of cities, the residents can arrive one from another one through these roads (which also means the road network in Byteland is a tree).
Ghaliyah, the queen of the land, has decided to construct kk new factories. To avoid contamination, she requires that a factory can locate at a city with only one road (which also means this city is a leaf in the road network). Besides, a city can only have one factory.
You, as the royal designer, are appointed to arrange the construction and meanwhile, minimize the sum of distances between every two factories.
Input
The input contains several test cases, and the first line is a positive integer TT indicating the number of test cases which is up to 103103.
For each test case, the first line contains two integers n (2≤n≤105)n (2≤n≤105) and k (1≤k≤100)k (1≤k≤100) indicating the number of cities in Byteland and the number of new factories which are asked to construct.
Each of the following n−1n−1 lines contains three integers u,v (1≤u,v≤n)u,v (1≤u,v≤n) and w (1≤w≤105)w (1≤w≤105) which describes a road between the city uu and the city vv of length ww.
We guarantee that the number of leaves in the road network is no smaller than kk, and the sum of nn in all test cases is up to 106106.
Output
For each test case, output a line containing Case #x: y, where x is the test case number starting from 11, and y is the minimum sum of distances between every two factories.
Example
Input
2
4 2
1 2 2
1 3 3
1 4 4
4 3
1 2 2
1 3 3
1 4 4
Output
Case #1: 5
Case #2: 18
题意,给一棵树,给出k各点,这k个点只能在叶子节点上,问着k个点到达其余各点的距离和最小值是多少。
树形dp。我们设定dp[x][i]代表着以x为祖先的子树有i个叶子节点的到达其余各点的最小距离。状态转移方程为dp[x][i]=min(dp[x][i],dp[x][i-j]+dp[son][j]+(k-j)*j *w)。其中son代表着x的子节点。注意到,dp[x][i]的转移与dp[x][i-j]相关,因此如果正着循环,可能会出现一个叶子节点多次更新一个父亲的情况。为了避免这个问题,可用常用套路,即利用一个辅助数组存储当前结果,到所有的转移都判断完毕后再重新复制给dp数组本身
代码如下:
努力加油a啊,(o)/~
总结
以上是生活随笔为你收集整理的Factories Gym - 102222G(2018宁夏邀请赛暨2019银川icpc网络预选赛)的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: No Pain No Game HDU
- 下一篇: Continuous Intervals