Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4.  
  5. using std::vector;
  6.  
  7. vector<int> graph[60000];
  8. bool used[60000];
  9. int timer, tin[60000], fup[60000];
  10. long long answer = 1000000000000;
  11. std::map<std::pair<int, int>, long long> costs;
  12.  
  13. void dfs(int vertex, int parent = -1) {
  14. used[vertex] = true;
  15. tin[vertex] = fup[vertex] = timer++;
  16. for (size_t i = 0; i < graph[vertex].size(); ++i) {
  17. int to = graph[vertex][i];
  18. if (to == parent) continue;
  19. if (used[to])
  20. fup[vertex] = std::min(fup[vertex], tin[to]);
  21. else {
  22. dfs(to, vertex);
  23. fup[vertex] = std::min(fup[vertex], fup[to]);
  24. if (fup[to] > tin[vertex]) {
  25. answer = std::min(answer, costs[std::make_pair(vertex, to)]);
  26. }
  27. }
  28. }
  29. }
  30.  
  31. void find_bridges(int n_size) {
  32. timer = 0;
  33. for (int i = 0; i < n_size; ++i)
  34. used[i] = false;
  35. for (int i = 0; i < n_size; ++i)
  36. if (!used[i])
  37. dfs(i);
  38. }
  39.  
  40. int main() {
  41. int n_size, m_size;
  42. std::cin >> n_size >> m_size;
  43.  
  44. int a_city, b_city, cost;
  45. while (m_size--) {
  46. std::cin >> a_city >> b_city >> cost;
  47. graph[a_city - 1].push_back(b_city - 1);
  48. graph[b_city - 1].push_back(a_city - 1);
  49. costs[std::make_pair(a_city - 1, b_city - 1)] = cost;
  50. costs[std::make_pair(b_city - 1, a_city - 1)] = cost;
  51.  
  52. }
  53. find_bridges(n_size);
  54. if (answer == 1000000000000) {
  55. answer = -1;
  56. }
  57. std::cout << answer;
  58.  
  59. return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement