zoukankan      html  css  js  c++  java
  • leetcode2065 最大化一张图中的路径价值

    思路:

    暴搜。

    实现:

     1 class Solution
     2 {
     3 public:
     4     int res = 0; 
     5     vector<int> vis;
     6     void dfs(int x, int time, int val, vector<int>& values, vector<vector<pair<int, int>>>& g)
     7     {
     8         if (x == 0) res = max(res, val);
     9         int n = g[x].size();
    10         int maxn = 0;
    11         for (int i = 0; i < n; i++)
    12         {
    13             auto tmp = g[x][i];
    14             int to = tmp.first, cost = tmp.second;
    15             if (cost > time) continue;
    16             if (!vis[to])
    17             {
    18                 vis[to] = true;
    19                 dfs(to, time - cost, val + values[to], values, g);
    20                 vis[to] = false;
    21             }
    22             else dfs(to, time - cost, val, values, g);
    23         }
    24     }
    25     int maximalPathQuality(vector<int>& values, vector<vector<int>>& edges, int maxTime)
    26     {
    27         res = values[0];
    28         int n = values.size();
    29         vis.resize(n, 0);
    30         int m = edges.size();
    31         vector<vector<pair<int, int>>> g(n, vector<pair<int, int>>());
    32         for (int i = 0; i < m; i++)
    33         {
    34             auto tmp = edges[i];
    35             int a = tmp[0], b = tmp[1], c = tmp[2];
    36             g[a].push_back({b, c});
    37             g[b].push_back({a, c});
    38         }
    39         dfs(0, maxTime, 0, values, g);
    40         return res;
    41     }
    42 };
  • 相关阅读:
    P5107 能量采集
    P4655 [CEOI2017]Building Bridges
    P1129 [ZJOI2007]矩阵游戏
    P5299 [PKUWC2018]Slay the Spire
    P1625求和 giao精大杂烩
    背包
    根号分治
    CF963B
    国王游戏
    P6006 USACO 3SUM G
  • 原文地址:https://www.cnblogs.com/wangyiming/p/15701660.html
Copyright © 2011-2022 走看看