Advertisement
Guest User

Mafia

a guest
Apr 24th, 2018
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.55 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. public class Program
  6. {
  7.     public static void Main()
  8.     {
  9.         int n = int.Parse(Console.ReadLine());
  10.         int[][] graph = DefaultGraph(n);
  11.  
  12.         string input;
  13.         int setCount = 1;
  14.         MaxFlow m;
  15.         while (!(input = Console.ReadLine()).Equals("end"))
  16.         {
  17.             int[] data = input.Split(new[] { ' ', '-' }, StringSplitOptions.RemoveEmptyEntries)
  18.                 .Select(int.Parse)
  19.                 .ToArray();
  20.             if (data.Length == 1)
  21.             {
  22.                 m = new MaxFlow(n);
  23.  
  24.                 Console.WriteLine($"Group {setCount++}: {m.fordFulkerson(graph, 0, n - 1)}");
  25.                 n = data[0];
  26.                 graph = DefaultGraph(n);
  27.                 continue;
  28.             }
  29.             int a = data[0] - 1, b = data[1] - 1, p = data[2];
  30.             graph[a][b] = p;
  31.             graph[b][a] = p;
  32.         }
  33.  
  34.         m = new MaxFlow(n);
  35.         Console.WriteLine($"Group {setCount++}: {m.fordFulkerson(graph, 0, n - 1)}");
  36.     }
  37.  
  38.     private static int[][] DefaultGraph(int n)
  39.     {
  40.         var graph = new int[n][];
  41.         for (int i = 0; i < n; i++)
  42.         {
  43.             graph[i] = new int[n];
  44.             for (int j = 0; j < n; j++)
  45.             {
  46.                 graph[i][j] = 0;
  47.             }
  48.         }
  49.         return graph;
  50.     }
  51. }
  52.  
  53. class MaxFlow
  54. {
  55.     private int V;//Number of vertices in graph
  56.     public MaxFlow(int v)
  57.     {
  58.         this.V = v;
  59.     }
  60.  
  61.     /* Returns true if there is a path from source 's' to sink
  62.       't' in residual graph. Also fills parent[] to store the
  63.       path */
  64.     bool bfs(int[][] rGraph, int s, int t, int[] parent)
  65.     {
  66.         // Create a visited array and mark all vertices as not
  67.         // visited
  68.         bool[] visited = new bool[V];
  69.         for (int i = 0; i < V; ++i)
  70.             visited[i] = false;
  71.  
  72.         // Create a queue, enqueue source vertex and mark
  73.         // source vertex as visited
  74.         Queue<int> queue = new Queue<int>(new[] { s });
  75.  
  76.         visited[s] = true;
  77.         parent[s] = -1;
  78.  
  79.         // Standard BFS Loop
  80.         while (queue.Count != 0)
  81.         {
  82.             int u = queue.Dequeue();
  83.  
  84.             for (int v = 0; v < V; v++)
  85.             {
  86.                 if (visited[v] == false && rGraph[u][v] > 0)
  87.                 {
  88.                     queue.Enqueue(v);
  89.                     parent[v] = u;
  90.                     visited[v] = true;
  91.                 }
  92.             }
  93.         }
  94.  
  95.         // If we reached sink in BFS starting from source, then
  96.         // return true, else false
  97.         return (visited[t] == true);
  98.     }
  99.  
  100.     // Returns tne maximum flow from s to t in the given graph
  101.     public int fordFulkerson(int[][] graph, int s, int t)
  102.     {
  103.         int u, v;
  104.  
  105.         // Create a residual graph and fill the residual graph
  106.         // with given capacities in the original graph as
  107.         // residual capacities in residual graph
  108.  
  109.         // Residual graph where rGraph[i][j] indicates
  110.         // residual capacity of edge from i to j (if there
  111.         // is an edge. If rGraph[i][j] is 0, then there is
  112.         // not)
  113.         int[][] rGraph = new int[V][];
  114.         for (int i = 0; i < V; i++)
  115.         {
  116.             rGraph[i] = new int[V];
  117.         }
  118.  
  119.         for (u = 0; u < V; u++)
  120.             for (v = 0; v < V; v++)
  121.                 rGraph[u][v] = graph[u][v];
  122.  
  123.         // This array is filled by BFS and to store path
  124.         int[] parent = new int[V];
  125.  
  126.         int max_flow = 0;  // There is no flow initially
  127.  
  128.         // Augment the flow while tere is path from source
  129.         // to sink
  130.         while (bfs(rGraph, s, t, parent))
  131.         {
  132.             // Find minimum residual capacity of the edhes
  133.             // along the path filled by BFS. Or we can say
  134.             // find the maximum flow through the path found.
  135.             int path_flow = int.MaxValue;
  136.             for (v = t; v != s; v = parent[v])
  137.             {
  138.                 u = parent[v];
  139.                 path_flow = Math.Min(path_flow, rGraph[u][v]);
  140.             }
  141.  
  142.             // update residual capacities of the edges and
  143.             // reverse edges along the path
  144.             for (v = t; v != s; v = parent[v])
  145.             {
  146.                 u = parent[v];
  147.                 rGraph[u][v] -= path_flow;
  148.                 rGraph[v][u] += path_flow;
  149.             }
  150.  
  151.             // Add path flow to overall flow
  152.             max_flow += path_flow;
  153.         }
  154.  
  155.         // Return the overall flow
  156.         return max_flow;
  157.     }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement