腐败分子们实在是太懒了,我也懒一下
Code:
#include<iostream>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;
int minn = 99999999, n, sum, a[1001], zh, l, r;
vector<int> g[1001];
void dfs(int u, int fa, int jl) { //u:当前点fa:原节点jl:距离
sum += jl * a[u]; //艰难值
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (v == 0 || v == fa)continue; //如果是根或原节点,跳出
dfs(v, u, jl + 1);
}
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> zh >> l >> r; //输入
a[i] = zh; //赋值
g[i].push_back(l);
g[i].push_back(r);
//无向图,可以从其它点出发
g[l].push_back(i);
g[r].push_back(i);//因为可以到任意一个点,所以要反向存储
}
for (int i = 1; i <= n; i++) {
sum = 0; //一定要归零!!!我也被坑了!!!
dfs(i, 0, 0);
minn = min(sum, minn);
}//全部遍历,取最小
cout << minn;
return 0;
}
//稳住,相信自己能做对的