Source code for submission s525

fr.cpp

  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4.  
  5. using namespace std;
  6.  
  7. class CNode
  8. {
  9. public:
  10. vector< CNode*> nexts;
  11. int prize;
  12.  
  13. CNode ( int p )
  14. : prize( p ) {}
  15.  
  16. void insert ( CNode * toInsert )
  17. {
  18. nexts.push_back( toInsert );
  19. }
  20.  
  21. int getCount ( void )
  22. {
  23. int len = nexts.size(), sum = 0;
  24.  
  25. if ( len == 0 ) {
  26. return prize;
  27. }
  28.  
  29. for ( int i = 0; i < len; i++ ) {
  30. sum += nexts[ i ]->getCount();
  31. }
  32.  
  33. return prize > sum ? sum : prize;
  34. }
  35. };
  36.  
  37. int main ( void )
  38. {
  39. int N, C;
  40.  
  41. while ( cin >> N >> C ) {
  42. map<int, CNode* > nodes;
  43.  
  44. nodes[ C ] = new CNode ( 10000 );
  45.  
  46. int U, V, W;
  47. for ( int i = 0; i < N - 1; i++ ) {
  48. cin >> U >> V >> W;
  49.  
  50. if ( nodes.find(U) == nodes.end() ) {
  51. int tmp = U;
  52. U = V;
  53. V = tmp;
  54. }
  55.  
  56. CNode * n = new CNode ( W );
  57. nodes[ U ] -> insert( n );
  58. nodes[ V ] = n;
  59. }
  60.  
  61. cout << nodes[ C ]->getCount() << endl;
  62. }
  63.  
  64. return 0;
  65. }
  66.  
  67.