• HDU 5242 Game (贪心)


    题意:给定一棵树,要求从根结点1走k次,每次都是到叶子结点结束,把走过的所有的结点权值加起来,最大是多少。

    析:先把每个结点到根结点的路径之和求出来,然后按权值从大到小排序,然后每次把路径中的权值求出来,最后求前k个值的和即可。

    代码如下:

    #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 <sstream>
    #define debug() puts("++++");
    #define gcd(a, b) __gcd(a, b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const LL LNF = 1e16;
    const double inf = 0x3f3f3f3f3f3f;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 1e5 + 10;
    const int mod = 1e9 + 7;
    const int dr[] = {-1, 0, 1, 0};
    const int dc[] = {0, 1, 0, -1};
    const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    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 bool is_in(int r, int c){
      return r >= 0 && r < n && c >= 0 && c < m;
    }
    struct Node{
      int v;
      LL val;
      bool operator < (const Node &p) const{
        return val > p.val;
      }
    };
    vector<Node> v;
    int p[maxn], a[maxn];
    LL dp[maxn];
    vector<int> G[maxn];
    
    void dfs(int u){
      for(int i = 0; i < G[u].size(); ++i){
        int vv = G[u][i];
        dp[vv] = dp[u] + a[vv];
        if(G[vv].size() == 0)  v.push_back((Node){vv, dp[vv]});
        dfs(vv);
        p[vv] = u;
      }
    }
    bool vis[maxn];
    
    int main(){
      int T;  cin >> T;
      for(int kase = 1; kase <= T; ++kase){
        scanf("%d %d", &n, &m);
        for(int i = 1; i <= n; ++i){
          scanf("%d", a+i);
          G[i].clear();
        }
        for(int i = 1; i < n; ++i){
          int u, v;
          scanf("%d %d", &u, &v);
          G[u].push_back(v);
        }
        dp[1] = a[1];
        v.clear();
        p[1] = 0;
        dfs(1);
        sort(v.begin(), v.end());
        vector<LL> tmp;
        memset(vis, 0, sizeof vis);
        for(int i = 0; i < v.size(); ++i){
          Node& u = v[i];
          int x = u.v;
          while(x && !vis[x]){
            vis[x] = 1;
            x = p[x];
          }
          tmp.push_back(u.val-dp[x]);
        }
        sort(tmp.begin(), tmp.end(), greater<LL>());
        LL ans = 0;
        int t = min((int)tmp.size(), m);
        for(int i = 0; i < t; ++i)  ans += tmp[i];
        printf("Case #%d: %lld
    ", kase, ans);
      }
      return 0;
    }
    

      

  • 相关阅读:
    js入门 关于js属性及其数据类型(详解)
    js入门关于js‘i++’‘++i’和‘i--’‘--i’计算的问题
    js入门关于函数
    js入门
    Canvas
    SVG
    H5表单属性
    移动式布局之弹性布局day1
    Mysql
    PHP抽象类和接口
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/6786068.html
Copyright © 2020-2023  润新知