日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當(dāng)前位置:首頁(yè) > > 充電吧
[導(dǎo)讀]題面: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 ?


題意:
???? 給定一張圖,求最小生成樹(shù),并求在圖中任取兩點(diǎn),兩點(diǎn)間路徑代價(jià)的期望值。


解題:

??? 因?yàn)榍舐窂酱鷥r(jià)都是唯一的,求兩點(diǎn)間路徑代價(jià)最小值,即求最小生成樹(shù)上的路徑最小值。代價(jià)是路徑邊上的值,故我們可以考慮最小生成樹(shù)上的邊,被取到的概率乘以其權(quán)值,累加邊代價(jià)期望,即可得到總期望。而每條邊被取到的概率為該邊兩側(cè)的點(diǎn)數(shù)量的乘積除以C(n,2)。


???? 先求最小生成樹(shù),并在尋找樹(shù)的過(guò)程中,保留最小生成樹(shù)上的邊,用于后續(xù)計(jì)算期望。采用dfs的方式,任意從樹(shù)上一點(diǎn)出發(fā),計(jì)算該節(jié)點(diǎn)所在的子樹(shù)上的節(jié)點(diǎn)數(shù)x,并由總數(shù)減去x得到邊另一側(cè)的節(jié)點(diǎn)數(shù)。


代碼:

#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;
}
//計(jì)算期望
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為該節(jié)點(diǎn)為根節(jié)點(diǎn)的子樹(shù)上的節(jié)點(diǎn)數(shù)
	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);
		//尋找最小生成樹(shù)
		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);
				//已經(jīng)添加了n-1條邊,則可以停止
				if(am==n-1)
					break;
			}
		}
		dfs(1,-1);
		printf("%lld?%.2lfn",cost,2*ans/(1LL*n*(n-1)));
	}
	return?0;
}


本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀(guān)點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專(zhuān)欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除。
換一批
延伸閱讀

納祥科技推出太陽(yáng)能+Type-C雙充電自行車(chē)前燈方案,方案核心模塊包含太陽(yáng)能板、單片機(jī)、三極管、3顆LED燈珠與1200mAh電池,通過(guò)低功耗單片機(jī)與三極管驅(qū)動(dòng),支持強(qiáng)光/弱光/爆閃3種模式,高流明遠(yuǎn)射程,適配多種車(chē)型

關(guān)鍵字: 方案開(kāi)發(fā) 電子方案 自行車(chē)前燈方案 納祥科技

慕尼黑2025年9月11日 /美通社/ -- 當(dāng)?shù)貢r(shí)間9月9日,賽力斯動(dòng)力在德國(guó)慕尼黑國(guó)際車(chē)展期間舉辦技術(shù)發(fā)布與交流會(huì),正式在海外市場(chǎng)推出全新一代賽力斯超級(jí)增程、高效發(fā)動(dòng)機(jī)和新一代分布式電驅(qū)動(dòng)系統(tǒng),同時(shí)與來(lái)自全球的汽車(chē)產(chǎn)...

關(guān)鍵字: 慕尼黑 分布式 發(fā)動(dòng)機(jī) 新能源汽車(chē)

慕尼黑2025年9月11日 /美通社/ -- 高端智能電動(dòng)汽車(chē)品牌問(wèn)界(AITO)在2025年德國(guó)國(guó)際汽車(chē)及智慧出行博覽會(huì)(IAA MOBILITY)上,正式發(fā)布了其最新全球產(chǎn)品陣容——專(zhuān)為中東市場(chǎng)深度本地化打造的AIT...

關(guān)鍵字: AI 智能駕駛 測(cè)試 生態(tài)系統(tǒng)

舍弗勒首次為中國(guó)頭部車(chē)企大規(guī)模生產(chǎn)高壓逆變磚 天津工廠(chǎng)一年內(nèi)完成量產(chǎn)準(zhǔn)備,逆變器模塊性能參數(shù)顯著提升 與合作伙伴羅姆半導(dǎo)體共研尖端碳化硅技術(shù),效率更高、性能更優(yōu) 模塊化可擴(kuò)展設(shè)計(jì)使逆變磚易于集成,可廣泛...

關(guān)鍵字: 逆變 高壓 逆變器 集成

舍弗勒以"專(zhuān)注驅(qū)動(dòng)技術(shù)的科技公司"為主題亮相IAA MOBILITY 2025(B3館B40展臺(tái)) 合并緯湃科技后首次亮相IAA MOBILITY,展示拓展后的汽車(chē)產(chǎn)品組合 憑借在軟件、...

關(guān)鍵字: 電氣 軟件 驅(qū)動(dòng)技術(shù) BSP

拉斯維加斯2025年9月11日 /美通社/ -- 在9月8日至11日舉辦的RE+ 2025展會(huì)上,全球綜合儲(chǔ)能解決方案供應(yīng)商德賽電池(Desay Battery)全面展示了其創(chuàng)新成果,并宣布與深圳市華寶新能源股份有限公司...

關(guān)鍵字: 電池 電芯 人工智能 鋰電

香港2025年 9月12日 /美通社/ -- 全球領(lǐng)先的互聯(lián)網(wǎng)社區(qū)創(chuàng)建者 - 網(wǎng)龍網(wǎng)絡(luò)控股有限公司 ("網(wǎng)龍"或"本公司",香港交易所股票代碼:777)欣然宣布,其子公司My...

關(guān)鍵字: AI 遠(yuǎn)程控制 控制技術(shù) BSP

慕尼黑2025年9月12日 /美通社/ -- 慕尼黑當(dāng)?shù)貢r(shí)間9月10日,在2025德國(guó)國(guó)際汽車(chē)及智慧出行博覽會(huì)(IAA MOBILITY)上,國(guó)際獨(dú)立第三方檢測(cè)、檢驗(yàn)和認(rèn)證機(jī)...

關(guān)鍵字: 測(cè)試 慕尼黑 模型 HUBER

上海2025年9月12日 /美通社/ -- 近日,國(guó)際獨(dú)立第三方檢測(cè)、檢驗(yàn)和認(rèn)證機(jī)構(gòu)德國(guó)萊茵TÜV大中華區(qū)(簡(jiǎn)稱(chēng)"TÜV萊茵")為上海...

關(guān)鍵字: 測(cè)試 信息安全 安全管理 開(kāi)關(guān)

廣州2025年9月12日 /美通社/ -- 9月11日,由國(guó)際獨(dú)立第三方檢測(cè)、檢驗(yàn)和認(rèn)證機(jī)構(gòu)德國(guó)萊茵TÜV大中華區(qū)(簡(jiǎn)稱(chēng)"TÜV萊茵"...

關(guān)鍵字: 數(shù)字化 供應(yīng)鏈 控制 電子
關(guān)閉