zoukankan      html  css  js  c++  java
  • HDU 5723:Abandoned country(最小生成树+算期望)

    http://acm.hdu.edu.cn/showproblem.php?pid=5723

    Abandoned country

    Problem Description
     
    An abandoned country has n(n100000) villages which are numbered from 1 to n. Since abandoned for a long time, the roads need to be re-built. There are m(m1000000) roads to be re-built, the length of each road is wi(wi1000000). 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(T10) 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
     
    题意:国家有M条废弃的路和N个点,现在要重新修,第一个问题是求出连通每一个点需要的最短路径是多少,第二个问题是求在连通的路任意选两个点走,它的最小期望是多少。
    思路:第一个问题很简单,就是单纯的最小生成树,但是第二个问题一开始让我和队友感到很迷茫。英文题面,真是比较难懂意思。最小期望??什么鬼。。后来仔细想了好久,建立起了最小生成树的图了。。最终还是不会做,不知道DFS怎么跑来算期望。。听了别人的讲解才知道  一棵树上,设某一段路在用DFS遍历路径过程中在该条路径遍历过的次数为 cnt,例如有这样一条路径:1 -> 3 -> 5 -> 2 ,那么 路 1 -> 3 的cnt为3,路 3 -> 5 的cnt为2,路 5 -> 2 的cnt为1,这样。我们要算 路 1 -> 3 对答案的贡献,那么设 路 1 -> 3的权值为 w, 它的贡献就是 cnt * ( n - cnt ) * w , 然后期望就是 所有的路的贡献加起来 * 2 / ( n * ( n - 1 ) ) 。
    这里用 Kruskal 算法比较好,复杂度低,也利于建最小生成树的图。
     
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <iostream>
     5 #include <cmath>
     6 #include <vector>
     7 using namespace std;
     8 #define N 100005
     9 #define M 1000005
    10 typedef pair <int, int> P;
    11 struct node
    12 {
    13     int u, v, w;
    14 }e[M*2];
    15 vector <P> edge[N];
    16 
    17 int n, m;
    18 double ans2;
    19 int fa[N];
    20 
    21 bool cmp(const node& e1, const  node& e2)
    22 {
    23     return e1.w < e2.w;
    24 }
    25 
    26 int Find(int x)
    27 {
    28     if(fa[x] == x) return x;
    29     return fa[x] = Find(fa[x]);
    30 }
    31 
    32 void Union(int x, int y)
    33 {
    34     int fx = Find(x), fy = Find(y);
    35     if(fx != fy) fa[fx] = fy;
    36 }
    37 
    38 long long Kruskal()
    39 {
    40     for(int i = 1; i <= n; i++)
    41         fa[i] = i;
    42     for(int i = 0; i <= n; i++)
    43         edge[i].clear();
    44 
    45     sort(e, e + m*2, cmp);
    46     long long res = 0;
    47     for(int i = 0; i < m*2; i++) {
    48         int u = e[i].u, v = e[i].v, w = e[i].w;
    49         int fu = Find( u ), fv = Find( v );
    50         if(fu != fv) {
    51             Union( u, v );
    52             res += e[i].w;
    53             edge[u].push_back(P(v, w));
    54             edge[v].push_back(P(u, w));
    55             //建最小生成树的图
    56         }
    57     }
    58     return res;
    59 }
    60 
    61 int dfs(int u, int fa)
    62 {
    63     int cnt = 0;
    64     for(int i = 0; i < edge[u].size(); i++) {
    65         int v = edge[u][i].first , w = edge[u][i].second;
    66         if( fa != v ) {
    67             int now = dfs(v, u);
    68             cnt += now;
    69             ans2 = ans2 + 1.0 * now * (n - now) * w;
    70             //算每段路的贡献
    71         }
    72     }
    73     return cnt + 1;
    74 }
    75 
    76 int main()
    77 {
    78     int t;
    79     scanf("%d", &t);
    80     while(t--) {
    81         scanf("%d%d", &n, &m);
    82         for(int i = 0; i < m; i++) {
    83             scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].w);
    84             e[m+i].v = e[i].u;
    85             e[m+i].u = e[i].v;
    86             e[m+i].w = e[i].w;
    87         }
    88 
    89         long long ans1 = Kruskal();
    90         ans2 = 0;
    91         dfs(1, -1);
    92         ans2 = ans2 * 2.0 / (1.0 * n) / (n - 1.0);
    93 
    94         printf("%I64d %.2f
    ", ans1, ans2);
    95     }
    96     return 0;
    97 }
  • 相关阅读:
    predis操作
    mysql 笔记(转载)
    mysql 汉字根据首字母排序
    sql 优化
    update多条不同数据
    解决network is unreachable问题
    开启服务器端口
    数据库开启远程访问
    激活2021.2.1idea
    python向excel追加数据
  • 原文地址:https://www.cnblogs.com/fightfordream/p/5688515.html
Copyright © 2011-2022 走看看