๐ ๋ฌธ์ ๋งํฌ
https://www.acmicpc.net/problem/1414
๐ฎ ๋ฌธ์ ํด๊ฒฐ ๋ฐฉ๋ฒ
๋ฌธ์ ์ ์ ๋ ฅ์ด ์ธ์ ํ๋ ฌ ๋ฐฉ์์ผ๋ก ๋ค์ด์ค๊ณ , ์ํ๋ฒณ๊ณผ 0์ผ๋ก ๋ค์ด์จ๋ค.
๋ฐ๋ผ์, ์ ๋ ฅ์ด ์ด์ด์ง ๋์ ์ ๊ธธ์ด์ด๊ธฐ ๋๋ฌธ์ ์ซ์๋ก ๋ณํํ๊ณ ๊ทธ๋ํ๋ฅผ ๋ง๋ค์ด์ค๋ค.
๋ชจ๋ ์ปดํจํฐ์ ๋ํด ๋์ ๊ทธ๋ํ๋ฅผ ๋ง๋ค์ด ์คฌ์ผ๋ฏ๋ก ๋ชจ๋ ์ปดํจํฐ๊ฐ ์ด์ด์ง ์ ์๊ฒ ๊ฐ์ฅ ์งง์ ๋์ ์ ๊ตฌํ๋ฉด ๋๋ค.
๋ฐ๋ผ์, ์ต์์ ์ฅํธ๋ฆฌ๋ฅผ ๊ตฌํ๋ ํ๋ฆผ ํน์ ํฌ๋ฃจ์ค์นผ ์๊ณ ๋ฆฌ์ฆ์ ์ด์ฉํด ๊ตฌํํ๋ฉด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ์ ์๋ค.
- ์ํ๋ฒณ์ ์ซ์๋ก ๋ณํํ๋ค.
- ์ปดํจํฐ์ ๋ํด ๋์ ์ ๋ณด๋ฅผ ์ฐ๊ฒฐํ๋ค(์ ์ ๊ณผ ๊ฐ์ ์ ์ด์ฉํด ๊ทธ๋ํ ์์ฑ)
- ํ๋ฆผ ์๊ณ ๋ฆฌ์ฆ์ ์ํํ๋ค. ์ด๋ค ์ปดํจํฐ์์ ์์ํ๋๋ผ๋ ๋ชจ๋ ์ด์ด์ ธ์ผํ๋ฏ๋ก ์๊ด์ด ์๋ค.
- ์ต์ ๊ธธ์ด ๋์ ์ ๊ตฌํ๊ณ ์ ์ฒด ๋์ ๊ธธ์ด์์ ๋นผ์ค๋ค.
๐ฉ Java ์ฝ๋
package com.algorithm.Baekjoon;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main_1414_๋ถ์ฐ์ด์๋๊ธฐ {
private static List<List<Computer>> computers;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
computers = new ArrayList<>();
for(int computer = 0; computer <= N; computer++) {
computers.add(new ArrayList<>());
}
// ๊ฐ์ ์ ๋ณด ๋ฃ๊ธฐ
int totalLength = 0;
for(int computerA = 1; computerA <= N; computerA++) {
String lengthInfo = br.readLine();
for(int computerB = 1; computerB <= N; computerB++) {
int length = convertAlpha(lengthInfo.charAt(computerB - 1));
computers.get(computerA).add(new Computer(computerB, length));
computers.get(computerB).add(new Computer(computerA, length));
totalLength += length;
}
}
// ๋์ ๊ตฌํ๊ธฐ
int rst = getMaxLanLength(N, totalLength);
System.out.println(rst);
br.close();
}
private static int convertAlpha(char alphabet) {
if(Character.isLowerCase(alphabet)) return alphabet - 'a' + 1;
else if(Character.isUpperCase(alphabet)) return alphabet - 'A' + 27;
return 0;
}
private static int getMaxLanLength(int n, int totalLength) {
/**
* 1. ๋ชจ๋ ์ปดํจํฐ๊ฐ ์ฐ๊ฒฐ๋์ด์ผ ํ๋ฏ๋ก 1๋ฒ ์ปดํจํฐ์ ๋ํด์ ์ต์์ ์ฅํธ๋ฆฌ ๊ตฌํ๊ธฐ
* 2. ๋ชจ๋ ๋ฐฉ๋ฌธ ์๋๋ ๊ฒฝ์ฐ -1
* 3. ๋ชจ๋ ๋ฐฉ๋ฌธ ์ฒ๋ฆฌ ๋ ๊ฒฝ์ฐ totalLength - ๊ธธ์ด๊ฐ
*/
int[] lan = new int[n + 1];
boolean[] visited = new boolean[n + 1];
PriorityQueue<Computer> pq = new PriorityQueue<>();
Arrays.fill(lan, Integer.MAX_VALUE);
pq.add(new Computer(1, 0));
lan[1] = 0;
// ํ๋ฆผ ์๊ณ ๋ฆฌ์ฆ
while(!pq.isEmpty()) {
Computer computerA = pq.poll();
if(visited[computerA.getComputer()]) continue;
visited[computerA.getComputer()] = true;
for (Computer computerB : computers.get(computerA.getComputer())) {
if(!visited[computerB.getComputer()] && computerB.getLength() != 0) {
// ์ต์ ๊ธธ์ด ๊ฐฑ์
lan[computerB.getComputer()] = Math.min(lan[computerB.getComputer()], computerB.getLength());
pq.add(computerB);
}
}
}
// ์ฌ์ฉํ ๋์ ๊ธธ์ด ๊ตฌํ๊ธฐ
int lanLength = 0;
for(int computer = 1; computer <= n; computer++) {
// ์ฐ๊ฒฐ ์๋ ์ปดํจํฐ ์๋ ๊ฒฝ์ฐ -1
if(!visited[computer]) return -1;
lanLength += lan[computer];
}
// ์ ์ฒด ๊ธธ์ด - ์ฌ์ฉ ๋์ ๊ธธ์ด = ๊ธฐ๋ถํ ๋์ ์ต๋ ๊ธธ์ด
return totalLength - lanLength;
}
static class Computer implements Comparable<Computer> {
private int computer;
private int length;
public Computer(int computer, int length) {
this.computer = computer;
this.length = length;
}
public int getComputer() {
return computer;
}
public int getLength() {
return length;
}
@Override
public int compareTo(Computer o) {
return Integer.compare(this.length, o.length);
}
}
}
๋ฐ์ํ
'Algorithm > ๊ทธ๋ํ(Graph)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค, Java] 2623๋ฒ : ์์ ํ๋ก๊ทธ๋จ (0) | 2022.08.09 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค, Java] ํฉ์น ํ์ ์๊ธ (0) | 2022.08.09 |
[๋ฐฑ์ค, Java] 18352๋ฒ : ํน์ ๊ฑฐ๋ฆฌ์ ๋์ ์ฐพ๊ธฐ (0) | 2022.05.13 |
[๋ฐฑ์ค, Java] 1707๋ฒ : ์ด๋ถ ๊ทธ๋ํ (0) | 2022.04.27 |