Abandoned country
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2226 Accepted Submission(s): 572
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
Recommend
wange2014 | We have carefully selected several similar problems for you:
5733
5732
5731
5730
5729
题目大意:
给出一些节点和一些边,求最小生成树的边之和以及在最小生成树上从任意一点走到任意一点所经过距离的期望。
解题思路:
求最小生成树边之和很简单,难点在求期望。
以任意一个点为祖先节点(为了方便就以编号为1的点好了)给最小生成树确定一个方向。我们如果可以将每条边经过的次数求出来,那么期望=(每个边经过的次数*这条边的长度)/总路径数。令总边数为E,总节点数为N,显然总路径数=N*(N-1)/2,而没一个边经过的次数=以这个点为祖先的子树的节点数*其它的节点数(注意会爆int)。
所以我们只需要在最小生成树上用O(N)的时间便利一边就可以求出每个点的子树的节点数。
比赛时用Prime无限超时,看来是智商不足啊。。。。。。
附AC代码:
题目大意:
给出一些节点和一些边,求最小生成树的边之和以及在最小生成树上从任意一点走到任意一点所经过距离的期望。
解题思路:
求最小生成树边之和很简单,难点在求期望。
以任意一个点为祖先节点(为了方便就以编号为1的点好了)给最小生成树确定一个方向。我们如果可以将每条边经过的次数求出来,那么期望=(每个边经过的次数*这条边的长度)/总路径数。令总边数为E,总节点数为N,显然总路径数=N*(N-1)/2,而没一个边经过的次数=以这个点为祖先的子树的节点数*其它的节点数(注意会爆int)。
所以我们只需要在最小生成树上用O(N)的时间便利一边就可以求出每个点的子树的节点数。
比赛时用Prime无限超时,看来是智商不足啊。。。。。。
附AC代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <map>
using namespace std;
#define LL long long
#define fi first
#define se second
#define INF 0x3f3f3f3f
struct Edge
{
int from,to,cost;
bool operator<(const Edge &other)const{return cost<other.cost; }
};
const int maxn=100000+3;
const int maxm=1000000+3;
Edge e[maxm];
vector<pair<int,int> > G[maxn]; //终点,花费保存最小生成树
int n,m,par[maxn];
bool vis[maxn];
LL ans;
int findfather(int x)
{
return par[x]=par[x]==x?x:findfather(par[x]);
}
double dfs(int now,int last_cost)//遍历找每个子树的节点数
{
LL num=1;
for(int i=0;i<G[now].size();++i)
{
if(!vis[G[now][i].fi])
{
vis[G[now][i].fi]=true;
num+=dfs(G[now][i].fi,G[now][i].se);
}
}
ans+=num*(n-num)*last_cost;//节点数*边长
return num;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
for(int i=1;i<=n;++i)
G[i].clear();
ans=0;
scanf("%d%d",&n,&m);
for(int i=0;i<m;++i)
scanf("%d%d%d",&e[i].from,&e[i].to,&e[i].cost);
sort(e,e+m);
for(int i=1;i<=n;++i)
par[i]=i;
LL min_cost=0;
for(int i=0;i<m;++i)
{
int fa=findfather(e[i].from),fb=findfather(e[i].to);
if(fa!=fb)
{
par[fa]=fb;
min_cost+=e[i].cost;
G[e[i].from].push_back(make_pair(e[i].to,e[i].cost));
G[e[i].to].push_back(make_pair(e[i].from,e[i].cost));
}
}
printf("%lld ",min_cost);
memset(vis,0,sizeof vis);
dfs(1,0);
printf("%.2f\n",1.0*ans/n/(n-1)*2);
}
return 0;
}