Facebook
From Edmonds_Karp, 3 Years ago, written in C#.
Embed
Download Paste or View Raw
Hits: 139
  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.     Console.WriteLine("Autorzy: ");
  49.     Console.WriteLine("Dominik Derewońko");
  50.     Console.WriteLine("Maciej Dominiak");
  51.     Console.WriteLine("Radosław Nagiel");
  52.     Console.Write("Kliknij dowolny przycisk aby zakończyć...");
  53.     Console.ReadKey();
  54.   }
  55.  
  56.   public static void AlgorytmEdmondsaKarpa(int m, int p, int n, int[,] C)
  57.   {
  58.     int[,] F = new int[n + 2, n + 2];
  59.     int fmax = 0;
  60.     Queue<int> Q = new Queue<int>();
  61.    
  62.     int[] P = new int[n + 2];
  63.     int[] CFP = new int[n + 2];
  64.     while (true)
  65.     {
  66.       for (int i = 0; i <= n + 1; i++) P[i] = -1;
  67.      
  68.       P[n] = -2;
  69.       CFP[n] = 9999999;
  70.      
  71.       Q.Clear();
  72.       Q.Enqueue(n);
  73.      
  74.       bool esc = false;
  75.      
  76.       while (Q.Count != 0)
  77.       {
  78.         int v = Q.Dequeue();
  79.        
  80.        
  81.         for (int u = 0; u <= n + 1; u++)
  82.         {
  83.           int cp = C[v, u] - F[v, u];
  84.           if ((cp != 0) && (P[u] == -1))
  85.           {
  86.             P[u] = v;
  87.             if (CFP[v] > cp) CFP[u] = cp; else CFP[u] = CFP[v];
  88.             if (u == n + 1)
  89.             {
  90.               fmax += CFP[n + 1];
  91.               int i = u;
  92.               while (i != n)
  93.               {
  94.                 v = P[i];
  95.                 F[v, i] += CFP[n + 1];
  96.                 F[i, v] -= CFP[n + 1];
  97.                 i = v;
  98.               }
  99.               esc = true; break;
  100.             }
  101.             Q.Enqueue(u);
  102.           }
  103.         }
  104.         if (esc) break;
  105.       }
  106.       if (!esc) break;
  107.     }
  108.    
  109.     Console.WriteLine();
  110.     Console.WriteLine("Wynik:");
  111.    
  112.     int licznik = 0;
  113.     if (fmax > 0)
  114.     {
  115.       for (int v = m; v < n; v++)
  116.       {
  117.         bool znaleziono = false;
  118.         for (int u = 0; u < n; u++)
  119.           if ((C[v, u] == 1) && (F[v, u] == 1))
  120.           {
  121.             int nrM = u + 1;
  122.             int nrP = v - m + 1;
  123.             Console.WriteLine("Pracownik nr {0} - maszyna {1}", nrP, nrM);
  124.             znaleziono = true;
  125.             licznik++;
  126.             break;
  127.           }
  128.         if (znaleziono == false)
  129.         {
  130.           int nrP = v - m + 1;
  131.          
  132.           Console.WriteLine("Pracownik nr {0} - brak maszyny", nrP);
  133.         }
  134.       }
  135.     }
  136.     Console.WriteLine("Liczba wszystkich maszyn: "+m);
  137.     Console.WriteLine("Liczba przypisanych maszyn: "+licznik);
  138.     Console.WriteLine("Liczba nieprzypisanych maszyn: "+(m-licznik));
  139.     Console.WriteLine();
  140.     Console.Write("Kliknij dowolny przycisk aby kontynuować...");
  141.     Console.ReadKey();
  142.     Console.WriteLine();
  143.   }
  144.  
  145.   public static bool WczytajDane(ref int m, ref int p, ref int n, ref int[,] C)
  146.   {
  147.     Console.Write("Podaj ilość maszyn: ");
  148.     if (!int.TryParse(Console.ReadLine(), out m)) return false;
  149.     Console.Write("Podaj ilość pracowników: ");
  150.     if (!int.TryParse(Console.ReadLine(), out p)) return false;
  151.     n = p + m;
  152.     C = new int[n + 2, n + 2];
  153.    
  154.     for (int i = 0; i < m; i++)
  155.     {
  156.       C[i, n + 1] = 1;
  157.     }
  158.     for (int i = 0; i < p; i++)
  159.     {
  160.       Console.WriteLine("Podaj numery maszyn, które może obsługiwać pracownik nr {0} (Oddzielone spacją, zatwierdź enterem)", i + 1);
  161.       string[] wejscie = Console.ReadLine().Split(' ');
  162.       int tmp;
  163.       for (int j = 0; j < wejscie.Length; j++)
  164.       {
  165.         if (!int.TryParse(wejscie[j], out tmp) || tmp > m || tmp <= 0)
  166.           continue;
  167.         C[m + i, tmp - 1] = 1;
  168.       }
  169.       C[m + i, n] = 1;
  170.     }
  171.     for (int i = 0; i < p; i++)
  172.     {
  173.       C[n, i + m] = 1;
  174.     }
  175.    
  176.     return true;
  177.   }
  178.  
  179.   public static bool WczytajPlik(ref int m, ref int p, ref int n, ref int[,] C)
  180.   {
  181.     string nazwa = "";
  182.     Console.Write("Wpisz nazwę pliku: ");
  183.     nazwa = Console.ReadLine();
  184.     StreamReader sr = null;
  185.    
  186.     try
  187.     {
  188.       sr = new StreamReader(nazwa + ".txt");
  189.     }
  190.     catch (Exception)
  191.     {
  192.       Console.WriteLine("Nie znaleziono pliku.");
  193.       return false;
  194.     }
  195.    
  196.    
  197.     if (sr != null)
  198.     {
  199.       if (!int.TryParse(sr.ReadLine(), out m)) return false;
  200.       if (!int.TryParse(sr.ReadLine(), out p)) return false;
  201.       n = p + m;
  202.       C = new int[n + 2, n + 2];
  203.      
  204.       for (int i = 0; i < m; i++)
  205.       {
  206.         C[i, n + 1] = 1;
  207.       }
  208.       for (int i = 0; i < p && !sr.EndOfStream; i++)
  209.       {
  210.         string[] wejscie = sr.ReadLine().Split(' ');
  211.         int tmp;
  212.         for (int j = 0; j < wejscie.Length; j++)
  213.         {
  214.           if (!int.TryParse(wejscie[j], out tmp) || tmp > m || tmp <= 0)
  215.             continue;
  216.           C[m + i, tmp - 1] = 1;
  217.         }
  218.         C[m + i, n] = 1;
  219.       }
  220.       for (int i = 0; i < p; i++)
  221.       {
  222.         C[n, i + m] = 1;
  223.       }
  224.     }
  225.     sr.Close();
  226.     return true;
  227.   }
  228.  
  229.   public static void WyswietlTablice(int[,] G)
  230.   {
  231.     for (int i = 0; i < G.GetLength(0); i++)
  232.     {
  233.       for (int j = 0; j < G.GetLength(1); j++)
  234.       {
  235.         Console.Write(G[i, j] + " ");
  236.       }
  237.       Console.WriteLine();
  238.     }
  239.     Console.WriteLine();
  240.   }
  241.   public static void WyswietlTablice(int[] G)
  242.   {
  243.     for (int i = 0; i < G.GetLength(0); i++)
  244.     {
  245.       Console.Write(G[i] + " ");
  246.     }
  247.     Console.WriteLine();
  248.     Console.WriteLine();
  249.   }
  250. }