思路:先跑一个最小生成树然后建好生成树,DFS直接求出任意点对距离求平均即可
坑点:各种爆int
#include<bits/stdc++.h>
using namespace std;
#define LL long long
int cas=1,T;
const int maxn = 100000+500;
const int maxm = 1000000+500;
struct Edge
{
int u,v;
LL dist;
Edge(){}
Edge(int u,int v,LL d):u(u),v(v),dist(d){}
bool operator < (const Edge&rhs)const
{
return dist < rhs.dist;
}
};
int n,m;
Edge edges[maxm];
int pre[maxn];
int Find(int x)
{
return pre[x]==-1?x:pre[x]=Find(pre[x]);
}
void init()
{
m=0;
memset(pre,-1,sizeof(pre));
}
void AddEdge(int u,int v,LL dist)
{
edges[m++]=Edge(u,v,dist);
}
struct Node
{
int v;
LL len;
Node(){};
Node(int vv,LL lenn):v(vv),len(lenn){};
};
vector<Node>vet[maxn];
double dp[maxn];
int siz[maxn];
void dfs(int root,int fa)
{
siz[root]=1;
for(int i = 0;i<vet[root].size();i++)
{
int son = vet[root][i].v;
LL len = vet[root][i].len;
if(son==fa)
continue;
dfs(son,root);
siz[root]+=siz[son];
dp[root]+=dp[son]+((LL)siz[son]*(LL)(n-siz[son]))*(double)len;
}
}
LL Kruskal()
{
LL sum = 0;
int cnt = 0;
sort(edges,edges+m);
for (int i = 0;i<m;i++)
{
int u = edges[i].u;
int v = edges[i].v;
if (Find(u) != Find(v))
{
pre[Find(u)] = Find(v);
sum+=edges[i].dist;
vet[u].push_back(Node(v,edges[i].dist));
vet[v].push_back(Node(u,edges[i].dist));
if (++cnt >= n-1)
break;
}
}
return sum;
}
int main()
{
scanf("%d",&T);
while(T--)
{
int flag = 0;
int mm;
scanf("%d%d",&n,&mm);
if(mm==0)
flag =1;
init();
for (int i = 0;i<mm;i++)
{
int a,b;
LL c;
scanf("%d%d%I64d",&a,&b,&c);
AddEdge(a,b,c);
}
LL ans = Kruskal();
printf("%I64d ",ans);
memset(siz,0,sizeof(siz));
memset(dp,0,sizeof(dp));
dfs(1,-1);
LL ress = (LL)n*(LL)(n-1);
LL res = ress/2;
if(!flag)
printf("%.2lf\n",(double)dp[1]/res);
else
printf("0.00\n");
for(int i = 0;i<=n;i++)
vet[i].clear();
}
//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
return 0;
}
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
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