Advertisement
Guest User

1112 Mouse Race

a guest
Feb 10th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.45 KB | None | 0 0
  1. /*
  2.  * 1112 Mice and Maze
  3.  *
  4.  * Author: John Bowers
  5.  *   Date: ##/##/2015
  6.  */
  7.  
  8. import java.util.*;
  9.  
  10. class Main {
  11.  
  12.     class Edge {
  13.       Node dest;
  14.       int time;
  15.       public Edge(Node dest, int time) { this.dest = dest; this.time = time; }
  16.     }
  17.  
  18.     public int nodes_reached = 0;
  19.  
  20.     class Node {
  21.       ArrayList<Edge> adj = new ArrayList<Edge>();
  22.       boolean visited = false;
  23.       int node_id;
  24.       public Node(int node_id) { this.node_id = node_id; }
  25.     }
  26.  
  27.     class NodePair {
  28.       Node node; int time;
  29.       public NodePair(Node n, int t) { this.node = n; this.time = t; }
  30.       public String toString() { return "(Node: " + (node.node_id+1) + ", " + time + ")"; }
  31.     }
  32.  
  33.     public Main(Scanner in) {
  34.       int N = in.nextInt(); in.nextLine();
  35.       int E = in.nextInt() - 1; in.nextLine();
  36.       int T = in.nextInt(); in.nextLine();
  37.       int M = in.nextInt(); in.nextLine();
  38.  
  39.       // Create the nodes:
  40.       Node[] nodes = new Node[N];
  41.       for (int i = 0; i < N; i++) {
  42.         nodes[i] = new Node(i);
  43.       }
  44.  
  45.       // Read in the edges (flipping them)
  46.       for (int i = 0; i < M; i++) {
  47.         int a = in.nextInt() - 1;
  48.         int b = in.nextInt() - 1;
  49.         int t = in.nextInt(); in.nextLine();
  50.  
  51.         nodes[b].adj.add(new Edge(nodes[a], t));
  52.       }
  53.  
  54.       PriorityQueue<NodePair> pq = new PriorityQueue<NodePair>(N,
  55.         new Comparator<NodePair>() {
  56.           public int compare(NodePair p, NodePair q) {
  57.             return p.time - q.time;
  58.           }
  59.         });
  60.  
  61.       NodePair np = new NodePair(nodes[E], 0);
  62.       pq.add(np);
  63.       System.err.println("PUSH: " + np);
  64.  
  65.       while (!pq.isEmpty()) {
  66.         NodePair p = pq.remove();
  67.         System.err.println("POP: " + p);
  68.         if (!p.node.visited) {
  69.           nodes_reached ++;
  70.           p.node.visited = true;
  71.           for (Edge e : p.node.adj) {
  72.             if (!e.dest.visited && p.time + e.time <= T) {
  73.               np = new NodePair(e.dest, p.time + e.time);
  74.               System.err.println("PUSH: " + np);
  75.               pq.add(np);
  76.             }
  77.           }
  78.         }
  79.       }
  80.       System.out.println(nodes_reached);
  81.     }
  82.  
  83.     public static void main(String[] args) {
  84.       Scanner in = new Scanner(System.in);
  85.  
  86.       int cases = in.nextInt();
  87.       in.nextLine();
  88.       in.nextLine();
  89.  
  90.       for (int i = 0; i < cases; i++) {
  91.         new Main(in);
  92.         if (i < cases - 1) System.out.println();
  93.       }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement