子树的决策问题一般都是树形, 定义状态表示
表示度数为
的点无法到达
的最小代价
- 情况
, 切断当前边代价为
- 情况
, 切断子树的某条边代价为
两种取最小值即可
#include <bits/stdc++.h>
#define x first
#define y second
#define all(x) x.begin(), x.end()
using namespace std;
using i128 = __int128;
using u128 = unsigned __int128;
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
const int N = 1e5 + 10, MOD = 998244353;
const int INF = 1e9;
const LL LL_INF = 1e18;
const LD EPS = 1e-8;
const int dx4[] = {-1, 0, 1, 0}, dy4[] = {0, 1, 0, -1};
const int dx8[] = {-1, -1, -1, 0, 0, 1, 1, 1}, dy8[] = {-1, 0, 1, -1, 1, -1, 0, 1};
istream &operator>>(istream &is, i128 &val) {
string str;
is >> str;
val = 0;
bool flag = false;
if (str[0] == '-') flag = true, str = str.substr(1);
for (char &c: str) val = val * 10 + c - '0';
if (flag) val = -val;
return is;
}
ostream &operator<<(ostream &os, i128 val) {
if (val < 0) os << "-", val = -val;
if (val > 9) os << val / 10;
os << static_cast<char>(val % 10 + '0');
return os;
}
void solve() {
int n, m, s;
cin >> n >> m >> s;
vector<vector<PII>> g(n + 1);
for (int i = 0; i < m; ++i) {
int a, b, w;
cin >> a >> b >> w;
g[a].push_back({b, w});
g[b].push_back({a, w});
}
vector<LL> f(n + 1);
function<void(int, int)> dfs = [&](int u, int fa) {
for (auto [v, w] : g[u]) {
if (v == fa) continue;
dfs(v, u);
if (g[v].size() == 1) f[u] += w;
else f[u] += min(1ll * w, f[v]);
}
};
dfs(s, 0);
cout << f[s] << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int T = 1;
while (T--) solve();
cout << fixed << setprecision(15);
return 0;
}

京公网安备 11010502036488号