Advertisement
Guest User

Untitled

a guest
Apr 17th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.05 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. class Solution {
  5.  
  6.   /**
  7.    * Returns true iff team x can still win the Cup.
  8.    */
  9.   public static boolean solve(InputStream in) {
  10.     List<Node> nodes = new ArrayList<Node>();
  11.     List<Team> teams = new ArrayList<Team>();
  12.  
  13.     Scanner sc = new Scanner(in);
  14.    
  15.     int m = sc.nextInt();
  16.    
  17.     // parse all lines into graph
  18.     for (int i=0; i<m;i++) {
  19.       teams.add(new Team(sc.nextInt(),sc.nextInt(),sc.nextInt()));
  20.     }
  21.    
  22.     Node src = new Node(0);
  23.     Node sink = new Node(-1);
  24.     nodes.add(src);
  25.     nodes.add(sink);
  26.     sc.close();
  27.    
  28.     Team x = teams.get(0);
  29.     int xWins = x.w + x.g;
  30.    
  31.     // loop through teams and create nodes for matches
  32.     for(int i=1; i<m; i++) {
  33.       for(int j=1; j<i; j++) {
  34.         Node teamNode = nodes.get(j );
  35.         if (teamNode == null) {
  36.           teamNode = new Node(j);
  37.           nodes.add(teamNode);
  38.          
  39.  
  40.         }
  41.           teamNode.addEdge(sink, xWins - teams.get(j).w);
  42.           sink.addEdge(teamNode, 0);
  43.           if (xWins - teams.get(j).w < 0) {
  44.             return false;
  45.           }
  46.        
  47.         Node matchNode = new Node(i*j + j);
  48.         nodes.add(matchNode);
  49.         // add edge from the source to the match
  50.         matchNode.addEdge(src, 0);
  51.         src.addEdge(matchNode, 1);
  52.         // attach edges to the teamNodes of this match
  53.         matchNode.addEdge(nodes.get(j), Integer.MAX_VALUE);
  54.         matchNode.addEdge(nodes.get(i), Integer.MAX_VALUE);
  55.         nodes.get(i).addEdge(matchNode, 0);
  56.         nodes.get(j).addEdge(matchNode, 0);
  57.       }
  58.     }
  59.    
  60.    
  61.     Graph g = new Graph(nodes, src, sink);
  62.     g.maximizeFlow();
  63.    
  64.     for (Edge e : src.getEdges()) {
  65.       System.out.println(e.getCapacity());
  66.       System.out.println(e.getFlow());
  67.       if (e.getCapacity() > e.getFlow()) {
  68.         return false;
  69.       }
  70.     }
  71.    
  72.     return true;
  73.   }
  74. }
  75.  
  76.  
  77. class Team {
  78.   Team(int id, int w, int g) {
  79.     this.id = id;
  80.     this.w = w;
  81.     this.g = g;
  82.   }
  83.  
  84.   int id;
  85.   int w;
  86.   int g;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement