• Source
    1. #include <bits/stdc++.h>
    2. using namespace std;
    3.  
    4. #define BRANCO 0
    5. #define CINZA 1
    6. #define PRETO 2
    7.  
    8. typedef pair<int, pair<int, int> > ip;
    9.  
    10. vector<ip> MST;
    11. vector<ip> arestas;
    12. vector<ip> outMST;
    13. vector<int> graph[502];
    14. int pai[502], peso[502], cor[502], firstRead, cost1, cost2 = -1;
    15. int v, e, x, y, w;
    16. bool trouve;
    17.  
    18. void dfs(int vertice)
    19. {
    20. cor[vertice] = CINZA;
    21.  
    22. for (int i = 0; i < graph[vertice].size(); i++) {
    23. if (cor[graph[vertice][i]] == BRANCO)
    24. dfs(graph[vertice][i]);
    25. }
    26. }
    27.  
    28. bool DFS()
    29. {
    30. for (int i = 1; i <= v; i++) {
    31. if (cor[i] == BRANCO) {
    32. if (i > 1)
    33. return false;
    34. dfs(i);
    35. }
    36. }
    37. return true;
    38. }
    39.  
    40. int FIND_SET(int v)
    41. {
    42. if (pai[v] != v)
    43. pai[v] = FIND_SET(pai[v]);
    44. return pai[v];
    45. }
    46.  
    47. void UNION(int v, int u)
    48. {
    49. v = FIND_SET(v);
    50. u = FIND_SET(u);
    51.  
    52. if (u == v)
    53. return;
    54. if (peso[u] > peso[v])
    55. pai[v] = u;
    56. else if (peso[v] > peso[u])
    57. pai[u] = v;
    58. else {
    59. pai[v] = u;
    60. peso[u]++;
    61. }
    62. }
    63.  
    64. int main()
    65. {
    66. scanf("%d%d", &v, &e);
    67.  
    68. for (int i = 1; i <= v; i++) {
    69. pai[i] = i;
    70. cor[i] = BRANCO;
    71. }
    72.  
    73. ip aresta;
    74. for (int i = 1; i <= e; i++) {
    75. scanf("%d%d%d", &x, &y, &w);
    76. aresta = make_pair(w, make_pair(x, y));
    77. arestas.push_back(aresta);
    78. }
    79.  
    80. stable_sort(arestas.begin(), arestas.end());
    81.  
    82. int v1, v2;
    83. for (int i = 0; i < arestas.size(); i++) {
    84. v1 = arestas[i].second.first;
    85. v2 = arestas[i].second.second;
    86. if (FIND_SET(v1) != FIND_SET(v2)) {
    87. MST.push_back(arestas[i]);
    88. UNION(v1, v2);
    89. }
    90. else
    91. outMST.push_back(arestas[i]);
    92. }
    93.  
    94. for (int i = 0; i < MST.size(); i++) {
    95. cost1 += MST[i].first;
    96. graph[MST[i].second.first].push_back(MST[i].second.second);
    97. graph[MST[i].second.second].push_back(MST[i].second.first);
    98. }
    99.  
    100. for (int i = MST.size() - 1; i >= 0; i--) {
    101.  
    102. for (int j = 0; j < graph[MST[i].second.first].size(); j++) {
    103. if (graph[MST[i].second.first][j] == MST[i].second.second) {
    104. graph[MST[i].second.first].erase(graph[MST[i].second.first].begin() + j);
    105. break;
    106. }
    107. }
    108.  
    109. for (int j = 0; j < graph[MST[i].second.second].size(); j++) {
    110. if (graph[MST[i].second.second][j] == MST[i].second.first) {
    111. graph[MST[i].second.second].erase(graph[MST[i].second.second].begin() + j);
    112. break;
    113. }
    114. }
    115.  
    116. int ind = lower_bound(outMST.begin(), outMST.end(), MST[i]) - outMST.begin();
    117.  
    118. for (int j = ind; j < outMST.size(); j++) {
    119. graph[outMST[j].second.first].push_back(outMST[j].second.second);
    120. graph[outMST[j].second.second].push_back(outMST[j].second.first);
    121. if (DFS()) {
    122. cost2 = cost1 + outMST[j].first - MST[i].first;
    123. trouve = true;
    124. goto fin;
    125. }
    126. else {
    127. for (int z = 1; z <= v; z++)
    128. cor[z] = BRANCO;
    129. graph[outMST[j].second.first].pop_back();
    130. graph[outMST[j].second.second].pop_back();
    131. }
    132. }
    133.  
    134. graph[MST[i].second.first].push_back(MST[i].second.second);
    135. graph[MST[i].second.second].push_back(MST[i].second.first);
    136. }
    137.  
    138. fin:
    139.  
    140. if (!trouve) {
    141. for (int j = 0; j < outMST.size(); j++) {
    142. graph[outMST[j].second.first].push_back(outMST[j].second.second);
    143. graph[outMST[j].second.second].push_back(outMST[j].second.first);
    144. if (DFS())
    145. cost2 = cost1 + outMST[j].first;
    146. else {
    147. for (int z = 1; z <= v; z++)
    148. cor[z] = BRANCO;
    149. graph[outMST[j].second.first].pop_back();
    150. graph[outMST[j].second.second].pop_back();
    151. }
    152. }
    153. }
    154.  
    155. printf("Cost: %d\n", cost1);
    156. printf("Cost: %d\n", cost2);
    157. return 0;
    158. }
    159.