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
题意:在一个废弃了的国家里有 n 个点和 m 条路,给出每条路的起点、终点和权值(u, v, w),建立一棵最小生成树,求出最小生成树每条边的和的值,并且求出期望。
一开始并不知道什么是期望,上网查才知道。我们先定义在建立最小生成树过程中,遍历过这条路的次数称为这条路的数次【cnt】,而一条路的贡献就是 cnt * (n - cnt) * w。所有路的贡献和,暂且用 sum 表示,期望就是 (sum * 2)/(n * (n-1))
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
struct node
{
int u, v, w;
};
node e[2000000 + 5];
typedef pair<int, int> PAIR;
vector <PAIR> edge[100000 + 5];
int n, m;
int father[100000 + 5];
double ans2;
int cmp(node a, node b)
{
return a.w < b.w;
}
int Find(int x)
{
if (father[x] == x)
return x;
return father[x] = Find(father[x]);
}
void Union(int x, int y)
{
int fx = Find(x);
int fy = Find(y);
if (fx != fy)
father[fx] = fy;
}
long long Kruskal()
{
for (int i = 1; i <= n; ++i)
father[i] = i;
for (int i = 0; i <= n; ++i)
edge[i].clear();
sort(e, e+2*m, cmp);
long long res = 0;
for (int i = 0; i < 2*m; ++i)
{
int u = e[i].u;
int v = e[i].v;
int w = e[i].w;
int fu = Find(u);
int fv = Find(v);
if (fu != fv)
{
Union(u, v);
res += e[i].w;
edge[u].push_back(PAIR(v, w));
edge[v].push_back(PAIR(u, w));
}
}
return res;
}
int DFS(int u, int father)
{
int cnt = 0;
int len = edge[u].size();
for (int i = 0; i < len; ++i)
{
int v = edge[u][i].first;
int w = edge[u][i].second;
if (father != v)
{
int temp = DFS(v, u);
cnt += temp;
ans2 = ans2 + 1.0 * temp * (n - temp) * w;
}
}
return cnt + 1;
}
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
scanf("%d%d", &n, &m);
for (int i = 0; i < m; ++i)
{
scanf("%d %d %d", &e[i].u, &e[i].v, &e[i].w);
e[m + i].u = e[i].v;
e[m + i].v = e[i].u;
e[m + i].w = e[i].w;
}
long long ans1 = Kruskal();
ans2 = 0;
DFS(1, -1);
ans2 = ans2 * 2.0 / (1.0 * n) / (n - 1.0);
printf("%I64d %.2f\n", ans1, ans2);
}
return 0;
}