Advertisement
Guest User

Untitled

a guest
May 26th, 2015
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <set>
  5.  
  6. using namespace std;
  7.  
  8. const int INF = 99999999;
  9.  
  10. void BellmanFord(vector<pair<int, pair<int, int> > >& g, vector<int>& d, int n, int k) {
  11. // Bellman-Ford
  12. d[k] = 0;
  13. for(int i = 0; i < n - 1; i++)
  14. for(int j = 0; j < static_cast<int>(g.size()); j++)
  15. if(d[g[j].first] < INF)
  16. d[g[j].second.first] = min(d[g[j].second.first], d[g[j].first] + g[j].second.second);
  17.  
  18. }
  19.  
  20. void Dijkstra(vector<pair<int, pair<int, int> > >& g, vector<int>& d, int n, int k) {
  21. vector<bool> used(n, false);
  22. used[k] = true;
  23. d[k] = 0;
  24. set<pair<int, int>> q;
  25. q.insert(make_pair(0, k));
  26. while(q.size() > 0) {
  27. pair<int, int> top = *q.begin();
  28. q.erase(top);
  29.  
  30. for(int i = 0; i < static_cast<int>(g.size()); i++)
  31. if(g[i].first == top.second && !used[g[i].second.first]) {
  32. q.erase(make_pair(d[g[i].second.first], g[i].second.first));
  33. d[g[i].second.first] = top.first + g[i].second.second;
  34. q.insert(make_pair(d[g[i].second.first], g[i].second.first));
  35. used[g[i].second.first] = true;
  36. }
  37. }
  38. }
  39.  
  40. bool CalcDistanceWithBellmanFord(vector<pair<int, pair<int, int> > >& g, vector<int>& d, int n, int k) {
  41. d.assign(n, INF);
  42. BellmanFord(g, d, n, k);
  43.  
  44. // check for negative cycles
  45. bool had_neg_cyc = false;
  46. for(int i = 0; i < static_cast<int>(g.size()); i++)
  47. if(d[g[i].second.first] > d[g[i].first] + g[i].second.second) {
  48. had_neg_cyc = true;
  49. break;
  50. }
  51. return !had_neg_cyc;
  52. }
  53.  
  54. bool CalcDistanceWithDijkstra(vector<pair<int, pair<int, int> > >& g, vector<int>& d, int n, int k) {
  55. // check for negative edges not incedent with k
  56. bool had_negative_edges = false;
  57. for(int i = 0; i < static_cast<int>(g.size()); i++)
  58. if(g[i].second.second < 0 && g[i].first != k) {
  59. had_negative_edges = true;
  60. break;
  61. }
  62.  
  63. if(had_negative_edges) return false;
  64.  
  65. d.assign(n, INF);
  66. Dijkstra(g, d, n, k);
  67. return true;
  68. }
  69.  
  70. int GetEdge(const vector<pair<int, pair<int, int> > >& g, int u, int v) {
  71. for(int i = 0; i < static_cast<int>(g.size()); i++)
  72. if(g[i].first == u && g[i].second.first == v)
  73. return g[i].second.second;
  74. return -1;
  75. }
  76.  
  77. int main() {
  78. //cout << "n, m? :" << endl;
  79. int n, m;
  80. cin >> n >> m;
  81. vector<pair<int, pair<int, int> > > g;
  82. int u, v, w;
  83. for(int i = 0; i < m; i++) {
  84. cin >> u >> v >> w;
  85. u--;
  86. v--;
  87. g.push_back(make_pair(u, make_pair(v, w)));
  88. }
  89.  
  90. //cout << "k?" << endl;
  91. int k;
  92. cin >> k;
  93. k--;
  94. vector<int> d;
  95.  
  96. // calc distance from k to all
  97. bool success = CalcDistanceWithBellmanFord(g, d, n, k);
  98. if(success) {
  99. cout << "distance vector(Bellman-Ford): " << endl;
  100. for (auto x : d)
  101. if(x != INF)
  102. cout << x << " ";
  103. else
  104. cout << "F" << " ";
  105. cout << endl;
  106. } else {
  107. cout << "We have negative cycles" << endl;
  108. }
  109.  
  110. // calc distance from u to v
  111. //cout << "u, v?" << endl;
  112. cin >> u >> v;
  113. u--;
  114. v--;
  115.  
  116. // check if exist edge (u, v)
  117. w = GetEdge(g, u, v);
  118. if(w > 0) {
  119. cout << "distance from " << u + 1 << " to " << v + 1 << " = " << w << endl;
  120.  
  121. } else {
  122. // calc distance with Bellman-Ford
  123. success = CalcDistanceWithBellmanFord(g, d, n, u);
  124. if (success)
  125. cout << "distance from" << u + 1 << " to " << v + 1 << " = " << d[v] << endl;
  126. else
  127. cout << "can't calc distance" << endl;
  128. }
  129.  
  130. // calc distance matrix from k
  131. success = CalcDistanceWithDijkstra(g, d, n, k);
  132. if(success) {
  133. cout << "distance vector(Dijkstra): " << endl;
  134. for (auto x : d)
  135. if(x != INF)
  136. cout << x << " ";
  137. else
  138. cout << "F" << " ";
  139. cout << endl;
  140. } else {
  141. cout << "graph had negative edges which not incedent with start vertrex" << endl;
  142. }
  143.  
  144. // calc distance from u to v
  145. //cout << "u, v?" << endl;
  146. cin >> u >> v;
  147. u--;
  148. v--;
  149. w = GetEdge(g, u, v);
  150. if(w > 0) {
  151. cout << "distance from " << u + 1 << " to " << v + 1 << " = " << w << endl;
  152. } else {
  153. // calc distance with Dijkstra
  154. success = CalcDistanceWithDijkstra(g, d, n, u);
  155. if (success)
  156. cout << "distance from " << u + 1 << " to " << v + 1 << " = " << d[v] << endl;
  157. else
  158. cout << "can't calc distance" << endl;
  159.  
  160. }
  161. return 0;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement