fr.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int conn[2000][2000];
int nodes;
int count(int node, int from) {
int i, inputc= 1000000000, childc = 0;
//printf("count node %d from %d\n", node, from);
for (i=1; i<nodes+1; i++) {
if (i==from) {
inputc = conn[node][i];
} else {
//childc += conn[node][i];
if (conn[node][i] > 0) childc += count(i, node);
}
}
//printf("node %d from %d input %d child %d\n", node, from, inputc, childc);
if (childc == 0) return inputc;
return childc > inputc ? inputc : childc;
}
int main(int argc, char **argv)
{
int rv, root, i, n1, n2, w;
while (1) {
rv
= scanf("%d%d", &nodes
, &root
); if (rv != 2) break;
memset(conn
, 0, sizeof(conn
)); for (i=0; i<nodes-1; i++) {
scanf("%d%d%d", &n1
, &n2
, &w
); conn[n1][n2] = w;
conn[n2][n1] = w;
}
printf("%d\n", count
(root
, -1)); }
return 0;
}