Source code for submission s833

fr.cpp

  1. //
  2. // File: f1.cc
  3. // Author: cteam029
  4. //
  5. // Created on October 19, 2013, 10:26 AM
  6. //
  7.  
  8. #include <cstdlib>
  9. #include <cstdio>
  10. #include <iostream>
  11. #include <vector>
  12. #include <map>
  13. #include <climits>
  14.  
  15. using namespace std;
  16.  
  17. struct Node{
  18. vector<int> off;
  19. int force;
  20. void Print(map <int, Node*> all){
  21. for(vector<int>::iterator it = off.begin(); it != off.end(); ++it){
  22. cout << *it << endl;
  23. all[*it]->Print(all);
  24. }
  25. }
  26. int Count(map <int, Node*> all){
  27. int res = 0;
  28. for(vector<int>::iterator it = off.begin(); it != off.end(); ++it){
  29. res += all[*it]->Count(all);
  30. }
  31. if(off.empty() || res > force){
  32. return force;
  33. }else{
  34. return res;
  35. }
  36. }
  37. };
  38. //
  39. //
  40. //
  41. int main(int argc, char** argv) {
  42. int count, central, start, end, force;
  43. Node *root;
  44. map <int, Node*> all;
  45. while(cin >> count >> central){
  46. root = new Node();
  47. root->force = INT_MAX;
  48. all.insert(pair<int, Node*>(central, root));
  49. count--;
  50. for(int i = 0; i < count; i++){
  51. cin >> start >> end >> force;
  52. map<int, Node*>::iterator it = all.find(start);
  53. if(it == all.end()){
  54. Node* s = new Node();
  55. s->force = force;
  56. all.insert(pair<int, Node*>(start, s));
  57. all[end]->off.push_back(start);
  58. }
  59. map<int, Node*>::iterator is = all.find(end);
  60. if(is == all.end()){
  61. Node* s= new Node();
  62. s->force = force;
  63. all.insert(pair<int, Node*>(end, s));
  64. all[start]->off.push_back(end);
  65. }
  66. }
  67. int res;
  68. res = root->Count(all);
  69. cout << res << endl;
  70. for(map<int, Node*>::iterator it = all.begin(); it != all.end(); ++it){
  71. delete (*it).second;
  72. }
  73. all.clear();
  74. }
  75. return 0;
  76. }
  77.  
  78.