Advertisement
Guest User

ข้อ 2b

a guest
Apr 19th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.52 KB | None | 0 0
  1. package cpecmu.cpe218.sp2019.hw6.submit;
  2.  
  3. import java.io.FileNotFoundException;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. import java.util.LinkedHashMap;
  7. import java.util.LinkedHashSet;
  8. import java.util.Map;
  9. import java.util.Scanner;
  10. import java.util.Set;
  11.  
  12. import cpecmu.cpe218.sp2019.hw6.OfficeHours;
  13. import java.util.ArrayList;
  14. import java.util.Collection;
  15. import java.util.HashMap;
  16. import java.util.HashSet;
  17. import java.util.List;
  18.  
  19. import cpecmu.cpe218.sp2019.FordFulkerson;
  20. import cpecmu.cpe218.sp2019.NetworkFlow;
  21. import cpecmu.cpe218.sp2019.Pair;
  22. import cpecmu.cpe218.sp2019.WeightedGraph;
  23. import cpecmu.cpe218.sp2019.WeightedGraphAdjList;
  24. import java.util.Stack;
  25.  
  26.  
  27. public class OfficeHoursImpl<T, S> implements OfficeHours<T, S> {
  28.  
  29. /** input file name */
  30. protected static final String inFile = "C:\\Users\\DELL\\Desktop\\algor\\261218-261-hw6-algor-la-master\\hw\\hw6tests/officehours01.in";
  31.  
  32. @Override
  33. public Map<T, Set<S>> officeHours(Set<S> slots, Map<T, Set<S>> available,
  34. int a, int b, int c) {
  35. // TODO Your code here
  36. WeightedGraph<String, Integer> g = new WeightedGraphAdjList<>();
  37. List<String> free = new ArrayList<>();
  38. g.addVertex("s");
  39. for(Map.Entry m : available.entrySet()){
  40. g.addVertex(m.getKey().toString());
  41. g.addEdge("s", m.getKey().toString(),b);
  42. Set<String> set = (Set<String>) m.getValue();
  43. free.addAll(set);
  44. //System.out.println(free);
  45. }
  46. g.addVertex("pret");
  47. g.addVertex("t");
  48. for(S s : slots){
  49. g.addVertex(s.toString());
  50. g.addEdge(s.toString() , "pret", 1);
  51. }
  52. int count = 0;
  53. for(Map.Entry m : available.entrySet()){
  54. Set<S> set = (Set<S>) m.getValue();
  55. for(int i = 0 ; i < set.size() ; i++){
  56. g.addEdge((String) m.getKey(), free.get(count), 1);
  57. count++;
  58. }
  59. }
  60.  
  61. int capt = 0;
  62. g.addEdge("pret", "t" , c);
  63. Map<T,Set<S>> result = new LinkedHashMap<>();
  64. NetworkFlow<String> nf = new FordFulkerson<>();
  65. Pair<Integer, WeightedGraph<String, Integer>> mf =
  66. nf.maximumFlow(g, "s", "t");
  67. System.out.println(mf.fst());
  68. for (String v : mf.snd().vertices()) {
  69. Set<S> sets = new HashSet<>();
  70. for (Pair<String, Integer> e : mf.snd().adjacentFrom(v)) {
  71. System.out.println(v + "→" + e.fst() + " = " + e.snd());
  72. if(e.snd() == 1 && available.containsKey(v)){
  73. sets.add((S) e.fst());
  74. result.put((T) v, sets);
  75. }else if(v=="pret" && e.fst() == "t"){
  76. capt = e.snd();
  77. }
  78. }
  79. }
  80. if(capt == c) return result;
  81. return null;
  82. }
  83.  
  84. public static void main(String[] args) {
  85. try (FileReader fr = new FileReader(inFile);
  86. Scanner s = new Scanner(fr)) {
  87. // read slots
  88. int n = s.nextInt();
  89. Set<String> slots = new LinkedHashSet<>();
  90. for (int i = 0; i < n; i++)
  91. slots.add(s.next());
  92. // read TAs
  93. int m = s.nextInt();
  94. Map<String, Set<String>> available = new LinkedHashMap<>();
  95. for (int i = 0; i < m; i++) {
  96. String ta = s.next();
  97. // read holdable slots
  98. int k = s.nextInt();
  99. Set<String> holdable = new LinkedHashSet<>();
  100. for (int j = 0; j < k; j++) {
  101. String slot = s.next();
  102. if (!slots.contains(slot))
  103. throw new IllegalArgumentException("unknown slot: "
  104. + slot);
  105. holdable.add(slot);
  106. }
  107. available.put(ta, holdable);
  108. }
  109. // read schedule parameters
  110. int a = s.nextInt();
  111. int b = s.nextInt();
  112. int c = s.nextInt();
  113.  
  114. // invoke algorithm
  115. OfficeHours<String, String> sbm = new OfficeHoursImpl<>();
  116. Map<String, Set<String>> res =
  117. sbm.officeHours(slots, available, a, b, c);
  118. System.out.println(res);
  119. }
  120. catch (FileNotFoundException e) {
  121. e.printStackTrace();
  122. }
  123. catch (IOException e) {
  124. e.printStackTrace();
  125. }
  126. }
  127.  
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement