• HDU 5723 Abandoned country 【最小生成树&&树上两点期望】


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

    Abandoned country

    Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 7573    Accepted Submission(s): 1850


    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
     
    Author
    HIT

     

    题意概括:

    有一个 N 个点 M 条边的无向图,题目需要求最小生成树路径和, 以及在这颗树上任意两点的期望(任意两点概率都一样,两点的期望即: 两点路径的权值 / C(N,2)  )

    解题思路:

    求最小生成树:选择堆优化的Prim 或者 kruskal (都是 O(N log M)。

    如果分开单独考虑树上两点的期望值 很容易想到最短路,不过应该超时。

    总体来看,我们要求的是期望和,所以只要直到经过每条路径的次数,总路程就等于求和每一条路径花费乘以出现次数,方法是 dfs。

    最后算出 总的路程 / C(N, 2) 就可以了。

    tip:注意数据类型要用到 long long,期望定义为double.

    AC code:

      1 #include <cstdio>
      2 #include <iostream>
      3 #include <cstring>
      4 #include <cmath>
      5 #include <vector>
      6 #include <algorithm>
      7 #define INF 0x3f3f3f3f
      8 #define LL long long
      9 using namespace std;
     10 const int MAXN = 1e5+10;
     11 const int MAXM = 1e6+10;
     12 int ff[MAXN];
     13 bool vis[MAXN];
     14 vector<pair<int, int> > ttp[MAXN];
     15 int N, M;
     16 int T_case;
     17 LL ans2;
     18 LL ans;
     19 struct edge
     20 {
     21     int u, v, cost;
     22 }e[MAXM];
     23 
     24 bool cmp(const edge& e1, const edge& e2)
     25 {
     26     return e1.cost < e2.cost;
     27 }
     28 
     29 void init()
     30 {
     31     memset(e, 0,sizeof(e));
     32     memset(vis, false, sizeof(vis));
     33     for(int i = 0; i <= N; i++){ff[i] = i;}
     34     for(int i = 0; i <= N; i++) ttp[i].clear();
     35     ans = 0;
     36     ans2 = 0;
     37 }
     38 
     39 int ufind(int u)
     40 {
     41     if(u == ff[u]) return ff[u];
     42     ff[u] = ufind(ff[u]);
     43     return ff[u];
     44 }
     45 
     46 void unite(int u, int v)
     47 {
     48     u = ufind(u);
     49     v = ufind(v);
     50     if(u == v) return;
     51     ff[u] = v;
     52 }
     53 
     54 void kruskal()
     55 {
     56     ans = 0;
     57     sort(e, e+M, cmp);
     58     for(int i = 0; i < M; i++){
     59         edge es = e[i];
     60         if(ufind(es.u) != ufind(es.v)){
     61             unite(es.u, es.v);
     62             ans += es.cost;
     63             ttp[es.u].push_back(make_pair(es.v, es.cost));
     64             ttp[es.v].push_back(make_pair(es.u, es.cost));
     65         }
     66     }
     67 }
     68 
     69 LL dfs(int x)
     70 {
     71     vis[x] = true;
     72     LL now = 0, all = 1;
     73     for(int i = 0; i < ttp[x].size(); i++){
     74         int b = ttp[x][i].first;
     75         if(!vis[b]){
     76             now = dfs(b);
     77             all+=now;
     78             ans2+=now*(N-now)*ttp[x][i].second;
     79         }
     80     }
     81     return all;
     82 }
     83 
     84 int main()
     85 {
     86     scanf("%d", &T_case);
     87     while(T_case--){
     88         scanf("%d%d", &N, &M);
     89         init();
     90         for(int i = 0; i < M; i++){
     91             scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].cost);
     92         }
     93         kruskal();
     94         dfs(1);
     95         double zz = 1.0*N*(N-1)/2;
     96         //printf("%.2lf %lld
    ", zz, ans2);
     97         printf("%lld %.2lf
    ", ans, (double)ans2/zz);
     98         //double kk = 1.00/6+2.00/6+3.00/6+3.00/6+4.00/6+5.00/6;
     99         //printf("%.2lf
    ", kk);
    100     }
    101     return 0;
    102 }
    View Code
  • 相关阅读:
    使用fscanf读取文本文件
    实验室开发机系统结构图
    字符串是否为数字及有效性检查
    winXP系统通过蓝牙在笔记本和手机之间传递数据
    HarborGIS3D场景编辑器速成街区数据
    判断一个浮点数是否为NAN(INF)
    单文档中只是想得到当前View类的指针
    windows控制台中使用不同颜色显示不同类型的日志
    c++中enum 如何使用
    陈佩斯
  • 原文地址:https://www.cnblogs.com/ymzjj/p/9713964.html
Copyright © 2020-2023  润新知