HDU5723(53/600)

本文探讨了在一个由村庄组成的网络中重建道路的问题,旨在找到最小成本连接所有村庄的方案,并计算信使在这些村庄间行走的最短期望路径长度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

An abandoned country has n(n≤100000)n(n≤100000) villages which are numbered from 1 to nn. Since abandoned for a long time, the roads need to be re-built. There are m(m≤1000000)m(m≤1000000) roads to be re-built, the length of each road is wi(wi≤1000000)wi(wi≤1000000). Guaranteed that any two wiwi 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(T≤10)T(T≤10) which indicates the number of test cases.

For each test case, the first line contains two integers n,mn,m indicate the number of villages and the number of roads to be re-built. Next mm lines, each line have three number i,j,wii,j,wi, the length of a road connecting the village ii and the village jj is wiwi.
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

对于每一条边的两个端点
下面的朝上每一个都有
上面的朝下每一个都有

全你妈坑点…草

#include<bits/stdc++.h>
using namespace std;
#define int long long
template <class T> inline void in(T &x) {
    T f = 1; char c; while ((c = getchar()) < '0' || c > '9') if (c == '-') f = -1;
    x = c - '0';
    while ((c = getchar()) >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0'; x *= f;
}
int n,m;
struct p
{
    int z,y,c;
    bool operator < (const p&a)const
    {
        return c<a.c;
    }
};
vector<int>tu[100001],bg[100001];
int die[100001];
int zhao(int x)
{
    if(x==die[x])return x;
    return die[x]=zhao(die[x]);
}
int gs[100001];
bool bj[100001];
void dfs1(int gen)
{
    bj[gen]=1;
    gs[gen]=1;
    for(int a=0;a<tu[gen].size();a++)
    {
        int dian=tu[gen][a];
        if(bj[dian])continue;
        dfs1(dian);
        gs[gen]+=gs[dian];
    }
}
long long dan=0;
void dfs3(int gen)
{
    bj[gen]=1;
    for(int a=0;a<tu[gen].size();a++)
    {
        int dian=tu[gen][a];
        int bi=bg[gen][a];
        if(bj[dian])continue;
        dfs3(dian);
        dan+=(n-gs[dian])*gs[dian]*bi;
    }
}
p bbb[1000001];
main()
{
    int T;
    cin>>T;
    while(T--)
    {
        dan=0;
        in(n),in(m);
        if(n==0||m==0)
        {
            printf("0 0.00\n");
            continue;
        }
        memset(gs,0,sizeof(gs));
        for(int a=1;a<=n;a++)tu[a].clear(),bg[a].clear();
        for(int a=1;a<=n;a++)die[a]=a;
        int q,w,e;
        for(int a=1;a<=m;a++)
        {
            in(q),in(w),in(e);
            bbb[a]={q,w,e};
        }
        sort(bbb+1,bbb+m+1);
        long long  scs=0;
        int ttt=0;
        for(int a=1;a<=m;a++)
        {
            p zs=bbb[a];
            if(zhao(zs.z)==zhao(zs.y))continue;
            die[zhao(zs.z)]=zhao(zs.y);
            ttt++;
            tu[zs.z].push_back(zs.y);
            tu[zs.y].push_back(zs.z);
            bg[zs.z].push_back(zs.c);
            bg[zs.y].push_back(zs.c);
            scs+=zs.c;
            if(ttt==n-1)break;
        }
        int gen=1;
        for(;gen<=n;gen++)
        {
            if(tu[gen].size()==1)break;
        }
        memset(bj,0,sizeof(bj));
        dfs1(gen);
        memset(bj,0,sizeof(bj));
        dfs3(gen);
        double tt=((n)*(n-1));
        double jg=1.0*dan*2/tt;
        printf("%I64d %.2lf\n",scs,jg);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值