Facebook
From Edmonds_Karp, 3 Years ago, written in C#.
Embed
Download Paste or View Raw
Hits: 141
  1.  class Program
  2.  {
  3.    static void Main(string[] args)
  4.    {
  5.      int w = 0;
  6.      
  7.      int m = 0, p = 0, n = 0;
  8.      int[,] C = null;
  9.      
  10.      do
  11.      {
  12.        Console.WriteLine("1 - Wpisz graf ręcznie w konsoli");
  13.        Console.WriteLine("2 - Wczytaj plik z grafem");
  14.        Console.WriteLine("3 - Zakończ działanie programu");
  15.        Console.Write("Wybór opcji: ");
  16.        int.TryParse(Console.ReadLine(),out w);
  17.        
  18.        bool f;
  19.        Console.WriteLine();
  20.        switch (w)
  21.        {
  22.          case 1:
  23.          f=WczytajDane(ref m, ref p, ref n, ref C);
  24.          if(f)
  25.          {
  26.            AlgorytmEdmondsaKarpa(m, p, n, C);
  27.          }
  28.          else
  29.          {
  30.            Console.WriteLine("Nie udało się poprawnie wpisać danych.");
  31.          }
  32.          break;
  33.          case 2:
  34.          f = WczytajPlik(ref m, ref p, ref n, ref C);
  35.          if(f)
  36.          {
  37.            AlgorytmEdmondsaKarpa(m, p, n, C);
  38.          }
  39.          else
  40.          {
  41.            Console.WriteLine("Nie udało się odczytać danych.");
  42.          }
  43.          break;
  44.        }
  45.        Console.WriteLine();
  46.      } while (w!=3);
  47.    }
  48.    
  49.    public static void AlgorytmEdmondsaKarpa(int m, int p, int n, int[,] C)
  50.    {
  51.      int[,] F = new int[n + 2, n + 2];
  52.      int fmax = 0;
  53.      Queue<int> Q = new Queue<int>();
  54.      
  55.      int[] P = new int[n + 2];
  56.      int[] CFP = new int[n + 2];
  57.      while (true)
  58.      {
  59.        for (int i = 0; i <= n + 1; i++) P[i] = -1;
  60.        
  61.        P[n] = -2;
  62.        CFP[n] = 9999999;
  63.        
  64.        Q.Clear();
  65.        Q.Enqueue(n);
  66.        
  67.        bool esc = false;
  68.        
  69.        while (Q.Count != 0)
  70.        {
  71.          int v = Q.Dequeue();
  72.          
  73.          
  74.          for (int u = 0; u <= n + 1; u++)
  75.          {
  76.            int cp = C[v, u] - F[v, u];
  77.            if ((cp != 0) && (P[u] == -1))
  78.            {
  79.              P[u] = v;
  80.              if (CFP[v] > cp) CFP[u] = cp; else CFP[u] = CFP[v];
  81.              if (u == n + 1)
  82.              {
  83.                fmax += CFP[n + 1];
  84.                int i = u;
  85.                while (i != n)
  86.                {
  87.                  v = P[i];
  88.                  F[v, i] += CFP[n + 1];
  89.                  F[i, v] -= CFP[n + 1];
  90.                  i = v;
  91.                }
  92.                esc = true; break;
  93.              }
  94.              Q.Enqueue(u);
  95.            }
  96.          }
  97.          if (esc) break;
  98.        }
  99.        if (!esc) break;
  100.      }
  101.      
  102.      Console.WriteLine();
  103.      Console.WriteLine("Wynik:");
  104.      
  105.      if (fmax > 0)
  106.        for (int v = m; v < n; v++)
  107.        {
  108.          bool znaleziono = false;
  109.          for (int u = 0; u < n; u++)
  110.            if ((C[v, u] == 1) && (F[v, u] == 1))
  111.            {
  112.              int nrM = u + 1;
  113.              int nrP = v - m + 1;
  114.              Console.WriteLine("Pracownik nr {0} - maszyna {1}", nrP, nrM);
  115.              znaleziono = true;
  116.              break;
  117.            }
  118.          if (znaleziono == false)
  119.          {
  120.            int nrP = v - m + 1;
  121.            Console.WriteLine("Pracownik nr {0} - brak maszyny", nrP);
  122.          }
  123.        }
  124.      
  125.      Console.WriteLine();
  126.      Console.Write("Kliknij dowolny przycisk aby kontynuować...");
  127.      Console.ReadKey();
  128.      Console.WriteLine();
  129.    }
  130.    
  131.    public static bool WczytajDane(ref int m, ref int p, ref int n, ref int[,] C)
  132.    {
  133.      Console.Write("Podaj ilość maszyn: ");
  134.      if (!int.TryParse(Console.ReadLine(), out m)) return false;
  135.      Console.Write("Podaj ilość pracowników: ");
  136.      if (!int.TryParse(Console.ReadLine(), out p)) return false;
  137.      n = p + m;
  138.      C = new int[n + 2, n + 2];
  139.      
  140.      for (int i = 0; i < m; i++)
  141.      {
  142.        C[i, n + 1] = 1;
  143.      }
  144.      for (int i = 0; i < p; i++)
  145.      {
  146.        Console.WriteLine("Podaj numery maszyn, które może obsługiwać pracownik nr {0} (Oddzielone spacją, zatwierdź enterem)", i + 1);
  147.        string[] wejscie = Console.ReadLine().Split(' ');
  148.        int tmp = 0;
  149.        for (int j = 0; j < wejscie.Length; j++)
  150.        {
  151.          if (wejscie[j] == "" || int.Parse(wejscie[j]) > m || int.Parse(wejscie[j]) < 0)
  152.            continue;
  153.          if (!int.TryParse(wejscie[j], out tmp)) return false;
  154.          C[m + i, tmp - 1] = 1;
  155.        }
  156.        C[m + i, n] = 1;
  157.      }
  158.      for (int i = 0; i < p; i++)
  159.      {
  160.        C[n, i + m] = 1;
  161.      }
  162.      
  163.      return true;
  164.    }
  165.    
  166.    public static bool WczytajPlik(ref int m,ref int p,ref int n, ref int[,]C )
  167.    {
  168.      string nazwa="";
  169.      Console.Write("Wpisz nazwę pliku: ");
  170.      nazwa = Console.ReadLine();
  171.      StreamReader sr=null;
  172.      
  173.      try
  174.      {
  175.        sr = new StreamReader(nazwa + ".txt");
  176.      }
  177.      catch (Exception)
  178.      {
  179.        Console.WriteLine("Nie znaleziono pliku.");
  180.        return false;
  181.      }
  182.      
  183.      
  184.      if (sr != null)
  185.      {
  186.        if (!int.TryParse(sr.ReadLine(), out m)) return false;
  187.        if (!int.TryParse(sr.ReadLine(), out p)) return false;
  188.        n = p + m;
  189.        C = new int[n + 2, n + 2];
  190.        
  191.        for (int i = 0; i < m; i++)
  192.        {
  193.          C[i, n + 1] = 1;
  194.        }
  195.        for (int i = 0; i < p; i++)
  196.        {
  197.          string[] wejscie = sr.ReadLine().Split(' ');
  198.          int tmp = 0;
  199.          for (int j = 0; j < wejscie.Length; j++)
  200.          {
  201.            if (wejscie[j] == "" || int.Parse(wejscie[j]) > m || int.Parse(wejscie[j]) < 0)
  202.              continue;
  203.            if (!int.TryParse(wejscie[j], out tmp)) return false;
  204.            C[m + i, tmp - 1] = 1;
  205.          }
  206.          C[m + i, n] = 1;
  207.        }
  208.        for (int i = 0; i < p; i++)
  209.        {
  210.          C[n, i + m] = 1;
  211.        }
  212.      }
  213.      return true;
  214.    }
  215.    
  216.    public static void WyswietlTablice(int[,] G)
  217.    {
  218.      for (int i = 0; i < G.GetLength(0); i++)
  219.      {
  220.        for (int j = 0; j < G.GetLength(1); j++)
  221.        {
  222.          Console.Write(G[i, j] + " ");
  223.        }
  224.        Console.WriteLine();
  225.      }
  226.      Console.WriteLine();
  227.    }
  228.    public static void WyswietlTablice(int[] G)
  229.    {
  230.      for (int i = 0; i < G.GetLength(0); i++)
  231.      {
  232.        Console.Write(G[i] + " ");
  233.      }
  234.      Console.WriteLine();
  235.      Console.WriteLine();
  236.    }
  237.  }