Abandoned country
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 7045 Accepted Submission(s): 1722
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.
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
题意:
给你一个无向图,让你先构造一棵最小生成树,然后问你每两个点之间的最小距离的期望。
思路:
首先我们可以利用组成最小生成树的边构造一副图,我们知道期望公式为权值*概率之和,我们知道每一个权值都是由任意条边构造的,那么我们可以直接考虑每一条边的贡献值,那么我们继续观察可以得出每一条边的共享值为它左边点数*右边点数*边权值,那么我们就可以利用dfs就能得出每一条边左边和右边的点数,因为它是一棵树。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include<queue>
#include<vector>
#include<string>
#include<iostream>
using namespace std;
#define Max 3000100
#define MOD 530600414
typedef long long LL;
struct node{
int to;
int v;
node(int a,int b)
{
to=a;
v=b;
}
};
struct node2{
int from;
int to;
int v;
};
bool cmp(node2 a,node2 b)
{
return a.v<b.v;
}
double ans;
LL n,m,t,f[1001000],sum[1001000],vis[1001000];
vector<node>arr[1001000];
node2 bian[1001000];
int myfind(int x)
{
if(x==f[x])
return x;
else
return f[x]=myfind(f[x]);
}
int he(int a,int b)
{
a=myfind(a);
b=myfind(b);
if(a!=b)
{
f[a]=b;
return 1;
}
else
return 0;
}
void dfs(int now,int f,int cos)
{
if(vis[now])return;
sum[now]=1;
vis[now]=1;
for(int i=0;i<arr[now].size();i++)
{
if(arr[now][i].to!=f)
{
dfs(arr[now][i].to,now,arr[now][i].v);
sum[now]+=sum[arr[now][i].to];
}
}
ans+=(sum[now]*(n-sum[now]))*cos;
}
int main()
{
cin>>t;
while(t--)
{
LL tt=0;
ans=0;
scanf("%lld%lld",&n,&m);
for(int i=1;i<=n;i++)
{
arr[i].clear();
f[i]=i;
sum[i]=0;
vis[i]=0;
}
for(int i=1;i<=m;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
bian[i].from=a,bian[i].to=b,bian[i].v=c;
}
sort(bian+1,bian+1+m,cmp);
for(int i=1;i<=m;i++)
{
if(he(bian[i].from,bian[i].to))
{
tt+=bian[i].v;
arr[bian[i].from].push_back(node(bian[i].to,bian[i].v));
arr[bian[i].to].push_back(node(bian[i].from,bian[i].v));
}
}
dfs(1,1,0);
printf("%lld %.2f\n",tt,ans/(n*(n-1))*2);
}
return 0;
}