(UVA) Risk - Solution

Link to the problem: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=508

The distance between every connected country is equal to 1. Given a starting country and a destination country, this problem wants us to find a path between them that uses the minimum number of countries. Once we will be given a lot of queries, one good solution is to use Floyd-Warshall's algorithm, that will give us all the minimum distance between every two countries.


import java.io.*;
import java.util.*;

class Main {
    public int reader(BufferedReader br) throws NumberFormatException, IOException {     
        int n;
        int resp = 0;     
      
        while (true) {         
            n = br.read();         
            if (n >= '0' && n <= '9') {
                break;
            }
        }
           
        while (true) {         
            resp = resp*10 + n-'0';         
            n = br.read();         
            if (n < '0' || n > '9') {
                break;     
            }
        }
      
        return resp;     
    }
   
    public void process() throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int numCase = 0;
        String line = br.readLine();
        while (line != null) {
            int[][] adjMatrix = new int[20][20];
            for (int i = 0; i < 20; i++) {
                for (int j = 0; j < 20; j++) {
                    adjMatrix[i][j] = Integer.MAX_VALUE/4;
                }
                adjMatrix[i][i] = 0;
            }
           
            String[] s = line.split(" ");
            int numConnections = Integer.parseInt(s[0]);
            for (int j = 0; j < numConnections; j++) {
                int numNode = Integer.parseInt(s[j+1])-1;
                adjMatrix[0][numNode] = 1;
                adjMatrix[numNode][0] = 1;
            }
           
            // construct edges
            for (int i = 1; i < 19; i++) {
                numConnections = reader(br);
                for (int j = 0; j < numConnections; j++) {
                    int numNode = reader(br)-1;
                    adjMatrix[i][numNode] = 1;
                    adjMatrix[numNode][i] = 1;
                }
            }
           
            // Floyd-Warshall
            for (int i = 0; i < 20; i++) {
                for (int j = 0; j < 20; j++) {
                    for (int k = 0; k < 20; k++) {
                        adjMatrix[j][k] = Math.min(adjMatrix[j][k], adjMatrix[j][i]+adjMatrix[i][k]);
                    }
                }
            }
           
            bw.write("Test Set #" + ++numCase + "\n");
            int numQueries = reader(br);
            for (int i = 0; i < numQueries; i++) {
                int start = reader(br)-1;
                int end = reader(br)-1;
               
                String answer = String.format("%2d to %2d: %d\n", (start+1), (end+1), adjMatrix[start][end]);
                bw.write(answer);
            }
            bw.write("\n");
           
            line = br.readLine();
        }
       
        bw.flush();
        bw.close();
                                              
        return;
    }
   
    public static void main(String[] args) throws NumberFormatException, IOException {
        Main m = new Main();
        m.process();
       
        System.exit(0);
    }
}

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Powers of Two - Solução

(Coderbyte) Counting Minutes I - Solução