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

當前位置:首頁 > 芯聞號 > 充電吧
[導讀]題面: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ù)量的乘積除以C(n,2)。


???? 先求最小生成樹,并在尋找樹的過程中,保留最小生成樹上的邊,用于后續(xù)計算期望。采用dfs的方式,任意從樹上一點出發(fā),計算該節(jié)點所在的子樹上的節(jié)點數(shù)x,并由總數(shù)減去x得到邊另一側的節(jié)點數(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;
}
//計算期望
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é)點為根節(jié)點的子樹上的節(jié)點數(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);
		//尋找最小生成樹
		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;
}


本站聲明: 本文章由作者或相關機構授權發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點,本站亦不保證或承諾內(nèi)容真實性等。需要轉載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權益,請及時聯(lián)系本站刪除( 郵箱:macysun@21ic.com )。
換一批
延伸閱讀
關閉