zoukankan      html  css  js  c++  java
  • HDU 4126 Genghis Khan the Conqueror (树形DP+MST)

    题意:给一图,n个点,m条边,每条边有个花费,给出q条可疑的边,每条边有新的花费,每条可疑的边出现的概率相同,求不能经过原来可疑边

    (可以经过可疑边新的花费构建的边),注意每次只出现一条可疑的边,n个点相互连通的最小花费的期望。

    析:要想连通先让他们连通起来,先构造出一个MST,然后再暴力,如果这个边不在这里面,那么花费不变,如果在里面,那我们需要知道是用原来的边最少,

    还是再找一条边使他们连通起来,这里就要先预处理了,dp[i]j[i] 表示 左边的那个一半 i 和 右边那一半 j 的最长距离,如果我们知道了,就可以用这个来比较了,

    我们要对MST进行 n 次更新,每次遍历是 n,所以时间复杂度是 O(n*n),可以实现。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    //#include <tr1/unordered_map>
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    //using namespace std :: tr1;
    
    typedef long long LL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const double inf = 0x3f3f3f3f3f3f;
    const LL LNF = 0x3f3f3f3f3f3f;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 3e3 + 5;
    const LL mod = 10000000000007;
    const int N = 1e6 + 5;
    const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
    const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
    const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    inline LL gcd(LL a, LL b){  return b == 0 ? a : gcd(b, a%b); }
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline int Min(int a, int b){ return a < b ? a : b; }
    inline int Max(int a, int b){ return a > b ? a : b; }
    inline LL Min(LL a, LL b){ return a < b ? a : b; }
    inline LL Max(LL a, LL b){ return a > b ? a : b; }
    inline bool is_in(int r, int c){
        return r >= 0 && r < n && c >= 0 && c < m;
    }
    struct Node{
        int u, v, val;
        Node() { }
        Node(int uu, int vv, int va) : u(uu), v(vv), val(va) { }
        bool operator < (const Node &p) const {
            return val < p.val;
        }
    };
    struct Edge{
        int to, next;
    };
    Edge edge[maxn<<1];
    Node a[maxn*maxn];
    int p[maxn], dist[maxn][maxn], head[maxn];
    int dp[maxn][maxn];
    bool is_tree[maxn][maxn];
    int cnt, sum;
    
    int Find(int x) { return x == p[x] ? x : p[x] = Find(p[x]); }
    
    void add(int u, int v){
        edge[cnt].to = v;
        edge[cnt].next = head[u];
        head[u] = cnt++;
    }
    
    void Kruskal(){
        sort(a, a+m);
        int cnt = 0;
        sum = 0;
        for(int i = 0; i < m; ++i){
            int x = Find(a[i].u);
            int y = Find(a[i].v);
            if(x != y){
                p[y] = x;
                add(a[i].u, a[i].v);
                add(a[i].v, a[i].u);
                is_tree[a[i].u][a[i].v] = is_tree[a[i].v][a[i].u] = true;
                sum += a[i].val;
                ++cnt;
            }
            if(cnt == n-1) break;
        }
    }
    
    int dfs(int u, int fa, int root){
        int ans = fa == root ? INF : dist[root][u]; //i d scf h
    
        for(int i = head[u]; ~i; i = edge[i].next){
            int v = edge[i].to;
            if(v == fa)  continue;
            int tmp = dfs(v, u, root);
            ans = Min(ans, tmp);
            dp[u][v] = dp[v][u] = Min(dp[u][v], tmp);
        }
        return ans;
    }
    
    int main(){
        while(scanf("%d %d", &n, &m) == 2 && m+n){
            for(int i = 0; i < n; ++i) p[i] = i;
            int u, v, c;
            memset(dist, INF, sizeof dist);
            for(int i = 0; i < m; ++i){
                scanf("%d %d %d", &u, &v, &c);
                a[i] = Node(u, v, c);
                dist[u][v] = dist[v][u] = c;
            }
    
            memset(head, -1, sizeof head);
            memset(is_tree, false, sizeof is_tree);
            cnt = 0;
            Kruskal();
            memset(dp, INF, sizeof dp);
            for(int i = 0; i < n; ++i)  dfs(i, -1, i);
    
            scanf("%d", &m);
            double ans = 0.0;
            for(int i = 0; i < m; ++i){
                scanf("%d %d %d", &u, &v, &c);
                if(!is_tree[u][v])  ans += sum;
                else  ans += sum - dist[u][v] + Min(c, dp[u][v]);
            }
            printf("%.4f
    ", ans/m);
        }
        return 0;
    }
    
  • 相关阅读:
    k3 cloud中获取自己开发的单据
    k3 cloud列表中出现很多空白
    k3 cloud支付申请单下推付款单时候提示未将对象引用设置到对象的实例
    k3 cloud出现应收单下推收款单,把收款单是结算方式修改成银行承兑汇票之后保存提示:收款单明细中结算方式为票据业务的实收金额之和不等于票据的当前占用金额之和,请检查数据!
    k3 cloud中出现合计和汇总以后没有显示出来,合价要新增一行以后才出现值
    共享打印机
    k3 cloud总账凭证点击保存的时候提示未将对象引用到对应的实例
    k3 cloud成本调整单引入单据后,再做出库成本核算。成本调整单列表已审核的单据消失,非已审核的单据还在,这是出库成本核算设置参数的问题吗?
    K3 cloud选单时候必须把必录的数据录完以后才可以选单
    k3 cloud成本调整单提示期末余额不存在调整单分录的维度,请先出库核算确认是否存在核算维度的数据
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/5935004.html
Copyright © 2011-2022 走看看