#include using namespace std; typedef pair PII; const int N = 210; bool st[N]; //是不是走过了 int n, ans = 0x3f3f3f3f; struct Node { int left; //左儿子 int right; //右儿子 int value; //此结点的人数 int parent; //父亲是哪结点 } nodes[N]; //广度优先搜索 int bfs(int u) { //既然是bfs,当然需要把根结点先入队列 queue q; q.push({u, 0}); //以u为源点的路径权值和 int sum = 0; //开始广度优先搜索 while (!q.empty()) { PII c = q.front(); q.pop(); //自己产生的权值 sum += c.second * nodes[c.first].value; //标识已使用 st[c.first] = true; //左子树 if (nodes[c.first].left && !st[nodes[c.first].left]) q.push({nodes[c.first].left, c.second + 1}); //右子树 if (nodes[c.first].right && !st[nodes[c.first].right]) q.push({nodes[c.first].right, c.second + 1}); //父子树 if (nodes[c.first].parent && !st[nodes[c.first].parent]) q.push({nodes[c.first].parent, c.second + 1}); } return sum; } int main() { //读入数据 cin >> n; for (int i = 1; i <= n; i++) { //读入权值,左儿子,右儿子 cin >> nodes[i].value >> nodes[i].left >> nodes[i].right; //记录左右儿子的父亲节点 nodes[nodes[i].left].parent = i; //为了找到父亲 nodes[nodes[i].right].parent = i;//为了找到父亲 } //遍历每个结点作为医院部署地 for (int i = 1; i <= n; i++) { //每次出发前,注意要清空状态数组,防止状态记录错误 memset(st, 0, sizeof st); //每个结点作为医院部署地,都会产生一个距离和,取最小值 ans = min(ans, bfs(i)); } cout << ans << endl; return 0; }