Advertisement
wurdalak007

Untitled

Mar 8th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. //const int inf = 100000;
  6. using namespace std;
  7.  
  8. class Graph {
  9. public:
  10. Graph( int n );
  11. ~Graph() = default;
  12. void UpdateMatrix( int from, int to, double weight );
  13. void Floyd();
  14. bool HasArbitrage();
  15. private:
  16. vector<vector<double>> graph;
  17. };
  18.  
  19. Graph::Graph( int n ) : graph(n, vector<double>(n)) {}
  20.  
  21. void Graph::UpdateMatrix( int from, int to, double weight ) {
  22. graph[from][to] = weight;
  23. }
  24.  
  25. void Graph::Floyd() {
  26. int n = (int)graph.size();
  27.  
  28. for( int i = 0; i < n; i++ )
  29. for (int j = 0; j < n; j++ )
  30. for( int k = 0; k < n; k++ )
  31. if( graph[j][i] > 0 && graph[i][k] > 0)
  32. graph[j][k] = max(graph[j][k], graph[j][i] * graph[i][k]);
  33. }
  34.  
  35. bool Graph::HasArbitrage() {
  36. for( int i = 0; i < graph.size(); i++ ) {
  37. if( graph[i][i] > 1 ) {
  38. return true;
  39. }
  40. }
  41. return false;
  42. }
  43.  
  44. int main() {
  45. int n = 0;
  46. cin >> n;
  47. Graph graph(n);
  48.  
  49. for( int i = 0; i < n; i ++ ) {
  50. graph.UpdateMatrix(i, i, 1.0);
  51. for( int j = 0; j < n; j++ ) {
  52. double weight = 0;
  53. if( j != i ) {
  54. cin >> weight;
  55. graph.UpdateMatrix(j, i, weight);
  56. }
  57. }
  58. }
  59.  
  60. if( graph.HasArbitrage() ) {
  61. cout << "YES";
  62. } else {
  63. cout << "NO";
  64. }
  65.  
  66. return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement