首页 > 编程知识 正文

hdu 多校 RXD and dividing

时间:2023-05-05 15:42:13 阅读:223030 作者:2787

Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 22 Accepted Submission(s): 6

Problem Description Define f(S) as the the cost of the minimal Steiner Tree of the set S on tree T.
he wants to divide 2,3,4,5,6,…n into k parts S1,S2,S3,…Sk,
where ⋃Si={2,3,…,n} and for all different i,j , we can conclude that Si⋂Sj=∅.
Then he calulates res=∑ki=1f({1}⋃Si).
He wants to maximize the res.
1≤k≤n≤106
the cost of each edge∈[1,105]
Si might be empty.
f(S) means that you need to choose a couple of edges on the tree to make all the points in S connected, and you need to minimize the sum of the cost of these edges. f(S) is equal to the minimal cost

Input
There are several test cases, please keep reading until EOF.
For each test case, the first line consists of 2 integer n,k, which means the number of the tree nodes , and k means the number of parts.
The next n−1 lines consists of 2 integers, a,b,c, means a tree edge (a,b) with cost c.
It is guaranteed that the edges would form a tree.
There are 4 big test cases and 50 small test cases.
small test case means n≤100.

Output
For each test case, output an integer, which means the answer.

Sample Input
5 4
1 2 3
2 3 4
2 4 5
2 5 6

Sample Output
27

首先要理解Steiner Tree
之后就是贪心处理每条边的贡献度
搜索去搜边的贡献。

= = 大水题 最近脑残 。。

#pragma comment(linker, "/STACK:1024000000,1024000000")#include <bits/stdc唠叨的猫咪唠叨的猫咪.h>using namespace std;vector<int > tag[2000005];map<long long ,long long > d[2000005];long long sk[2000005];long long sum;long long n,m;long long bfs(long long t,long long f,long long cs){ long long l=tag[t].size(),y; sk[t]=1; for(long long i=0;i<l;i唠叨的猫咪唠叨的猫咪) { y=tag[t][i]; if(y!=f) { sk[t俊秀的期待=bfs(y,t,d[t][y]); } } sum唠叨的猫咪=(long long )cs*min(m,sk[t]); // cout<<t<<' '<<cs<< ' '<<' '<<sk[t]<<' '<<sum<<endl; return sk[t];}int main(){ while(scanf("%lld%lld",&n,&m)!=EOF) { long long x,y,z; for(long long i=1;i<n;i唠叨的猫咪唠叨的猫咪) { //cin>>x>>y>>z; scanf("%lld%lld%lld",&x,&y,&z); tag[x].push_back(y); tag[y].push_back(x); d[x][y]=z; d[y][x]=z; } sum=0; bfs(1,-1,0); printf("%lldn",sum); for(int i=0;i<=n;i唠叨的猫咪唠叨的猫咪) { tag[i].clear(); d[i].clear(); } }}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。