Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cpecmu.cpe218.sp2019.hw6.submit;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.LinkedHashMap;
- import java.util.LinkedHashSet;
- import java.util.Map;
- import java.util.Scanner;
- import java.util.Set;
- import cpecmu.cpe218.sp2019.hw6.OfficeHours;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.List;
- import cpecmu.cpe218.sp2019.FordFulkerson;
- import cpecmu.cpe218.sp2019.NetworkFlow;
- import cpecmu.cpe218.sp2019.Pair;
- import cpecmu.cpe218.sp2019.WeightedGraph;
- import cpecmu.cpe218.sp2019.WeightedGraphAdjList;
- import java.util.Stack;
- public class OfficeHoursImpl<T, S> implements OfficeHours<T, S> {
- /** input file name */
- protected static final String inFile = "C:\\Users\\DELL\\Desktop\\algor\\261218-261-hw6-algor-la-master\\hw\\hw6tests/officehours01.in";
- @Override
- public Map<T, Set<S>> officeHours(Set<S> slots, Map<T, Set<S>> available,
- int a, int b, int c) {
- // TODO Your code here
- WeightedGraph<String, Integer> g = new WeightedGraphAdjList<>();
- List<String> free = new ArrayList<>();
- g.addVertex("s");
- for(Map.Entry m : available.entrySet()){
- g.addVertex(m.getKey().toString());
- g.addEdge("s", m.getKey().toString(),b);
- Set<String> set = (Set<String>) m.getValue();
- free.addAll(set);
- //System.out.println(free);
- }
- g.addVertex("pret");
- g.addVertex("t");
- for(S s : slots){
- g.addVertex(s.toString());
- g.addEdge(s.toString() , "pret", 1);
- }
- int count = 0;
- for(Map.Entry m : available.entrySet()){
- Set<S> set = (Set<S>) m.getValue();
- for(int i = 0 ; i < set.size() ; i++){
- g.addEdge((String) m.getKey(), free.get(count), 1);
- count++;
- }
- }
- int capt = 0;
- g.addEdge("pret", "t" , c);
- Map<T,Set<S>> result = new LinkedHashMap<>();
- NetworkFlow<String> nf = new FordFulkerson<>();
- Pair<Integer, WeightedGraph<String, Integer>> mf =
- nf.maximumFlow(g, "s", "t");
- System.out.println(mf.fst());
- for (String v : mf.snd().vertices()) {
- Set<S> sets = new HashSet<>();
- for (Pair<String, Integer> e : mf.snd().adjacentFrom(v)) {
- System.out.println(v + "→" + e.fst() + " = " + e.snd());
- if(e.snd() == 1 && available.containsKey(v)){
- sets.add((S) e.fst());
- result.put((T) v, sets);
- }else if(v=="pret" && e.fst() == "t"){
- capt = e.snd();
- }
- }
- }
- if(capt == c) return result;
- return null;
- }
- public static void main(String[] args) {
- try (FileReader fr = new FileReader(inFile);
- Scanner s = new Scanner(fr)) {
- // read slots
- int n = s.nextInt();
- Set<String> slots = new LinkedHashSet<>();
- for (int i = 0; i < n; i++)
- slots.add(s.next());
- // read TAs
- int m = s.nextInt();
- Map<String, Set<String>> available = new LinkedHashMap<>();
- for (int i = 0; i < m; i++) {
- String ta = s.next();
- // read holdable slots
- int k = s.nextInt();
- Set<String> holdable = new LinkedHashSet<>();
- for (int j = 0; j < k; j++) {
- String slot = s.next();
- if (!slots.contains(slot))
- throw new IllegalArgumentException("unknown slot: "
- + slot);
- holdable.add(slot);
- }
- available.put(ta, holdable);
- }
- // read schedule parameters
- int a = s.nextInt();
- int b = s.nextInt();
- int c = s.nextInt();
- // invoke algorithm
- OfficeHours<String, String> sbm = new OfficeHoursImpl<>();
- Map<String, Set<String>> res =
- sbm.officeHours(slots, available, a, b, c);
- System.out.println(res);
- }
- catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement