Facebook
From Edmonds_Karp, 3 Years ago, written in C#.
Embed
Download Paste or View Raw
Hits: 116
  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.     int licznik = 0;
  106.     if (fmax > 0)
  107.     {
  108.       for (int v = m; v < n; v++)
  109.       {
  110.         bool znaleziono = false;
  111.         for (int u = 0; u < n; u++)
  112.           if ((C[v, u] == 1) && (F[v, u] == 1))
  113.           {
  114.             int nrM = u + 1;
  115.             int nrP = v - m + 1;
  116.             Console.WriteLine("Pracownik nr {0} - maszyna {1}", nrP, nrM);
  117.             znaleziono = true;
  118.             licznik++;
  119.             break;
  120.           }
  121.         if (znaleziono == false)
  122.         {
  123.           int nrP = v - m + 1;
  124.          
  125.           Console.WriteLine("Pracownik nr {0} - brak maszyny", nrP);
  126.         }
  127.       }
  128.     }
  129.     Console.WriteLine("Liczba wszystkich maszyn: "+m);
  130.     Console.WriteLine("Liczba przypisanych maszyn: "+licznik);
  131.     Console.WriteLine("Liczba nieprzypisanych maszyn: "+(m-licznik));
  132.     Console.WriteLine();
  133.     Console.Write("Kliknij dowolny przycisk aby kontynuować...");
  134.     Console.ReadKey();
  135.     Console.WriteLine();
  136.   }
  137.  
  138.   public static bool WczytajDane(ref int m, ref int p, ref int n, ref int[,] C)
  139.   {
  140.     Console.Write("Podaj ilość maszyn: ");
  141.     if (!int.TryParse(Console.ReadLine(), out m)) return false;
  142.     Console.Write("Podaj ilość pracowników: ");
  143.     if (!int.TryParse(Console.ReadLine(), out p)) return false;
  144.     n = p + m;
  145.     C = new int[n + 2, n + 2];
  146.    
  147.     for (int i = 0; i < m; i++)
  148.     {
  149.       C[i, n + 1] = 1;
  150.     }
  151.     for (int i = 0; i < p; i++)
  152.     {
  153.       Console.WriteLine("Podaj numery maszyn, które może obsługiwać pracownik nr {0} (Oddzielone spacją, zatwierdź enterem)", i + 1);
  154.       string[] wejscie = Console.ReadLine().Split(' ');
  155.       int tmp;
  156.       for (int j = 0; j < wejscie.Length; j++)
  157.       {
  158.         if (!int.TryParse(wejscie[j], out tmp) || tmp > m || tmp <= 0)
  159.           continue;
  160.         C[m + i, tmp - 1] = 1;
  161.       }
  162.       C[m + i, n] = 1;
  163.     }
  164.     for (int i = 0; i < p; i++)
  165.     {
  166.       C[n, i + m] = 1;
  167.     }
  168.    
  169.     return true;
  170.   }
  171.  
  172.   public static bool WczytajPlik(ref int m, ref int p, ref int n, ref int[,] C)
  173.   {
  174.     string nazwa = "";
  175.     Console.Write("Wpisz nazwę pliku: ");
  176.     nazwa = Console.ReadLine();
  177.     StreamReader sr = null;
  178.    
  179.     try
  180.     {
  181.       sr = new StreamReader(nazwa + ".txt");
  182.     }
  183.     catch (Exception)
  184.     {
  185.       Console.WriteLine("Nie znaleziono pliku.");
  186.       return false;
  187.     }
  188.    
  189.    
  190.     if (sr != null)
  191.     {
  192.       if (!int.TryParse(sr.ReadLine(), out m)) return false;
  193.       if (!int.TryParse(sr.ReadLine(), out p)) return false;
  194.       n = p + m;
  195.       C = new int[n + 2, n + 2];
  196.      
  197.       for (int i = 0; i < m; i++)
  198.       {
  199.         C[i, n + 1] = 1;
  200.       }
  201.       for (int i = 0; i < p && !sr.EndOfStream; i++)
  202.       {
  203.         string[] wejscie = sr.ReadLine().Split(' ');
  204.         int tmp;
  205.         for (int j = 0; j < wejscie.Length; j++)
  206.         {
  207.           if (!int.TryParse(wejscie[j], out tmp) || tmp > m || tmp <= 0)
  208.             continue;
  209.           C[m + i, tmp - 1] = 1;
  210.         }
  211.         C[m + i, n] = 1;
  212.       }
  213.       for (int i = 0; i < p; i++)
  214.       {
  215.         C[n, i + m] = 1;
  216.       }
  217.     }
  218.     sr.Close();
  219.     return true;
  220.   }
  221.  
  222.   public static void WyswietlTablice(int[,] G)
  223.   {
  224.     for (int i = 0; i < G.GetLength(0); i++)
  225.     {
  226.       for (int j = 0; j < G.GetLength(1); j++)
  227.       {
  228.         Console.Write(G[i, j] + " ");
  229.       }
  230.       Console.WriteLine();
  231.     }
  232.     Console.WriteLine();
  233.   }
  234.   public static void WyswietlTablice(int[] G)
  235.   {
  236.     for (int i = 0; i < G.GetLength(0); i++)
  237.     {
  238.       Console.Write(G[i] + " ");
  239.     }
  240.     Console.WriteLine();
  241.     Console.WriteLine();
  242.   }
  243. }