public static int[] calculate(int[][] g, int n) {
        int[] dist = new int[n];
        boolean[] visited = new boolean[n];
        Arrays.fill(visited, false);
        Arrays.fill(dist, -1);
        dist[0] = 0;
        for (int i = 0; i < n; ++i) {
            int v = 0;
            int minValue = -1;
            for (int j = 0; j < n; ++j) {
                if (visited[j] | dist[j] == -1) {
                    continue;
                }
                if (minValue == -1 || dist[j] < minValue) {
                    minValue = dist[j];
                    v = j;
                }
            }
            visited[v] = true;
            for (int j = 0; j < n; ++j) {
                if (g[v][j] == -1) {
                    continue;
                }
                if (dist[j] == -1 || dist[v] + g[v][j] < dist[j]) {
                    dist[j] = dist[v] + g[v][j];
                }
            }
        }
        return dist;
    }