Abandoned country
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 953 Accepted Submission(s): 254
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
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int pre[1000010],ran[1000010];
int head[1000010],path[1000010],num[1000010];
int tot;
int m,n;
struct node1
{
int u;
int v;
int w;
};
node1 node[1000010];
struct node2
{
int v,w,next;
}temp[1000010];
void init()
{
for(int i=0;i<=n;i++)
{
pre[i]=i;
ran[i]=1;
}
tot=0;
memset(head,-1,sizeof(head));
memset(path,0,sizeof(path));
memset(num,0,sizeof(num));
}
int find(int x)
{
if(x==pre[x])
return x;
else
return find(pre[x]);
}
bool cmp(node1 a,node1 b)
{
return a.w<b.w;
}
bool unite(int a,int b)
{
int x=find(a);
int y=find(b);
if(x==y)
return false;
else if(x>y)
{
pre[x]=y;
ran[y]++;
}
else if(x<=y)
{
pre[y]=x;
ran[x]++;
}
return true;
}
void add(int u,int v,int w)
{
temp[tot].v=v;
temp[tot].next=head[u];
temp[tot].w=w;
head[u]=tot++;
}
void dfs(int u,int v)
{
num[u]=1;
for(int i=head[u];i!=-1;i=temp[i].next)
{
if(temp[i].v==v)
continue;
path[temp[i].v]=temp[i].w;
dfs(temp[i].v,u);
num[u]+=num[temp[i].v];
}
}
void kruskal()
{
init();
long long sum=0;
int countt=0;
sort(node,node+m,cmp);
for(int i=0;i<m;i++)
{
if(unite(node[i].u,node[i].v))
{
countt++;
sum+= (long long)node[i].w;
add(node[i].u,node[i].v,node[i].w);
add(node[i].v,node[i].u,node[i].w);
}
if(countt==n-1)
break;
}
dfs(1,0);
long long ans=0;
for(int i=2;i<=n;i++)
ans+=(long long)num[i]*path[i]*(n-num[i]);
printf("%lld %.2lf\n",sum,ans*1.0/((long long)n*(n-1)/2));
}
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",&node[i].u,&node[i].v,&node[i].w);
kruskal();
}
return 0;
}