当前位置:首页 > > 充电吧
[导读]题面:Abandoned country Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth

题面:


Abandoned country Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1204    Accepted Submission(s): 315


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 2016 Multi-University Training Contest 1  


题意:
     给定一张图,求最小生成树,并求在图中任取两点,两点间路径代价的期望值。


解题:

    因为求路径代价都是唯一的,求两点间路径代价最小值,即求最小生成树上的路径最小值。代价是路径边上的值,故我们可以考虑最小生成树上的边,被取到的概率乘以其权值,累加边代价期望,即可得到总期望。而每条边被取到的概率为该边两侧的点数量的乘积除以C(n,2)。


     先求最小生成树,并在寻找树的过程中,保留最小生成树上的边,用于后续计算期望。采用dfs的方式,任意从树上一点出发,计算该节点所在的子树上的节点数x,并由总数减去x得到边另一侧的节点数。


代码:

#include#include#include#include#include#include#include#include#include#include#include#include#define eps 1e-8
#define LL long long
#define sz1 1000010
#define sz2 100010
using namespace std;
struct Edge
{
	int fm,to,cost,nxt;
}E[sz2<<1];
struct edge
{
	int fm,to,cost;
}store[sz1];
int cnt=0,n,m;
int fa[sz2],head[sz2];
LL cost;
double ans=0;
void addedge(int u,int v,int c)
{
   E[cnt].nxt=head[u];
   head[u]=cnt;
   E[cnt].fm=u;
   E[cnt].to=v;
   E[cnt++].cost=c;
}
bool cmp(edge a,edge b)
{
	return a.cost<b.cost;
}
//并查集操作
int Find(int x)
{
	return  fa[x]!=x?fa[x]=Find(fa[x]):x;
}
void Union(int x,int y)
{
   fa[x]=y;
}
//计算期望
int dfs(int x,int pre)
{
	int res=1,tmp;
	for(int i=head[x];~i;i=E[i].nxt)
	{
		//不回去
		if(E[i].to!=pre)
		{
			tmp=dfs(E[i].to,x);
			ans+=1.0*tmp*(n-tmp)*E[i].cost;
			res+=tmp;
		}
	}
	//res为该节点为根节点的子树上的节点数
	return res;
}
int main()
{
	int t,u,v,x,y,c,am;
	scanf("%d",&t);
	while(t--)
	{
		ans=0;
		cnt=am=0;
		cost=0;
		memset(head,-1,sizeof(head));
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++)
			fa[i]=i;
        for(int i=0;i<m;i++)
			scanf("%d%d%d",&store[i].fm,&store[i].to,&store[i].cost);
		sort(store,store+m,cmp);
		//寻找最小生成树
		for(int i=0;i<m;i++)
		{
            u=store[i].fm;
			v=store[i].to;
			c=store[i].cost;
			x=Find(u);
			y=Find(v);
			if(x!=y)
            {
				Union(x,y);
				am++;
				cost+=store[i].cost;
				addedge(u,v,c);
				addedge(v,u,c);
				//已经添加了n-1条边,则可以停止
				if(am==n-1)
					break;
			}
		}
		dfs(1,-1);
		printf("%lld %.2lfn",cost,2*ans/(1LL*n*(n-1)));
	}
	return 0;
}


本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

纳祥科技推出太阳能+Type-C双充电自行车前灯方案,方案核心模块包含太阳能板、单片机、三极管、3颗LED灯珠与1200mAh电池,通过低功耗单片机与三极管驱动,支持强光/弱光/爆闪3种模式,高流明远射程,适配多种车型

关键字: 方案开发 电子方案 自行车前灯方案 纳祥科技

慕尼黑2025年9月11日 /美通社/ -- 当地时间9月9日,赛力斯动力在德国慕尼黑国际车展期间举办技术发布与交流会,正式在海外市场推出全新一代赛力斯超级增程、高效发动机和新一代分布式电驱动系统,同时与来自全球的汽车产...

关键字: 慕尼黑 分布式 发动机 新能源汽车

慕尼黑2025年9月11日 /美通社/ -- 高端智能电动汽车品牌问界(AITO)在2025年德国国际汽车及智慧出行博览会(IAA MOBILITY)上,正式发布了其最新全球产品阵容——专为中东市场深度本地化打造的AIT...

关键字: AI 智能驾驶 测试 生态系统

舍弗勒首次为中国头部车企大规模生产高压逆变砖 天津工厂一年内完成量产准备,逆变器模块性能参数显著提升 与合作伙伴罗姆半导体共研尖端碳化硅技术,效率更高、性能更优 模块化可扩展设计使逆变砖易于集成,可广泛...

关键字: 逆变 高压 逆变器 集成

舍弗勒以"专注驱动技术的科技公司"为主题亮相IAA MOBILITY 2025(B3馆B40展台) 合并纬湃科技后首次亮相IAA MOBILITY,展示拓展后的汽车产品组合 凭借在软件、...

关键字: 电气 软件 驱动技术 BSP

拉斯维加斯2025年9月11日 /美通社/ -- 在9月8日至11日举办的RE+ 2025展会上,全球综合储能解决方案供应商德赛电池(Desay Battery)全面展示了其创新成果,并宣布与深圳市华宝新能源股份有限公司...

关键字: 电池 电芯 人工智能 锂电

香港2025年 9月12日 /美通社/ -- 全球领先的互联网社区创建者 - 网龙网络控股有限公司 ("网龙"或"本公司",香港交易所股票代码:777)欣然宣布,其子公司My...

关键字: AI 远程控制 控制技术 BSP

慕尼黑2025年9月12日 /美通社/ -- 慕尼黑当地时间9月10日,在2025德国国际汽车及智慧出行博览会(IAA MOBILITY)上,国际独立第三方检测、检验和认证机...

关键字: 测试 慕尼黑 模型 HUBER

上海2025年9月12日 /美通社/ -- 近日,国际独立第三方检测、检验和认证机构德国莱茵TÜV大中华区(简称"TÜV莱茵")为上海...

关键字: 测试 信息安全 安全管理 开关

广州2025年9月12日 /美通社/ -- 9月11日,由国际独立第三方检测、检验和认证机构德国莱茵TÜV大中华区(简称"TÜV莱茵"...

关键字: 数字化 供应链 控制 电子
关闭