Advertisement
hristocr

MaxTaskAssignment

Sep 9th, 2019
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace P01MaxTaskAssignmentDfs
  5. {
  6. internal class Program
  7. {
  8. // rows are tasks, cols are people
  9. private static int[][] graph;
  10.  
  11. private static char[] peopleNames;
  12.  
  13. private static bool[] visited;
  14.  
  15. private static int[] assignedTasks;
  16.  
  17. public static void Main(string[] args)
  18. {
  19. ReadInput();
  20.  
  21. // for (int person = 0; person < graph[0].Length; person++)
  22. for (int person = graph[0].Length - 1; person >= 0; person--)
  23. {
  24. visited = new bool[graph.Length];
  25. FindPath(person);
  26. }
  27.  
  28. var result = new List<string>();
  29. for (int i = 0; i < assignedTasks.Length; i++)
  30. {
  31. var personToRmove = assignedTasks[i];
  32. if (personToRmove != -1)
  33. {
  34. var personName = peopleNames[personToRmove];
  35. result.Add($"{personName}-{i + 1}");
  36. }
  37. }
  38.  
  39. result.Sort();
  40. Console.WriteLine(string.Join(Environment.NewLine, result));
  41. }
  42.  
  43. private static bool FindPath(int person)
  44. {
  45. for (int task = 0; task < graph.Length; task++)
  46. // for (int task = graph.Length - 1; task >= 0; task--)
  47. {
  48. if (graph[task][person] > 0 && !visited[task])
  49. {
  50. visited[task] = true;
  51. // if task is not assigned or the assigned person can take another task
  52. if (assignedTasks[task] < 0)
  53. {
  54. assignedTasks[task] = person;
  55.  
  56. return true;
  57. }
  58.  
  59. var hasPath = FindPath(assignedTasks[task]);
  60. if (hasPath)
  61. {
  62. assignedTasks[task] = person;
  63.  
  64. return true;
  65. }
  66.  
  67. Console.WriteLine();
  68. }
  69. }
  70.  
  71. return false;
  72. }
  73.  
  74.  
  75. private static void ReadInput()
  76. {
  77. var peopleCount = int.Parse(Console.ReadLine().Split(' ')[1]);
  78. var tasksCount = int.Parse(Console.ReadLine().Split(' ')[1]);
  79.  
  80. graph = new int[tasksCount][];
  81. for (int i = 0; i < graph.Length; i++)
  82. {
  83. graph[i] = new int[peopleCount];
  84. }
  85.  
  86. peopleNames = new char[peopleCount];
  87.  
  88. visited = new bool[tasksCount];
  89.  
  90. assignedTasks = new int[tasksCount];
  91.  
  92. for (int i = 0; i < tasksCount; i++)
  93. {
  94. assignedTasks[i] = -1;
  95. }
  96.  
  97. var startChar = 'A';
  98.  
  99. for (int personNodeId = 0; personNodeId < peopleCount; personNodeId++)
  100. {
  101. peopleNames[personNodeId] = startChar;
  102. startChar++;
  103. }
  104.  
  105. for (int person = 0; person < peopleCount; person++)
  106. {
  107. var line = Console.ReadLine().ToCharArray();
  108. for (int task = 0; task < tasksCount; task++)
  109. {
  110. var capacity = line[task] == 'Y' ? 1 : 0;
  111.  
  112. if (capacity == 1)
  113. {
  114. graph[task][person] = 1;
  115. }
  116. }
  117. }
  118. }
  119. }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement