Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- //const int inf = 100000;
- using namespace std;
- class Graph {
- public:
- Graph( int n );
- ~Graph() = default;
- void UpdateMatrix( int from, int to, double weight );
- void Floyd();
- bool HasArbitrage();
- private:
- vector<vector<double>> graph;
- };
- Graph::Graph( int n ) : graph(n, vector<double>(n)) {}
- void Graph::UpdateMatrix( int from, int to, double weight ) {
- graph[from][to] = weight;
- }
- void Graph::Floyd() {
- int n = (int)graph.size();
- for( int i = 0; i < n; i++ )
- for (int j = 0; j < n; j++ )
- for( int k = 0; k < n; k++ )
- if( graph[j][i] > 0 && graph[i][k] > 0)
- graph[j][k] = max(graph[j][k], graph[j][i] * graph[i][k]);
- }
- bool Graph::HasArbitrage() {
- for( int i = 0; i < graph.size(); i++ ) {
- if( graph[i][i] > 1 ) {
- return true;
- }
- }
- return false;
- }
- int main() {
- int n = 0;
- cin >> n;
- Graph graph(n);
- for( int i = 0; i < n; i ++ ) {
- graph.UpdateMatrix(i, i, 1.0);
- for( int j = 0; j < n; j++ ) {
- double weight = 0;
- if( j != i ) {
- cin >> weight;
- graph.UpdateMatrix(j, i, weight);
- }
- }
- }
- if( graph.HasArbitrage() ) {
- cout << "YES";
- } else {
- cout << "NO";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement