hdu 多校 RXD and dividing

来源:互联网 发布:windows aero 用不了 编辑:程序博客网 时间:2024/06/08 12:19

RXD and dividing

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

Problem Description
RXD has a tree T, with the size of n. Each edge has a cost.
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("%lld\n",sum);            for(int i=0;i<=n;i++)            {                tag[i].clear();                d[i].clear();            }        }}
原创粉丝点击