• HDU 5723 Abandoned country 最小生成树+搜索


    Abandoned country

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


    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
    /*
    HDU 5723 Abandoned country 最小生成树+搜索
    
    problem:
    给你n个点和m条边,让你求最少花费多少可以将所有点连通并求出任意两点的花费期望
    
    solve:
    第一个直接求最小生成树。主要是不懂它这个期望到底要求什么。看题解说的是深搜求出每条路用过
    的次数来得到总花费。然后除以可能发生的次数
    
    by——hhh
    */
    #include <algorithm>
    #include <iostream>
    #include <cstdlib>
    #include <cstdio>
    #include <cstring>
    #include <map>
    #define lson  ch[r][0]
    #define rson  ch[r][1]
    #define ll long long
    #define key_val ch[ch[root][1]][0]
    using namespace std;
    const int maxn = 100010;
    const int inf = 0x3f3f3f3f;
    int vis[maxn];
    int f[maxn];
    vector<pair<int,int>> q[maxn];
    struct Edge
    {
        int u,v,w;
    } edge[1000010];
    
    int tot;
    
    void add(int u,int v,int val)
    {
        edge[tot].u = u,edge[tot].v = v,edge[tot++].w = val;
    }
    
    bool cmp(Edge a,Edge b)
    {
        return a.w < b.w;
    }
    
    int fin(int x)
    {
        if(f[x] == -1) return x;
        return f[x] = fin(f[x]);
    }
    
    ll cal(int n)
    {
        memset(f,-1,sizeof(f));
        sort(edge,edge+tot,cmp);
        ll cnt = 0,ans = 0;
        for(int i = 0; i < tot; i++)
        {
            int u = edge[i].u;
            int v = edge[i].v;
            int w = edge[i].w;
            int t1 = fin(u),t2 = fin(v);
            if(t1 != t2)
            {
                ans = (ll)(ans + w);
                f[t1] = t2;
                cnt++;
                q[u].push_back(make_pair(v,w));
                q[v].push_back(make_pair(u,w));
    
            }
            if(cnt == n-1)
                break;
        }
        // cout << cnt <<endl;
        return ans;
    }
    int n;
    double ans;
    ll dfs(int now)
    {
        vis[now] = 1;
        ll t = 0,ta = 0;
        for(int i = 0; i < q[now].size(); i++)
        {
            ll v = q[now][i].first;
            ll w = q[now][i].second;
            if(!vis[v])
            {
                t = dfs(v);
                ta += t;
                ans = ans+1.0*t*(n-t)*w;
            }
        }
        return ta+1;
    }
    
    int main()
    {
        int T,a,c,b;
    //    freopen("in.txt","r",stdin);
        scanf("%d",&T);
        while(T--)
        {
            int m;
            ll tans;
            tot = 0,ans = 0;
            memset(vis,0,sizeof(vis));
            scanf("%d%d",&n,&m);
    
            for(int i =0; i <= n; i++)
                q[i].clear();
            for(int i = 1; i <= m; i++)
            {
                scanf("%d%d%d",&a,&b,&c);
                add(a,b,c);
            }
            if(!n || !m)
            {
                printf("0 0.00
    ");
                continue;
            }
            tans = cal(n);
            dfs(1);
            double t = (1.0*n*(n-1)/2);
    //        cout <<ans <<" " <<t<<endl;
            printf("%I64d %.2f
    ",tans,ans/t);
        }
        return 0;
    }
    

      

  • 相关阅读:
    简单说说数据库表设计的三种范式
    存储过程简单的动态订单号
    Asp.Net页面生命周期
    jq 小笔记,上传判断其格式
    吃一垫长一智
    离散事件模拟
    二叉树查找树
    冷暖自知
    基督徒的人生箴言
    迷宫寻路
  • 原文地址:https://www.cnblogs.com/Przz/p/5792156.html
Copyright © 2020-2023  润新知