Abandoned country

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

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
 
 
Author
HIT
 
Source
 
Recommend
wange2014   |   We have carefully selected several
similar problems for you:  5733 5732 5731 5730 5729 
 
这题我完全没有思路,比赛时也不会写,看了别人的代码,貌似懂了一点点。。
 
题意:给了一个图,求最小生成树和期望值,最小生成树指所有的点连接成一条完整的路的最小权值和,期望指图中任意两个点的距离之和除以所有的边的个数(如测试数据:4个点,就是用1-2,1-3,1-4,2-3,2-4,3-4这六条距离的和除以边长总数6)。
 
附上代码:
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define M 200010
#define ll long long
using namespace std;
ll n,m; struct edge
{
int u,v;
int w;
} e[M*]; struct node
{
int u,v,w;
int ans; ///ans记录经过这条边能达到的边的个数
int next; ///next记录上一次连接的边的序号
}ee[M]; int father[M],head[M]; ///father数组表示根节点,head记录每条边最后一次连接的边的序号
ll ans,sum;
int tol; ll dfs(int u,int p) ///搜索
{
ll num=;
for(int i=head[u];i!=-;i=ee[i].next)
{
if(ee[i].v!=p)
{
ee[i].ans+=dfs(ee[i].v,ee[i].u);
sum+=ee[i].ans*(n-ee[i].ans)*ee[i].w; ///此边能到达的边的个数*能到此边的边的个数*边的长度
// cout<<ee[i].ans<<" "<<n-ee[i].ans<<" "<<ee[i].w<<endl;
//cout<<sum<<endl;
num+=ee[i].ans;
}
}
return num;
} void add_node(edge a) ///建立邻接表
{
ee[tol].u=a.u; ///正向
ee[tol].v=a.v;
ee[tol].w=a.w;
ee[tol].ans=;
ee[tol].next=head[a.u];
head[a.u]=tol++;
ee[tol].u=a.v; ///反向
ee[tol].v=a.u;
ee[tol].w=a.w;
ee[tol].ans=;
ee[tol].next=head[a.v];
head[a.v]=tol++;
} int find(int x) ///搜索根节点
{
while(x!=father[x])
x=father[x];
return x;
} void sourch(int x,int y,int z,edge ss)
{
x=find(x);
y=find(y);
if(x!=y) ///若根节点不相同
{
add_node(ss); ///并且此边存在,建立邻接表
father[x]=y; ///将其连接在一起
ans+=z;
} } void init()
{
ans=,sum=;
int i,j;
for(i=; i<=n; i++)
{
father[i]=i; ///将所有父节点初始定义为自己本身
head[i]=-;
}
tol=;
for(i=; i<m; i++)
{
sourch(e[i].u,e[i].v,e[i].w,e[i]); ///求最小生成树
}
dfs(,-);
ll nn=n*(n-)/;
printf("%I64d %.2lf\n",ans,(double)sum/nn);
} bool cmp(edge a,edge b)
{
return a.w<b.w;
} int main()
{
int T,i;
scanf("%d",&T);
while(T--)
{
scanf("%lld%lld",&n,&m);
for(i=; i<m; i++)
{
scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w); ///记录每边的数据
}
sort(e,e+m,cmp); ///要选取最小的花费,因此要进行排序
init();
}
return ;
}

hdu 5723 Abandoned country(2016多校第一场) (最小生成树+期望)的更多相关文章

  1. HDU 5723 Abandoned country(落后渣国)

    HDU 5723 Abandoned country(落后渣国) Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 ...

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

    Abandoned country Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  3. 最小生成树 kruskal hdu 5723 Abandoned country

    题目链接:hdu 5723 Abandoned country 题目大意:N个点,M条边:先构成一棵最小生成树,然后这个最小生成树上求任意两点之间的路径长度和,并求期望 /************** ...

  4. HDU 5723 Abandoned country 【最小生成树&&树上两点期望】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=5723 Abandoned country Time Limit: 8000/4000 MS (Java/ ...

  5. HDU 5723 Abandoned country (最小生成树 + dfs)

    Abandoned country 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5723 Description An abandoned coun ...

  6. hdu 5723 Abandoned country 最小生成树 期望

    Abandoned country 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5723 Description An abandoned coun ...

  7. HDU 5723 Abandoned country(kruskal+dp树上任意两点距离和)

    Problem DescriptionAn abandoned country has n(n≤100000) villages which are numbered from 1 to n. Sin ...

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

    Abandoned country Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  9. HDU 5723 Abandoned country(最小生成树+边两边点数)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=5723 题意:给出一个无向图,每条路都有一个代价,求出把所有城市连通的最小代价.在此基础上,国王会从这 ...

随机推荐

  1. CURL POST PHP

    function SendPostCurl($url,$post_data){ $curl = curl_init(); //初始化 curl_setopt($curl, CURLOPT_URL, $ ...

  2. linux 查看磁盘空间占用情况

    工作中有时被分配的测试机空间不大,经常遇到磁盘空间占满的情况.排查过程如下: 一.首先使用df -h 命令查看磁盘剩余空间,通过以下图看出/目录下的磁盘空间已经被占满. 二.进入根目录,因为最近常用的 ...

  3. Linux硬链接和软连接

    硬链接(hard link): A是B的硬链接(A和B都是文件名),则A的目录项中的inode节点号与B的目录项中的inode节点号相同,即一个inode节点对应两个不同的文件名,两个文件名指向同一个 ...

  4. Katalon系列十八:用例变量&用例间调用

    一.用例变量写用例时,我们可以用代码定义变量,如:String name = '新闻'println(name) 上面是硬编码,我们也可以在用例里定义变量,只在该用例里生效哦,想跨用例就用全局变量. ...

  5. OpenCV灰度化图像

    OpenCV2版本号非常多函数发生了变化.比如二值化,其演示样例: void CmyMFC2Dlg::OnBnClickedButton1() { // TODO: Add your control ...

  6. HTTP请求封装Java工具类

    装载自:http://www.open-open.com/lib/view/open1384844838743.html package com.wiker; import java.io.Buffe ...

  7. Bundler和Minifier Visual Studio扩展

    原文地址:https://marketplace.visualstudio.com/items?itemName=MadsKristensen.BundlerMinifier 特征 将CSS,Java ...

  8. 在vue项目中同时使用element-ui和mint-ui,的时候,.babelrc配置文件怎么写

    我们安装vue组件库的时候,考虑到大小问题,需要根据需要仅引入部分组件 借助 babel-plugin-component,我们可以只引入需要的组件,以达到减小项目体积的目的. 但是在配置  .bab ...

  9. 【水滴石穿】github_popular

    项目不难,就是文件摆放位置跟别的不一样 https://github.com/chenji336/github_popular //定义入口是app.js ///** @format */ impor ...

  10. iOS GCD 使用

    1. dispatch queue的概念 dispatch queue分成以下三种: a)运行在主线程的Main queue,通过dispatch_get_main_queue获取.dispatch_ ...