HDU 6060 RXD and dividing【DFS】

来源:互联网 发布:2016年ac尼尔森数据 编辑:程序博客网 时间:2024/06/05 19:46



RXD and dividing

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


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 SiSj=
Then he calulates res=ki=1f({1}Si).
He wants to maximize the res.
1kn106
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 n1 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 n100.
 

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

Sample Input
5 41 2 32 3 42 4 52 5 6
 

Sample Output
27
 

Source
2017 Multi-University Training Contest - Team 3
 

  • 对于每一个子树,将子树中的每一个节点分到不同的集合的时候,所需要的边权是最大的,所以考虑每一课子树它的节点数和k的大小,这个值就是这个子树连接它父亲的边的经过的总次数。

  • 问题的实质就是转化为求每个点的子孙节点个数,然后算出每条边要重复计算的次数即可。

#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<queue>#include<stack>#include<vector>#include<map>#include<set>#include<algorithm>using namespace std;#define ll long long#define ms(a,b)  memset(a,b,sizeof(a))#define maxn 510const int M=1e6+10;const int inf=0x3f3f3f3f;const int mod=1e9+7;const double eps=1e-10;ll n,m,k;struct node{    int x,val;};vector<node>v[M];ll d[M],sz[M];void dfs(int u,int fa){    sz[u]=1;    for(int i=0;i<v[u].size();i++){        node cur=v[u][i];        if(cur.x==fa)continue;        d[cur.x]=cur.val;        dfs(cur.x,u);        sz[u]+=sz[cur.x];    }}int main(){    while(~scanf("%lld%lld",&n,&k)){        for(int i=0;i<n-1;i++){            int a,b,c;            scanf("%d%d%d",&a,&b,&c);            v[a].push_back(node{b,c});            v[b].push_back(node{a,c});        }        dfs(1,-1);        ll ans=0;        for(int i=2;i<=n;i++)            ans+=d[i]*min(k,sz[i]);        printf("%lld\n",ans);        for(int i=1;i<=n;i++)v[i].clear();    }    return 0;}



#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<queue>#include<stack>#include<vector>#include<map>#include<set>#include<algorithm>using namespace std;#define ll long long#define ms(a,b)  memset(a,b,sizeof(a))#define maxn 510const int M=1e7+10;const int inf=0x3f3f3f3f;const int mod=1e9+7;const double eps=1e-10;int n,m,k;struct Edge{int v,nxt,w;}edge[M*2];int tot,head[M],sz[M];ll ans;void add(int u,int v,int w){edge[tot]=(Edge){v,head[u],w};head[u]=tot++;edge[tot]=(Edge){u,head[v],w};head[v]=tot++;}void dfs(int u,int fa){sz[u]=1;for(int i=head[u];~i;i=edge[i].nxt){int v=edge[i].v,w=edge[i].w;if(v==fa)continue;dfs(v,u);sz[u]+=sz[v];ll tol=sz[v]<k?sz[v]:k;ans+=tol*w;}}int main(){while(~scanf("%d%d",&n,&k)){     ms(head,-1); tot=ans=0; for(int i=1;i<n;i++){ int v,u,c; scanf("%d%d%d",&u,&v,&c); add(u,v,c); } dfs(1,-1); printf("%lld\n",ans);}  return 0;}



 

原创粉丝点击