Advertisement
Tetrikitty

PS3

Oct 8th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.90 KB | None | 0 0
  1. // Copy paste this Java Template and save it as "HospitalRenovation.java"
  2. import java.util.*;
  3. import java.io.*;
  4.  
  5. // write your matric number here: A0155700N
  6. // write your name here: Soh Zi Quan
  7. // write list of collaborators here:
  8. // year 2018 hash code: xrVYbth32e6GM6jXHLKb (do NOT delete this line) <- generate a new hash code
  9.  
  10. class HospitalRenovation {
  11.   private int V; // number of vertices in the graph (number of rooms in the hospital)
  12.   private int[][] AdjMatrix; // the graph (the hospital)
  13.   private int[] RatingScore; // the weight of each vertex (rating score of each room)
  14.  
  15.   int [] visited, p;
  16.  
  17.   public HospitalRenovation() {
  18.    
  19.   }
  20.  
  21.   int [] dfs(int start, int[][] mat){
  22.     visited = new int[V];
  23.     p = new int[V];
  24.     for (int i=0;i<V;i++){
  25.       visited[i] = 0;
  26.       p[i] = -1;
  27.     }
  28.     p[start] = 0;
  29.     dfsrec(start, mat);
  30.    
  31.     return p;
  32.   }
  33.  
  34.   void dfsrec(int u, int[][] mat){
  35.     int [] neighbours = new int[V];
  36.     for (int i=0;i<V;i++){
  37.       neighbours[i] = -1;
  38.     }
  39.     visited[u] = 1;
  40.     for (int i=0;i<V;i++){
  41.       if (mat[u][i] != 0){
  42.         neighbours[i] = i;
  43.       }
  44.     }
  45.    
  46.     for (int i=0;i<V;i++){
  47.       if (neighbours[i] != -1 && visited[neighbours[i]] == 0){
  48.         p[neighbours[i]] = u;
  49.         dfsrec(neighbours[i], mat);
  50.       }
  51.     }
  52.   }
  53.  
  54.   void printMat(int[][] arr){
  55.     for (int i=0;i<arr.length;i++){
  56.       String ln = "";
  57.       for (int j=0;j<arr[0].length;j++){
  58.         ln += arr[i][j] + " ";
  59.       }
  60.       System.out.println(ln);
  61.     }
  62.     System.out.println();
  63.   }
  64.  
  65.   int Query() {
  66.     int [] critical = new int[V];
  67.     int ans = -1;
  68.    
  69.     for (int i=0;i<V;i++){
  70.       critical[i] = -1;
  71.     }
  72.  
  73.    
  74.     for (int i=0;i<V;i++){
  75.       int [] dfs;
  76.       int start;
  77.       //Perform DFS on copies of the graph with the ith vertex missing.
  78.       int EditedAdjMatrix[] [] = new int[V][V];
  79.       for (int j=0;j<V;j++) {
  80.         for (int k=0;k<V;k++){
  81.           if (i == j || i == k)
  82.             EditedAdjMatrix[j][k] = 0;
  83.           else
  84.             EditedAdjMatrix[j][k] = AdjMatrix[j][k];
  85.         }
  86.       }
  87.       //printMat(EditedAdjMatrix);
  88.       if (i == 0)
  89.         start = 1;
  90.       else
  91.         start = 0;
  92.  
  93.       dfs = dfs(start, EditedAdjMatrix);
  94.       /*
  95.       String line = "";
  96.       for (int j=0;j<V;j++){
  97.         line += dfs[j]+" ";
  98.       }
  99.       System.out.println(line);
  100.       */
  101.      
  102.       for (int j=0;j<V;j++){
  103.         if (i != j && start != j && dfs[j] == -1){
  104.           critical[i] = i;
  105.           break;
  106.         }
  107.       }
  108.     }
  109.     /*
  110.     String l = "";
  111.     for (int i=0;i<V;i++){
  112.       l += critical[i]+" ";
  113.     }
  114.     System.out.println(l);
  115.     */
  116.     for (int i=0;i<V;i++){
  117.       if (critical[i] != -1 && (ans == -1 || RatingScore[critical[i]] < ans)){
  118.         ans = RatingScore[critical[i]];
  119.       }
  120.     }
  121.    
  122.     return ans;
  123.   }
  124.  
  125.   void run() throws Exception {
  126.     // for this PS3, you can alter this method as you see fit
  127.    
  128.     FileInputStream is = new FileInputStream(new File("C:/Users/Ziquan/Documents/School/CS2010/codecrunch/PS3/in3.txt"));
  129.     System.setIn(is);
  130.    
  131.     BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  132.     PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
  133.     int TC = Integer.parseInt(br.readLine()); // there will be several test cases
  134.     while (TC-- > 0) {
  135.       br.readLine(); // ignore dummy blank line
  136.       V = Integer.parseInt(br.readLine());
  137.      
  138.       StringTokenizer st = new StringTokenizer(br.readLine());
  139.       // read rating scores, A (index 0), B (index 1), C (index 2), ..., until the V-th index
  140.       RatingScore = new int[V];
  141.       for (int i = 0; i < V; i++)
  142.         RatingScore[i] = Integer.parseInt(st.nextToken());
  143.      
  144.       // clear the graph and read in a new graph as Adjacency Matrix
  145.       AdjMatrix = new int[V][V];
  146.       for (int i = 0; i < V; i++) {
  147.         st = new StringTokenizer(br.readLine());
  148.         int k = Integer.parseInt(st.nextToken());
  149.         while (k-- > 0) {
  150.           int j = Integer.parseInt(st.nextToken());
  151.           AdjMatrix[i][j] = 1; // edge weight is always 1 (the weight is on vertices now)
  152.         }
  153.       }
  154.      
  155.       pr.println(Query());
  156.     }
  157.     pr.close();
  158.   }
  159.  
  160.   public static void main(String[] args) throws Exception {
  161.     // do not alter this method
  162.     HospitalRenovation ps3 = new HospitalRenovation();
  163.     ps3.run();
  164.   }
  165. }
  166.  
  167.  
  168.  
  169. class IntegerPair implements Comparable < IntegerPair > {
  170.   Integer _first, _second;
  171.  
  172.   public IntegerPair(Integer f, Integer s) {
  173.     _first = f;
  174.     _second = s;
  175.   }
  176.  
  177.   public int compareTo(IntegerPair o) {
  178.     if (!this.first().equals(o.first()))
  179.       return this.first() - o.first();
  180.     else
  181.       return this.second() - o.second();
  182.   }
  183.  
  184.   Integer first() { return _first; }
  185.   Integer second() { return _second; }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement