๋ฌธ์ ์ง : https://www.acmicpc.net/workbook/view/8708
ํ์ด : https://www.acmicpc.net/problem/1238
๋ฌธ์ ์ ์ ๋ง ๋์๋์๋ ๋ธ๋ก๊ทธ
https://loosie.tistory.com/209
1. ํ๋ก์ด๋ ์์ฌ๋ก ํ์ด
๊ฒฝ๋ก๋ณ๋ก ์ต๋จ ๊ฑฐ๋ฆฌ๋ฅผ ์ ์ฅํด์ ์ต์ข ์ ์ผ๋ก ๋น๊ตํ๋ ํ์ด์ด๋ค.
์ฌ๊ธฐ์ ์ค์ํ๋ ๊ฑด์ ์ด๊ธฐ์ ์ธํ ํ ๋ ๊ฐ์ ์ง์ ํ๋ ๊ฒ์ด์๋๋ฐ, Integer.MAX_VALUE๋ก ์ธํ ํ๋ฉด ๊ฐ์ ๋น๊ตํ๋ ๊ณผ์ ์์ ์ค๋ฒํ๋ก์ฐ๊ฐ ๋ฐ์ํ๊ณ
๋ฌธ์ ์ ๋ฐ๋ผ ๊ฑฐ๋ฆฌ ์ต๋๊ฐ์ธ 100์ผ๋ก ์ธํ ํ๋ฉด ๊ฒฝ๋ก๋ฅผ ํฉ์น๋ฉด์ 100์ ์ด๊ณผํ์ฌ ์๋ํ๋ ๊ฒฐ๊ณผ๊ฐ ๋์๊ฐ์ง ์์๋ค๋ ์ฌ์ค์ด์๋ค.
๊ฒฐ๊ตญ ์ ๋นํ ๊ฐ์ผ๋ก ์ธํ ํด์ ๋ฌธ์ ๋ฅผ ํ์๋ค.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.ArrayList;
import java.util.Arrays;
class Path {
int end;
int time;
Path(int end, int time) {
this.end = end;
this.time = time;
}
}
public class Main {
static ArrayList<ArrayList<Path>> pathList;
static int[][] pathTimeList;
static int townCount;
static ArrayList<Integer> timeList;
static boolean[] visited;
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
townCount = Integer.parseInt(st.nextToken());
int pathCount = Integer.parseInt(st.nextToken());
int dest = Integer.parseInt(st.nextToken());
// ArrayList ์ธํ
pathList = new ArrayList<>();
for (int i = 0; i <= townCount; i++) {
pathList.add(new ArrayList<>());
}
// ๊ฒฝ๋ก๋ณ ์๊ฐ ์ ์ฅ
pathTimeList = new int[townCount + 1][townCount + 1]; // townCount + 1๋ก ๋ณด์
for(int i = 1; i <= townCount; i++) {
for(int j = 1; j <= townCount; j++) {
if(i == j) {
pathTimeList[i][j] = 0;
}else {
pathTimeList[i][j] = 10000000; // ์ค์ (Integer.MAX_VALUE๋ก ์ธํ
ํ๋ฉด ์ค๋ฒ ํ๋ก์ฐ ๋ฐ์ )
}
}
}
// Path ์ธํ
for (int i = 0; i < pathCount; i ++) {
st = new StringTokenizer(br.readLine());
int start = Integer.parseInt(st.nextToken());
int end = Integer.parseInt(st.nextToken());
int time = Integer.parseInt(st.nextToken());
// System.out.printf("%d %d %d \n", start, end, time);
pathList.get(start).add(new Path(end, time));
pathTimeList[start][end] = time;
}
floyd();
// System.out.printf("%d %d \n", pathTimeList[1][3], pathTimeList[3][1]);
int max = Integer.MIN_VALUE;
for (int i = 1; i<= townCount; i++) {
if (max < (pathTimeList[i][dest] + pathTimeList[dest][i])) {
max = pathTimeList[i][dest] + pathTimeList[dest][i];
}
}
System.out.println(max);
}
public static void floyd() {
for (int k = 1; k <= townCount; k++) {
for (int i = 1; i <= townCount; i++) {
for (int j = 1; j <= townCount; j++) {
if ((pathTimeList[i][k] + pathTimeList[k][j]) < pathTimeList[i][j]) {
pathTimeList[i][j] = pathTimeList[i][k] + pathTimeList[k][j];
}
}
}
}
}
}
2. ๋ค์ต์คํธ๋ผ๋ก ํ์ด .. (์ดํด๋ชปํ.. ๋ค์๋ฒ์ ๋ค์ ํ์ด๋ณด๋ ๊ฑธ๋ก)
์๋๋ ๋ด๊ฐ ์๋ํ๋ ํ์ด๋ค..
1. 1์ฐจ ์๋ : ์ฌ๊ทํจ์ => 9% ์คํจ #20221007
์ฌ๊ทํจ์ ์ด์ฉํด์ ํ์ด ์์.
9%์์ ์ค๋ฅ ๋ฐ์ํด์ ๋ณด๋ visited ๋ณ์ ์ฒ๋ฆฌ๊ฐ ์๋ชป๋์ด ์์๋ค.
๋ด ๊ฒฝ์ฐ ์๋ ์ผ์ด์ค๋ฅผ ๋๋ ธ์๋ 486์ด ๋์๋ค. (๋ต์ 216์)
1->3์ผ๋ก ๋จ๋ฐฉํฅ์ผ๋ก ์ต์๊ฐ์ ๊ตฌํ์๋ 61์ด ๋์์ผ ํ๋ค.
6 20 3
3 2 45
6 1 66
6 2 31
2 4 94
5 3 46
5 2 79
3 1 64
4 3 74
3 5 59
1 6 93
3 6 45
6 4 40
3 4 67
1 3 61
1 2 42
4 2 50
4 1 55
2 6 93
5 4 95
1 4 54
2. 2์ฐจ ์๋ : ์ฌ๊ทํจ์ => ์๊ฐ ์ด๊ณผ #20221007
์ฌ๊ทํจ์๋ก ๋๋ฆฌ๋ค๋ณด๋ ํจ์จ์ ์ผ๋ก ๊ฒฝ๋ก๋ฅผ ์ฐพ๋๊ฒ ์๋๋ผ ์ ์ฒด ํ์์ ํ๋ ๊ฒ์ ๊ฐ๊น์์ ์๊ฐ ์ด๊ณผ๊ฐ ๋ฐ์ํ๋ค.
์ ์ด์ ์์์ ์๋ชปํด์ .. ๋ด์ผ ๋ค์ ๋ค์ต์คํธ๋ผ ์๊ณ ๋ฆฌ์ฆ ๊ณต๋ถํด์ ์๋ํด๋ณด๋ ๊ฒ์ผ๋ก
์๋๋ ๋ด๊ฐ ์๋ํ๋ ํ์ด.. (distance + possibleTime) < min์ผ๋ก ์๊ฐ์ด๊ณผ ํด๊ฒฐํด๋ณด๋ ค๊ณ ํ๋ ์๋๋ค ..
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.ArrayList;
import java.util.Arrays;
class Path {
int end;
int time;
Path(int end, int time) {
this.end = end;
this.time = time;
}
}
public class Main {
static ArrayList<ArrayList<Path>> pathList;
static int townCount;
static ArrayList<Integer> timeList;
static boolean[] visited;
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
townCount = Integer.parseInt(st.nextToken());
int pathCount = Integer.parseInt(st.nextToken());
int dest = Integer.parseInt(st.nextToken());
// ArrayList ์ธํ
pathList = new ArrayList<>();
for (int i = 0; i <= townCount; i++) {
pathList.add(new ArrayList<>());
}
// Path ์ธํ
for (int i = 0; i < pathCount; i ++) {
st = new StringTokenizer(br.readLine());
int start = Integer.parseInt(st.nextToken());
int end = Integer.parseInt(st.nextToken());
int time = Integer.parseInt(st.nextToken());
// System.out.printf("%d %d %d \n", start, end, time);
pathList.get(start).add(new Path(end, time));
}
//
int[] go = new int[townCount + 1];
for (int i = 1; i <= townCount; i++) {
// System.out.printf("for : %d %d %d \n", i, dest, 0);
timeList = new ArrayList<>();
visited = new boolean[townCount + 1]; // ๋ง์ ๋ฒํธ๊ฐ 1๋ถํฐ ์์ํด์ ๋ณด์
visited[i] = true;
party(i, dest, 0, visited);
// System.out.printf("%s \n", timeList.toString());
int min = Integer.MAX_VALUE;
for (int t = 0; t < timeList.size(); t++) {
min = Math.min(min, timeList.get(t));
}
// System.out.printf("%d %d \n", i, min);
go[i] = min;
}
int[] back = new int[townCount + 1];
for (int i = 1; i <= townCount; i++) {
// System.out.printf("for : %d %d %d \n", i, dest, 0);
timeList = new ArrayList<>();
visited = new boolean[townCount + 1]; // ๋ง์ ๋ฒํธ๊ฐ 1๋ถํฐ ์์ํด์ ๋ณด์
visited[dest] = true;
party(dest, i, 0, visited);
// System.out.printf("%s \n", timeList.toString());
int min = Integer.MAX_VALUE;
for (int t = 0; t < timeList.size(); t++) {
min = Math.min(min, timeList.get(t));
}
// System.out.printf("%d %d \n", i, min);
back[i] = min;
}
int time = go[1] +back[1];
for (int i = 1; i <= townCount; i ++) {
// System.out.printf("%d %d %d \n", go[i], back[i], go[i] + back[i]);
time = Math.max(time, go[i] + back[i]);
}
System.out.println(time);
}
public static void party(int start, int dest, int distance, boolean[] visited) {
if (start == dest) {
// System.out.printf("%b %b %b %b %s\n", visited[1], visited[2], visited[3], visited[4], debug);
timeList.add(distance);
return;
}
// ์ด๋ฏธ ๊ฒฐ๊ณผ๊ฐ ์์ผ๋ฉด
int min = Integer.MAX_VALUE;
for (int t = 0; t < timeList.size(); t++) {
min = Math.min(min, timeList.get(t));
}
ArrayList<Path> possibles = pathList.get(start);
for (int i = 0; i < possibles.size(); i++) {
// System.out.printf("from %s to %s %d \n", start, possibles.get(i).end, possibles.get(i).time);
int possibleEnd = possibles.get(i).end;
int possibleTime = possibles.get(i).time;
// ๋ฐฉ๋ฌธ ์ํ์ผ๋ฉด
if (! visited[possibleEnd] && (distance + possibleTime) < min) {
// if (! debug.contains(Integer.toString(possibleEnd))) {
// debug = debug + Integer.toString(possibleEnd);
visited[possibleEnd] = true;
party(possibleEnd, dest, distance + possibleTime, visited);
// visited[possibleEnd] = false; // ์ค์
}
}
}
}
'๐ค ์๊ณ ๋ฆฌ์ฆ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
baekjoon. ์คํ (10828) [python][Silver IV] (0) | 2023.01.16 |
---|---|
baekjoon. ํ์ ์ด๋ฐฅ (2531) | hashmap์ฌ์ฉ [java] (0) | 2022.10.17 |
baekjoon. ๋ ๊ฒ์ (0) | 2022.08.31 |
baekjoon. ๋ฌธ์์ด ํญ๋ฐ (0) | 2022.08.29 |
programmers. ๋ค๋จ๊ณ ์นซ์ ํ๋งค (0) | 2022.06.29 |