Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- class Solution {
- /**
- * Returns true iff team x can still win the Cup.
- */
- public static boolean solve(InputStream in) {
- List<Node> nodes = new ArrayList<Node>();
- List<Team> teams = new ArrayList<Team>();
- Scanner sc = new Scanner(in);
- int m = sc.nextInt();
- // parse all lines into graph
- for (int i=0; i<m;i++) {
- teams.add(new Team(sc.nextInt(),sc.nextInt(),sc.nextInt()));
- }
- Node src = new Node(0);
- Node sink = new Node(-1);
- nodes.add(src);
- nodes.add(sink);
- sc.close();
- Team x = teams.get(0);
- int xWins = x.w + x.g;
- // loop through teams and create nodes for matches
- for(int i=1; i<m; i++) {
- for(int j=1; j<i; j++) {
- Node teamNode = nodes.get(j );
- if (teamNode == null) {
- teamNode = new Node(j);
- nodes.add(teamNode);
- }
- teamNode.addEdge(sink, xWins - teams.get(j).w);
- sink.addEdge(teamNode, 0);
- if (xWins - teams.get(j).w < 0) {
- return false;
- }
- Node matchNode = new Node(i*j + j);
- nodes.add(matchNode);
- // add edge from the source to the match
- matchNode.addEdge(src, 0);
- src.addEdge(matchNode, 1);
- // attach edges to the teamNodes of this match
- matchNode.addEdge(nodes.get(j), Integer.MAX_VALUE);
- matchNode.addEdge(nodes.get(i), Integer.MAX_VALUE);
- nodes.get(i).addEdge(matchNode, 0);
- nodes.get(j).addEdge(matchNode, 0);
- }
- }
- Graph g = new Graph(nodes, src, sink);
- g.maximizeFlow();
- for (Edge e : src.getEdges()) {
- System.out.println(e.getCapacity());
- System.out.println(e.getFlow());
- if (e.getCapacity() > e.getFlow()) {
- return false;
- }
- }
- return true;
- }
- }
- class Team {
- Team(int id, int w, int g) {
- this.id = id;
- this.w = w;
- this.g = g;
- }
- int id;
- int w;
- int g;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement