hdu 5723 Abandoned country 最小生成树+子节点统计

Abandoned country

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

Problem Description

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

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
typedef  long long  ll;
typedef unsigned long long ull;
#define MM(a,b) memset(a,b,sizeof(a));
#define inf 0x7f7f7f7f
#define FOR(i,n) for(int i=1;i<=n;i++)
#define CT continue;
#define PF printf
#define SC scanf
const int mod=1000000007;
const int N=1e6+10;
int num[N],f[N];
vector<int> G[N];

struct edge{
   int u,v,cost,flag;
}e[N];

bool cmp(edge a,edge b)
{
   return a.cost<b.cost;
}

int findr(int u)
{
    if(f[u]!=u)
        f[u]=findr(f[u]);
    return f[u];
}

int dfs_clock;

void dfs(int u,int pre)
{
    int p=++dfs_clock;
    for(int i=0;i<G[u].size();i++)
    {
        int v=G[u][i];
        if(v==pre) continue;
        dfs(v,u);
    }
    num[u]=dfs_clock-p+1;
}

int main()
{
    int cas,n,m;
    scanf("%d",&cas);
    while(cas--)
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++) G[i].clear();
        for(int i=1;i<=m;i++)
                {
                    scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].cost);
                     e[i].flag=0;
                }
        sort(e+1,e+m+1,cmp);
        for(int i=1;i<=n;i++) f[i]=i;
        ll cost=0;
        for(int i=1;i<=m;i++)
        {
            int u=findr(e[i].u),v=findr(e[i].v);
            if(u==v) continue;
            f[u]=v;
            e[i].flag=1;
            cost+=e[i].cost;
            G[e[i].u].push_back(e[i].v);
            G[e[i].v].push_back(e[i].u);
        }

        dfs_clock=0;
        dfs(1,-1);
        double q=0;
        for(int i=1;i<=m;i++)
           if(e[i].flag)
           {
               int u=e[i].u,v=e[i].v;
               ll k=min(num[u],num[v]);
               q+=k*(n-k)*e[i].cost;
           }

       q=2*q/n/(n-1);
       printf("%lld %.2f\n",cost,q);
    }
    return 0;
}

  分析:刚开始以为是道kruskal+统计子节点的水题,,,,后来写了下,,发现这道题目有个很迷的地方,就是连接起所有村庄的最小的总cost和最小的期望值,最小的cost当然跑kruskal,那会不会在同一种

cost的情况下出现不同期望值?那该怎么选择,,,于是瞬间懵逼,,就放弃了。。。

可见,,比赛时缺乏基本的随机应变和见招拆招的能力,,这要靠比赛来加强了。。。。比赛时胆子真的太小了,,,只有多多比赛慢慢克服了。。。

解决:其实因为题目说了所有边的权值均不同,,kruskal中对边sort后,形成最小生成树的边的选取方案是

唯一的,所以最小生成树唯一;

时间: 2024-11-03 20:56:11

hdu 5723 Abandoned country 最小生成树+子节点统计的相关文章

HDU 5723 Abandoned country 最小生成树+搜索

Abandoned country Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 4477    Accepted Submission(s): 1124 Problem Description An abandoned country has n(n≤100000) villages which are numbered from 1

hdu 5723 Abandoned country(最小生成树,dfs)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5723 Abandoned country Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2969    Accepted Submission(s): 725 Problem Description An abandoned country

最小生成树 kruskal hdu 5723 Abandoned country

题目链接:hdu 5723 Abandoned country 题目大意:N个点,M条边:先构成一棵最小生成树,然后这个最小生成树上求任意两点之间的路径长度和,并求期望 /************************************************************** Problem:hdu 5723 User: youmi Language: C++ Result: Accepted Time:2932MS Memory:22396K solution:首先注意到任

HDU 5723 Abandoned country

题目说每条边权值都不一样,说明最小生成树是唯一的,不存在最小期望这一说. 然后就是先求出最小生成树,随便确定一个根节点,计算出每个点的子树有多少节点,记为c[x]. 指向x的这条边被统计的次数为c[x]*(n-c[x]).然后基本就可以算出答案了. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include<

HDU5723 Abandoned country 最小生成树+深搜回溯法

Description An abandoned country has n(n≤100000) villages which are numbered from 1 to n. Since abandoned for a long time, the roads need to be re-built. There are m(m≤1000000) roads to be re-built, the length of each road is wi(wi≤1000000). Guarante

hdu 4632 子字符串统计的区间dp

题意:查找这样的子回文字符串(未必连续,但是有从左向右的顺序)个数. 简单的区间dp,哎,以为很神奇的东西,其实也是dp,只是参数改为区间,没做过此类型的题,想不到用dp,以后就 知道了,若已经知道[0,i],推[0,i+1], 显然还要从i+1 处往回找,dp方程也简单: dp[j][i]=(dp[j+1][i]+dp[j][i-1]+10007-dp[j+1][i-1])%10007; 减去中间一段重复的 if(s[i]==s[j])dp[j][i]=(dp[j][i]+dp[j+1][i-

HDU 1863 畅通工程 (最小生成树)

畅通工程 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 16937    Accepted Submission(s): 7099 Problem Description 省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).经过调查评估,得到的统计表中列出了有可能建设

【JS学习笔记】DOM基础-首尾子节点,兄弟节点

一.DOM节点 (1)首尾子节点 有兼容性问题 firstChild.firstElementChild firstChild在高版本的浏览器上具有兼容问题,firstChild在高版本浏览器中指的是文本元素. firstElementChild高级浏览器下可以使用,在IE6-8下反而不兼容. 解决兼容性的办法是使用if进行判断 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://

【JSTREE】动态加载子节点

js中初始化jstree $('#contact-org').jstree({ "data" : { "dataType": 'json', "url":function(node){ return ctxPath + "/v-contact?queryOrg"; }, "data":function(node){ return {"id" : node.id}; } } } } 返回的