Abandoned country
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2325 Accepted Submission(s): 592
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
题意:
有n个点,由m条无向边连通,同时,这m条边各自有一个互不相同的长度wi。现在想知道这个图的MST以及在MST的基础上,任意两点间距的期望值。
0 < n ≤ 100000
0 < m ≤ 1000000
0 < wi ≤ 1000000
题解:
MST很好求,同时因为每条边从长度都不一样,那么MST一定是唯一的。用一个Kruskal求出MST后,找到一个根,并由此DFS,计算每条边要使用的次数,乘以边权加到result里,最后将result除以总搭配数。
对于每条边的使用次数,应该是此边上方点数乘以下方点数乘以2,那么可以只算上下两边点数乘积,最后乘2。具体实施方法,对于一个边,下方点数应该是儿子结点的下方点数加上儿子节点数,递归DFS即可。
//
// main.cpp
// HDU-5723-Abandoned country
//
// Created by 袁子涵 on 16/7/20.
// Copyright © 2016年 袁子涵. All rights reserved.
//
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <string>
#include <queue>
#include <set>
#include <algorithm>
#include <stack>
using namespace std;
const int MAXN=100005;
const int MAXM=1000005;
int t;
long long int n,m,father[MAXN],a,b,fa,fb,root,mst;
double result;
bool vis[MAXN];
typedef struct edge1
{
long long int to,w;
edge1(long long int _to,long long int _w):to(_to),w(_w){}
}edge1;
typedef struct edge
{
long long int u,v,w;
bool operator <(const edge &r)const
{
return w<r.w;
}
}edge;
long long int find(long long int num)
{
long long int now=num,root;
while (father[now]!=now)
now=father[now];
root=now;
now=num;
while (father[now]!=now) {
now=father[now];
father[now]=root;
}
return root;
}
vector<edge1>Node[MAXN];
edge Edge[MAXM];
long long int dfs(long long int num,long long int wt)
{
long long int son=0;
vis[num]=1;
for (long long int i=0; i<Node[num].size(); i++)
if (vis[Node[num][i].to]==0)
son+=dfs(Node[num][i].to, Node[num][i].w);
son++;
result+=(n-son)*son*wt;
return son;
}
int main(int argc, const char * argv[]) {
scanf("%d",&t);
while (t--) {
mst=result=0;
scanf("%lld%lld",&n,&m);
for (long long int i=1; i<=n; i++)
{
vis[i]=0;
Node[i].clear();
father[i]=i;
}
for (long long int i=0; i<m; i++)
scanf("%lld%lld%lld",&Edge[i].u,&Edge[i].v,&Edge[i].w);
sort(Edge+0,Edge+m);
for (long long int i=0; i<m; i++) {
a=Edge[i].u,b=Edge[i].v;
fa=find(a),fb=find(b);
if(fa!=fb)
{
Node[a].push_back(edge1(b,Edge[i].w));
Node[b].push_back(edge1(a,Edge[i].w));
mst+=Edge[i].w;
father[fa]=fb;
}
}
for (long long int i=1; i<=n; i++) {
if (father[i]==i) {
root=i;
break;
}
}
dfs(root,0);
result/=(double)(n*n-n);
result*=2;
printf("%lld %.2lf\n",mst,result);
}
return 0;
}