Source code for submission s953

Go to diff to previous submission

spider.cpp

  1. #include <algorithm>
  2. #include <cctype>
  3. #include <cmath>
  4. #include <complex>
  5. #include <cstdio>
  6. #include <cstring>
  7. #include <iomanip>
  8. #include <iostream>
  9. #include <list>
  10. #include <map>
  11. #include <queue>
  12. #include <set>
  13. #include <sstream>
  14. #include <stack>
  15. #include <string>
  16. #include <utility>
  17. #include <vector>
  18. using namespace std;
  19.  
  20. #define DEBUG(x) cout << ">>> " << #x << " : " << x << endl;
  21. #define REP(i,a) for (int i = 0; i < (a); ++i)
  22. #define FOR(i,a,b) for (int i = (a); i <= (b); ++i)
  23. #define FORD(i,a,b) for (int i = (a); i >= (b); --i)
  24. inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
  25.  
  26. const int INF = 1<<29;
  27. typedef long long ll;
  28. ///////////////////////////////////////////////////////////////////////////
  29.  
  30. const int MAX = 2007;
  31. int N, M;
  32. int parent[MAX];
  33.  
  34. int get(int n)
  35. {
  36. return parent[n] = (n == parent[n] ? n : get(parent[n]));
  37. }
  38.  
  39. bool joined(int a, int b)
  40. {
  41. a = get(a);
  42. b = get(b);
  43. return a == b;
  44. }
  45.  
  46. void join(int a, int b)
  47. {
  48. a = get(a);
  49. b = get(b);
  50. parent[a] = b;
  51. }
  52.  
  53. struct Edge
  54. {
  55. int a, b, c;
  56. bool operator<(const Edge & e) const { return c < e.c; }
  57. } edges[1000000];
  58.  
  59. vector<pair<int, int> > tree[MAX];
  60. int dist[MAX][MAX];
  61.  
  62. void go(int root, int node, int parent, int best)
  63. {
  64. dist[root][node] = best;
  65. REP(i, tree[node].size())
  66. {
  67. int next = tree[node][i].first, c = tree[node][i].second;
  68. if (next != parent)
  69. go(root, next, node, max(best, c));
  70. }
  71. }
  72.  
  73. int main()
  74. {
  75. while (scanf("%d%d", &N, &M) == 2)
  76. {
  77. REP(i, N)
  78. {
  79. parent[i] = i;
  80. tree[i].clear();
  81. }
  82.  
  83. REP(i, M)
  84. {
  85. scanf("%d%d%d", &edges[i].a, &edges[i].b, &edges[i].c);
  86. --edges[i].a;
  87. --edges[i].b;
  88. }
  89. sort(edges, edges+M);
  90.  
  91. int used = 0, sum = 0;
  92. REP(i, M)
  93. if (!joined(edges[i].a, edges[i].b))
  94. {
  95. join(edges[i].a, edges[i].b);
  96. sum += edges[i].c;
  97. ++used;
  98. tree[edges[i].a].push_back(make_pair(edges[i].b, edges[i].c));
  99. tree[edges[i].b].push_back(make_pair(edges[i].a, edges[i].c));
  100. }
  101. if (used != N-1)
  102. {
  103. printf("disconnected\n");
  104. continue;
  105. }
  106.  
  107. REP(i, N) go(i, i, -1, 0);
  108. int result = 1000000000;
  109. REP(i, M)
  110. {
  111. int a = edges[i].a, b = edges[i].b;
  112. result = min(result, sum-dist[a][b]-edges[i].c);
  113. }
  114. printf("%d\n", result);
  115. }
  116. return 0;
  117. }
  118.  

Diff to submission s587

spider.cpp

--- c4.s587.cteam052.spider.cpp.0.spider.cpp
+++ c4.s953.cteam052.spider.cpp.0.spider.cpp
@@ -57,9 +57,28 @@
 } edges[1000000];
 
+vector<pair<int, int> > tree[MAX];
+int dist[MAX][MAX];
+
+void go(int root, int node, int parent, int best)
+{
+  dist[root][node] = best;
+  REP(i, tree[node].size())
+  {
+   int next = tree[node][i].first, c = tree[node][i].second;
+   if (next != parent)
+     go(root, next, node, max(best, c));
+  }
+}
+
 int main()
 {
   while (scanf("%d%d", &N, &M) == 2)
   {
-    REP(i, N) parent[i] = i;
+    REP(i, N)
+    {
+      parent[i] = i;
+      tree[i].clear();
+    }
+    
     REP(i, M)
     {
@@ -70,25 +89,28 @@
     sort(edges, edges+M);
     
-    int used = 0, result = 0;
-    if (M)
-    {
-      result -= edges[M-1].c;
-      join(edges[M-1].a, edges[M-1].b);
-      ++used;
-    }
-    REP(i, M-1)
-    {
+    int used = 0, sum = 0;
+    REP(i, M)
       if (!joined(edges[i].a, edges[i].b))
       {
         join(edges[i].a, edges[i].b);
-        result += edges[i].c;
+        sum += edges[i].c;
         ++used;
+        tree[edges[i].a].push_back(make_pair(edges[i].b, edges[i].c));
+        tree[edges[i].b].push_back(make_pair(edges[i].a, edges[i].c));
       }
-    }
-    
     if (used != N-1)
+    {
       printf("disconnected\n");
-    else
-      printf("%d\n", result);
+      continue;
+    }
+    
+    REP(i, N) go(i, i, -1, 0);
+    int result = 1000000000;
+    REP(i, M)
+    {
+        int a = edges[i].a, b = edges[i].b;
+        result = min(result, sum-dist[a][b]-edges[i].c);
+    }
+    printf("%d\n", result);
   }
   return 0;