Source code for submission s843

fr.cpp

  1. #include <cstdio>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. struct Node
  6. {
  7. int num;
  8. int effort;
  9. int min;
  10. vector<int> children;
  11. int parent;
  12.  
  13. Node( int n = 0, int p = 0 )
  14. {
  15. num = n;
  16. parent = p;
  17. }
  18.  
  19. void add( int n )
  20. {
  21. children.push_back(n);
  22. }
  23.  
  24. int ccount()
  25. {
  26. return children.size();
  27. }
  28. };
  29.  
  30. int bublej( Node * node, Node ** graph, int parent )
  31. {
  32. if( node->ccount() == 1 )
  33. {
  34. //printf( "jsem %d a vracim %d\n", node->num, node->effort );
  35. return node->effort;
  36. }
  37.  
  38. //printf( "jsem %d a prisel jsem z %d\n", node->num, parent );
  39.  
  40. int sum = 0;
  41. for( int i = 0; i < node->ccount(); i++ )
  42. {
  43. if( node->children[i] == parent )
  44. continue;
  45.  
  46. int b = bublej( graph[ node->children[i]-1 ], graph, node->num );
  47.  
  48. //printf( "sum (%d) += %d\n", sum, b );
  49. sum += b;
  50. }
  51.  
  52. //printf( "jsem %d, mam %d a od deti mam %d\n", node->num, node->effort, sum );
  53. if( sum < node->effort )
  54. node->effort = sum;
  55.  
  56. return node->effort;
  57. }
  58.  
  59. int main()
  60. {
  61. int nodec, center;
  62. while( scanf("%d %d\n", &nodec, &center) == 2 )
  63. {
  64. Node ** nodeArr = new Node*[nodec];
  65. for( int i = 0; i < nodec; i++ )
  66. nodeArr[i] = new Node(i+1);
  67.  
  68. nodeArr[center-1]->effort = 2000;
  69.  
  70. for( int i = 0; i < nodec-1; i++ )
  71. {
  72. int n,c,e;
  73. scanf( "%d %d %d\n", &n,&c,&e );
  74.  
  75. if( c != center )
  76. nodeArr[c-1]->effort = e;
  77. else
  78. nodeArr[n-1]->effort = e;
  79.  
  80. nodeArr[n-1]->add(c);
  81. nodeArr[c-1]->add(n);
  82. }
  83.  
  84. /*
  85. for( int i = 0; i < nodec; i++ )
  86. {
  87. printf( "Node%d (%d) : ", nodeArr[i]->num, nodeArr[i]->effort );
  88. for( int j = 0; j < nodeArr[i]->ccount(); j++ )
  89. printf( " %d", nodeArr[i]->children[j] );
  90. printf( "\n" );
  91. }
  92. */
  93.  
  94. int result = bublej( nodeArr[center-1], nodeArr, 0 );
  95. printf( "%d\n", result );
  96. }
  97.  
  98. return 0;
  99. }
  100.