Advertisement
h0tk3y

Untitled

Nov 26th, 2014
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.59 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8.  
  9. namespace AlgorithmsLab5_C_v2
  10. {
  11.     class Edge
  12.     {
  13.         public int From, To;
  14.         public long Capactity, Flow;
  15.  
  16.         public Edge(int from, int to, int capacity, int flow)
  17.         {
  18.             From = from;
  19.             To = to;
  20.             Capactity = capacity;
  21.             Flow = flow;
  22.         }
  23.     }
  24.  
  25.     class Program
  26.     {
  27.         private static List<Edge> _edges;
  28.         private static List<int>[] _graph;
  29.  
  30.         static void CreateEdge(int from, int to, int capacity)
  31.         {
  32.             Edge forward = new Edge(from, to, capacity, 0),
  33.                 backward = new Edge(to, from, 0, 0);
  34.             _graph[from].Add(_edges.Count);
  35.             _edges.Add(forward);
  36.             _graph[to].Add(_edges.Count);
  37.             _edges.Add(backward);
  38.         }
  39.  
  40.         private static int n;
  41.  
  42.         private static int[] _q;
  43.         private static int[] _d;
  44.         private static bool BFS()
  45.         {
  46.             int qh = 0, qt = 0;
  47.             _q[qt++] = 0;
  48.             for (int i = 0; i < _d.Length; i++)
  49.                 _d[i] = -1;
  50.             _d[0] = 0;
  51.             while (qh < qt && _d[n - 1] == -1)
  52.             {
  53.                 int v = _q[qh++];
  54.                 for (int i = 0; i < _graph[v].Count; i++)
  55.                 {
  56.                     int id = _graph[v][i],
  57.                         to = _edges[id].To;
  58.                     if (_d[to] == -1 && _edges[id].Flow < _edges[id].Capactity)
  59.                     {
  60.                         _q[qt++] = to;
  61.                         _d[to] = _d[v] + 1;
  62.                     }
  63.                 }
  64.             }
  65.             return _d[n - 1] != -1;
  66.         }
  67.  
  68.         private static int[] ptr;
  69.  
  70.         private static long DFS(int from, long flow)
  71.         {
  72.             if (flow == 0) return 0;
  73.             if (from == n - 1) return flow;
  74.             while (ptr[from] < _graph[from].Count)
  75.             {
  76.                 int id = _graph[from][ptr[from]],
  77.                     to = _edges[id].To;
  78.                 if (_d[to] == _d[from] + 1)
  79.                 {
  80.                     long pushed = DFS(to, Math.Min(flow, _edges[id].Capactity - _edges[id].Flow));
  81.                     if (pushed != 0)
  82.                     {
  83.                         _edges[id].Flow += pushed;
  84.                         _edges[id ^ 1].Flow -= pushed;
  85.                         return pushed;
  86.                     }
  87.                 }
  88.                 ptr[from]++;
  89.             }
  90.             return 0;
  91.         }
  92.  
  93.         static void Main(string[] args)
  94.         {
  95.             new Thread(Solve, 64000000).Start();
  96.         }
  97.  
  98.         private static void Solve()
  99.         {
  100.             StreamReader sr = new StreamReader("cut.in");
  101.             StreamWriter sw = new StreamWriter("cut.out");
  102.             string[] terms = sr.ReadLine().Split(' ');
  103.             n = int.Parse(terms[0]);
  104.             int m = int.Parse(terms[1]);
  105.             _edges = new List<Edge>();
  106.             _graph = new List<int>[n];
  107.             _q = new int[n];
  108.             _d = new int[n];
  109.             ptr = new int[n];
  110.             for (int i = 0; i < n; i++)
  111.                 _graph[i] = new List<int>();
  112.             for (int i = 0; i < m; i++)
  113.             {
  114.                 terms = sr.ReadLine().Split(' ');
  115.                 int from = int.Parse(terms[0]) - 1,
  116.                     to = int.Parse(terms[1]) - 1,
  117.                     capacity = int.Parse(terms[2]);
  118.                 CreateEdge(@from, to, capacity);
  119.                 CreateEdge(to, @from, capacity);
  120.             }
  121.             while (BFS())
  122.             {
  123.                 for (int i = 0; i < ptr.Length; i++)
  124.                     ptr[i] = 0;
  125.                 while (true)
  126.                     if (DFS(0, long.MaxValue) == 0) break;
  127.             }
  128.             bool[] visited = new bool[n];
  129.             DfsOnFull(0, _graph, _edges, visited);
  130.             sw.WriteLine(visited.Count(x => x));
  131.             for (int i = 0; i < n; i++)
  132.                 if (visited[i])
  133.                     sw.Write((i + 1) + " ");
  134.             sw.Close();
  135.         }
  136.  
  137.         static void DfsOnFull(int from, List<int>[] graph, List<Edge> edges, bool[] used)
  138.         {
  139.             if (!used[from])
  140.             {
  141.                 used[from] = true;
  142.                 foreach (var edge_id in graph[from])
  143.                     if (edges[edge_id].Flow < edges[edge_id].Capactity)
  144.                         DfsOnFull(edges[edge_id].To, graph, edges, used);
  145.             }
  146.         }
  147.  
  148.     }
  149.  
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement