11월이 시작됐다. 낮에 20도까지 올라가는 11월이다. 봄날씨가 느껴지는 11월의 첫 주를 되돌아본다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
new Main().run();
}
private void run() {
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
Input ip = readInput(br);
Solution s = new Solution();
System.out.println(s.solution(ip.numOfHouses, ip.roads));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private Input readInput(BufferedReader br) throws IOException {
String[] tokens = br.readLine().split(" ");
int numOfHouese = Integer.parseInt(tokens[0]);
int numOfRoads = Integer.parseInt(tokens[1]);
int[][] roads = new int[numOfRoads][3];
for (int i = 0; i < numOfRoads; i++) {
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int from = Integer.parseInt(st.nextToken());
int to = Integer.parseInt(st.nextToken());
int cost = Integer.parseInt(st.nextToken());
roads[i][0] = from;
roads[i][1] = to;
roads[i][2] = cost;
}
return new Input(numOfHouese, roads);
}
private static class Input {
private final int numOfHouses;
private final int[][] roads;
public Input(int numOfHouses, int[][] roads) {
this.numOfHouses = numOfHouses;
this.roads = roads;
}
}
}
class Solution {
public int solution(int numOfHouses, int[][] roads) {
int[] group = IntStream.range(0, numOfHouses + 1).toArray();
Arrays.sort(roads, Comparator.comparing(a -> a[2]));
int max = 0;
int answer = 0;
for (int[] road : roads) {
int from = road[0];
int to = road[1];
if(isGroup(group, from, to)) continue;
connect(group, from, to);
int cost = road[2];
answer += cost;
if(max < cost) max = cost;
}
return answer - max;
}
private boolean isGroup(int[] group, int target1, int target2) {
return getGroup(group, target1) == getGroup(group, target2);
}
private int getGroup(int[] group, int target) {
if(target != group[target]) group[target] = getGroup(group, group[target]);
return group[target];
}
private void connect(int[] group, int target1, int target2) {
int g1 = getGroup(group, target1);
int g2 = getGroup(group, target2);
if (g1 != g2) {
group[Math.max(g1, g2)] = Math.min(g1, g2);
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) {
new Main().run();
}
private void run() {
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
Solution s = new Solution();
System.out.println(s.solution(readInput(br), readQuestions(br)));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private int[][] readInput(BufferedReader br) throws IOException {
int len = Integer.parseInt(br.readLine());
int[][] result = new int[len + 1][2];
for (int i = 1; i < len + 1; i++) {
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int start = Integer.parseInt(st.nextToken());
int end = Integer.parseInt(st.nextToken());
result[i][0] = start;
result[i][1] = end;
}
return result;
}
private int[][] readQuestions(BufferedReader br) throws IOException {
int len = Integer.parseInt(br.readLine());
int[][] result = new int[len][2];
for (int i = 0; i < len; i++) {
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int start = Integer.parseInt(st.nextToken());
int end = Integer.parseInt(st.nextToken());
result[i][0] = start;
result[i][1] = end;
}
return result;
}
}
class Solution {
public String solution(int[][] people, int[][] questions) {
int len = people.length;
int[][] relations = getRelations(people);
for (int k = 1; k < len; k++) {
for (int i = 1; i < len; i++) {
for (int j = 1; j < len; j++) {
if (relations[i][j] > relations[i][k] + relations[k][j]) {
relations[i][j] = relations[i][k] + relations[k][j];
}
}
}
}
StringBuilder answer = new StringBuilder();
for (int[] question : questions) {
int from = question[0];
int to = question[1];
if(relations[from][to] == 0 || relations[from][to] == 999_999_999) answer.append(-1);
else answer.append(relations[from][to]);
answer.append("\n");
}
return answer.toString();
}
private int[][] getRelations(int[][] people) {
int len = people.length;
int[][] relations = new int[len][len];
for (int i = 1; i < len; i++) {
for (int j = 1; j < len; j++) {
if(i == j) continue;
if(isConnected(people[i], people[j])) relations[i][j] = 1;
}
}
for (int i = 1; i < len; i++) {
for (int j = 1; j < len; j++) {
if(i == j) continue;
if(relations[i][j] == 0) relations[i][j] = 999_999_999;
}
}
return relations;
}
private boolean isConnected(int[] target1, int[] target2) {
return Math.max(target1[0], target2[0]) <= Math.min(target1[1], target2[1]);
}
}