HDU 5723 - Abandoned country

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

 

題意:在一個廢棄了的國家裏有 n 個點和 m 條路,給出每條路的起點、終點和權值(u, v, w),建立一棵最小生成樹,求出最小生成樹每條邊的和的值,並且求出期望。

一開始並不知道什麼是期望,上網查才知道。我們先定義在建立最小生成樹過程中,遍歷過這條路的次數稱爲這條路的數次【cnt】,而一條路的貢獻就是 cnt * (n - cnt) * w。所有路的貢獻和,暫且用 sum 表示,期望就是 (sum * 2)/(n * (n-1))

 

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

struct node
{
    int u, v, w;
};
node e[2000000 + 5];
typedef pair<int, int> PAIR;
vector <PAIR> edge[100000 + 5];

int n, m;
int father[100000 + 5];
double ans2;

int cmp(node a, node b)
{
    return a.w < b.w;
}

int Find(int x)
{
    if (father[x] == x)
        return x;
    return father[x] = Find(father[x]);
}

void Union(int x, int y)
{
    int fx = Find(x);
    int fy = Find(y);
    if (fx != fy)
        father[fx] = fy;
}

long long Kruskal()
{
    for (int i = 1; i <= n; ++i)
        father[i] = i;
    for (int i = 0; i <= n; ++i)
        edge[i].clear();

    sort(e, e+2*m, cmp);
    long long res = 0;
    for (int i = 0; i < 2*m; ++i)
    {
        int u = e[i].u;
        int v = e[i].v;
        int w = e[i].w;
        int fu = Find(u);
        int fv = Find(v);
        if (fu != fv)
        {
            Union(u, v);
            res += e[i].w;
            edge[u].push_back(PAIR(v, w));
            edge[v].push_back(PAIR(u, w));
        }
    }
    return res;
}

int DFS(int u, int father)
{
    int cnt = 0;
    int len = edge[u].size();
    for (int i = 0; i < len; ++i)
    {
        int v = edge[u][i].first;
        int w = edge[u][i].second;
        if (father != v)
        {
            int temp = DFS(v, u);
            cnt += temp;
            ans2 = ans2 + 1.0 * temp * (n - temp) * w;
        }
    }
    return cnt + 1;
}

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", &e[i].u, &e[i].v, &e[i].w);
            e[m + i].u = e[i].v;
            e[m + i].v = e[i].u;
            e[m + i].w = e[i].w;
        }
        long long ans1 = Kruskal();
        ans2 = 0;
        DFS(1, -1);
        ans2 = ans2 * 2.0 / (1.0 * n) / (n - 1.0);

        printf("%I64d %.2f\n", ans1, ans2);
    }
    return 0;
}


 

發佈了158 篇原創文章 · 獲贊 23 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章