GastonFontenla

N3P4 - Correo Central (Floyd Warshall)

Sep 1st, 2019
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. bool M[1001][1001];
  6.  
  7. int correocentral(int n, vector <int> a, vector <int> b)
  8. {
  9.     /**
  10.     Solución que apunta a subtareas 1, 2, 3
  11.     solo en caso que la respuesta sea n
  12.     80% de 90 puntos = 72 puntos
  13.    
  14.     Complejidad: O(n^3) =~ 1.000.000.000
  15.     Aunque es rápido debido a operaciones super ligeras (or, and)
  16.     **/
  17.  
  18.     for(int i=0; i<a.size(); i++)
  19.         M[a[i]][b[i]] = true;
  20.  
  21.     for(int i=1; i<=n; i++)
  22.         M[i][i] = true;
  23.  
  24.     ///Algoritmo Floyd-Warshall
  25.     ///para calcular alcanzabilidad
  26.     for(int k=1; k<=n; k++)
  27.         for(int i=1; i<=n; i++)
  28.             for(int j=1; j<=n; j++)
  29.                 M[i][j] = M[i][j] or (M[i][k] and M[k][j]);
  30.  
  31.     bool todosUnos = true;
  32.  
  33.     for(int i=1; i<=n; i++)
  34.         for(int j=1; j<=n; j++)
  35.             if(M[i][j] == false)
  36.                 todosUnos = false;
  37.  
  38.     if(todosUnos)
  39.         return n;
  40.     return 0;
  41. }
  42.  
  43. /**
  44. //Función main auxiliar para testear
  45. int main()
  46. {
  47.     int n, m;
  48.     cin >> n >> m;
  49.  
  50.     vector <int> a(m), b(m);
  51.  
  52.     for(int i=0; i<m; i++)
  53.         cin >> a[i] >> b[i];
  54.  
  55.     cout << correocentral(n, a, b) << endl;
  56.  
  57.     return 0;
  58. }
  59. **/
Advertisement
Add Comment
Please, Sign In to add comment