hdu5723(树形DP+MST)

5 篇文章 0 订阅

第一问就是裸MST。。

第二问求2点之间距离期望。。就是把距离和全求一遍然后除以n(n-1)嘛。。

所以就是求距离和了。。经典树DP。。

对每个点X分别枚举累加节点深度和(深度指到子树根的距离),对每个儿子t,只要用距离和乘t的节点数,再加上t距离和乘以当前x的节点数,就能算出经过x的路径和了。。




#include<bits/stdc++.h>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define inf 1e9
#define ll long long
#define link(x) for(edge*j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define NM 100005
#define nm 1000005
using namespace std;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return x*f;
}

struct edge{ll t,v;edge*next;}e[2*nm],*h[NM],*o=e;
void add(int x,int y,ll v){o->t=y;o->v=v;o->next=h[x];h[x]=o++;}
struct tmp{
    int a,b,c;
    bool operator<(const tmp&o)const{return c<o.c;}
}a[nm];
int n,m,f[NM],size[NM];
ll s;
double ans,mod,d[NM];
int fin(int x){return f[x]==x?x:f[x]=fin(f[x]);}
void dfs(int x){
    size[x]=1;d[x]=0;
    link(x)if(!f[j->t]){
        f[j->t]=x;
        dfs(j->t);d[j->t]+=j->v*(ll)size[j->t]/mod;
        ans+=(ll)size[x]*d[j->t]+(ll)size[j->t]*d[x];
        d[x]+=d[j->t];size[x]+=size[j->t];
    }
}

int main(){
    int _=read();
    while(_--){
        mem(f);mem(e);mem(h);o=e;mem(d);mem(size);ans=s=0LL;
        n=read();m=read();mod=n*1LL*(n-1)/2;
        inc(i,1,m){
            a[i].a=read();a[i].b=read();a[i].c=read();
        }
        inc(i,1,n)f[i]=i;
        sort(a+1,a+1+m);
        inc(i,1,m){
            int x=fin(a[i].a),y=fin(a[i].b);
            if(x==y)continue;
            f[x]=y;s+=a[i].c;add(a[i].a,a[i].b,a[i].c);add(a[i].b,a[i].a,a[i].c);
        }
        inc(i,1,n)f[i]=0;
        printf("%lld ",s);
        dfs(f[1]=1);
        printf("%.2f\n",ans);
    }
    return 0;
}





Abandoned country

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 6957    Accepted Submission(s): 1703


Problem Description
An abandoned country has n(n100000) villages which are numbered from 1 to n. Since abandoned for a long time, the roads need to be re-built. There are m(m1000000) roads to be re-built, the length of each road is wi(wi1000000). Guaranteed that any two wi are different. The roads made all the villages connected directly or indirectly before destroyed. Every road will cost the same value of its length to rebuild. The king wants to use the minimum cost to make all the villages connected with each other directly or indirectly. After the roads are re-built, the king asks a men as messenger. The king will select any two different points as starting point or the destination with the same probability. Now the king asks you to tell him the minimum cost and the minimum expectations length the messenger will walk.
 

Input
The first line contains an integer T(T10) which indicates the number of test cases.

For each test case, the first line contains two integers n,m indicate the number of villages and the number of roads to be re-built. Next m lines, each line have three number i,j,wi, the length of a road connecting the village i and the village j is wi.
 

Output
output the minimum cost and minimum Expectations with two decimal places. They separated by a space.
 

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

Sample Output
 
 
6 3.33
 

Author
HIT
 

Source
 

Recommend
wange2014   |   We have carefully selected several similar problems for you:   6275  6274  6273  6272  6271 
 

Statistic |  Submit |  Discuss | Note
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值