fr.cpp
#include <iostream>
#include <vector>
#include <map>
using namespace std;
class CNode
{
public:
vector< CNode*> nexts;
int prize;
CNode ( int p )
: prize( p ) {}
void insert ( CNode * toInsert )
{
nexts.push_back( toInsert );
}
int getCount ( void )
{
int len = nexts.size(), sum = 0;
if ( len == 0 ) {
return prize;
}
for ( int i = 0; i < len; i++ ) {
sum += nexts[ i ]->getCount();
}
return prize > sum ? sum : prize;
}
};
int main ( void )
{
int N, C;
while ( cin >> N >> C ) {
map<int, CNode* > nodes;
nodes[ C ] = new CNode ( 10000 );
int U, V, W;
for ( int i = 0; i < N - 1; i++ ) {
cin >> U >> V >> W;
if ( nodes.find(U) == nodes.end() ) {
int tmp = U;
U = V;
V = tmp;
}
CNode * n = new CNode ( W );
nodes[ U ] -> insert( n );
nodes[ V ] = n;
}
cout << nodes[ C ]->getCount() << endl;
}
return 0;
}