Abandoned country

来源:互联网 发布:实施工程师sql笔试题 编辑:程序博客网 时间:2024/05/18 00:48
Abandoned country
Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3006    Accepted Submission(s): 346


Problem Description
An abandoned country has n (n \leq 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 \leq 1000000) roads to be re-built, the length of each road is w_{i} (w_{i} \leq 1000000). Guaranteed that any two w_{i} 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 \leq 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, w_{i}, the length of a road connecting the village i and the village j is w_{i}.
 

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 <cstdio>#include <cstring>#include <cstdlib>#include <algorithm>#include <vector>using namespace std;#define N 100010struct node {    int u, v, w;    node() {}    node(int _u, int _v, int _w):u(_u), v(_v), w(_w) {}};struct Node{   int v,len;   Node(){}   Node(int _v, int _len):v(_v), len(_len){}};vector<Node>vet[N];vector<node> edge;int n, m, f[N];double dp[N];int sum[N];bool cmp(const node &x, const node &y) {    return x.w < y.w;}int find_set(int x) {    if (f[x] == x) return x;    return f[x] = find_set(f[x]);}long long Kruskal() {    sort(edge.begin(), edge.end(), cmp);    for (int i = 1; i <= n; i++) f[i] = i;    long long ans = 0;    for (int i = 0, u, v, w; i < (int)edge.size(); i++)    {        u = edge[i].u, v = edge[i].v, w = edge[i].w;        u = find_set(u), v = find_set(v);        if (u == v) continue;        f[u] = v;        ans += (long long)w;        vet[edge[i].u].push_back(Node(edge[i].v,w));        vet[edge[i].v].push_back(Node(edge[i].u,w));    }    return ans;}void dfs(int root,int father){   sum[root] = 1;   for(int i = 0;i < (int)vet[root].size();i++)    {      int son = vet[root][i].v;      int len = vet[root][i].len;      if(son == father)continue;      dfs(son,root);      sum[root] += sum[son];      dp[root] += dp[son] + ((double)sum[son] * (n - sum[son])) * len;   }}int main(){    //freopen("in.txt","r",stdin);    //freopen("out.txt","w",stdout);    int t;    scanf("%d",&t);    while(t--)    {        scanf("%d%d", &n, &m);        edge.clear();        for (int i = 0, a, b, c; i < m; i++)        {            scanf("%d%d%d", &a, &b, &c);            edge.push_back(node(a, b, c));        }        for(int i = 1;i <= n;i++)vet[i].clear();        memset(sum,0,sizeof(sum));        memset(dp,0,sizeof(dp));        long long ans = Kruskal();        dfs(1,0);        long long s = (long long)n * (n - 1) / 2;        printf("%I64d %.2f\n",ans,dp[1] / (double)s);    }    return 0;}

0 0