Advertisement
VladNitu

ProjectSelectionWL10_Vlad

Jan 24th, 2023
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. package weblab;
  2.  
  3. import java.util.*;
  4.  
  5. class Solution {
  6.  
  7. /**
  8. * @param n The number of members of the Camel Club
  9. * @param m The number of jobs for the club
  10. * @param mNames The names of the members, in indices 1 to n.
  11. * @param t The times that each member is available, in indices 1 to n.
  12. * @param ms The skillsets of each member, in indices in 1 to n.
  13. * @param jobNames The names/descriptions of the jobs, in indices 1 to m.
  14. * @param p The times that each job takes, in indices 1 to m.
  15. * @param js The skillsets required for each job, in indices 1 to m.
  16. * @return true iff the club can complete all jobs.
  17. */
  18. public static boolean solve(
  19. int n,
  20. int m,
  21. String[] mNames,
  22. int[] t,
  23. Set<String>[] ms,
  24. String[] jobNames,
  25. int[] p,
  26. Set<String>[] js) {
  27. List<Node> nodes = new ArrayList<>();
  28.  
  29. Node[] members = new Node[n + 1];
  30. Node[] jobs = new Node[m + 1];
  31. Node source = new Node("src");
  32. Node sink = new Node("dst");
  33. nodes.add(source);
  34. nodes.add(sink);
  35.  
  36. for (int i = 1; i <= n; ++i) {
  37. members[i] = new Node(mNames[i]);
  38. nodes.add(members[i]);
  39. source.addEdge(members[i], t[i]);
  40. }
  41.  
  42. for (int j = 1; j <= m; ++j) {
  43. jobs[j] = new Node(jobNames[j]);
  44. nodes.add(jobs[j]);
  45. jobs[j].addEdge(sink, p[j]);
  46. }
  47.  
  48.  
  49. for (int i = 1; i <= n; ++i)
  50. for (int j = 1; j <= m; ++j)
  51. if (ms[i].containsAll(js[j])) // If member 'i' can fulfill all the requirements of job 'j'
  52. members[i].addEdge(jobs[j], Integer.MAX_VALUE / 2);
  53.  
  54.  
  55. Graph G = new Graph(nodes, source, sink);
  56. G.maximizeFlow();
  57.  
  58. int jobTimes = 0;
  59. for (int j = 1; j <= m; ++j)
  60. jobTimes += p[j];
  61.  
  62. for (Edge e: sink.getEdges()) {
  63. Edge term = e.getBackwards();
  64. jobTimes -= term.getFlow();
  65. }
  66.  
  67. return jobTimes == 0; // If all jobs were fulfilled -> all edges (node, t) are saturated by flow
  68.  
  69. }
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement