baekjoon. νν°
λ¬Έμ μ§ : 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; // μ€μ
}
}
}
}