Source code for submission s1044

Go to diff to previous submission

fr.cpp

  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <set>
  4. #include <map>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. typedef struct {
  10. map<int, int> post; // <node, cena>
  11. } mynode;
  12.  
  13. void vypis(set<int> &u) {
  14. printf("pouzite: ");
  15. for (set<int>::iterator it=u.begin(); it != u.end(); it++) {
  16. printf("%d ", *it);
  17. }
  18. printf("\n");
  19. }
  20.  
  21. int dotcena(mynode &g, set<int> &u, mynode *gr, bool nieSomCentrum) {
  22. if (g.post.size() == 1 && nieSomCentrum) { return 2000000; }
  23. int acc = 0;
  24. int oldAcc;
  25. for (map<int,int>::iterator it=g.post.begin(); it != g.post.end(); it++) {
  26. // vypis(u);
  27. if (u.find(it->first) == u.end()) {
  28. u.insert(it->first);
  29. oldAcc = acc;
  30. acc += min(it->second, dotcena(gr[it->first], u, gr, true));
  31. //printf("idem na %d s cenou %d, sec = %d \n", it->first, acc - oldAcc, it->second);
  32. }
  33. }
  34. return acc;
  35. };
  36.  
  37. int main()
  38. {
  39. int n, c;
  40. int i;
  41. int u, v, w;
  42. int v1, v2;
  43.  
  44. while (true) {
  45. if ((scanf("%d %d\n", &n, &c) < 2)) return 0;
  46.  
  47. mynode graf[1100];
  48. set<int> used;
  49.  
  50. used.insert(c);
  51.  
  52. if (n == 2) {
  53. scanf("%d %d %d\n", &u, &v, &w);
  54. printf("%d\n", w);
  55. } else {
  56. for (i = 0; i < n - 1; i++) {
  57. scanf("%d %d %d\n", &u, &v, &w);
  58. graf[u].post[v] = w;
  59. graf[v].post[u] = w;
  60.  
  61. //printf("%d %d\n", graf[u].post[v], graf[v].post[u]);
  62. }
  63.  
  64. printf("%d\n", dotcena(graf[c], used, graf, false));
  65. }
  66. }
  67. return 0;
  68. }

Diff to submission s1037

fr.cpp

--- c5.s1037.cteam082.fr.cpp.0.fr.cpp
+++ c5.s1044.cteam082.fr.cpp.0.fr.cpp
@@ -19,6 +19,6 @@
 }
 
-int dotcena(mynode &g, set<int> &u, mynode *gr) {
-        if (g.post.size() == 1) { return 2000000; }
+int dotcena(mynode &g, set<int> &u, mynode *gr, bool nieSomCentrum) {
+        if (g.post.size() == 1 && nieSomCentrum) { return 2000000; }
         int acc = 0;
         int oldAcc;
@@ -28,5 +28,5 @@
                         u.insert(it->first);
                         oldAcc = acc;
-                        acc += min(it->second, dotcena(gr[it->first], u, gr));
+                        acc += min(it->second, dotcena(gr[it->first], u, gr, true));
                         //printf("idem na %d s cenou %d, sec = %d \n", it->first, acc - oldAcc, it->second);
                 }
@@ -62,5 +62,5 @@
                         }
                 
-                        printf("%d\n", dotcena(graf[c], used, graf));
+                        printf("%d\n", dotcena(graf[c], used, graf, false));
                 }
         }