πŸ€– μ•Œκ³ λ¦¬μ¦˜

baekjoon. νŒŒν‹°

ttoance 2022. 10. 7. 22:32
λ°˜μ‘ν˜•

 

λ¬Έμ œμ§‘ : 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;  // μ€‘μš” 
            }
        }
    }
}

 

λ°˜μ‘ν˜•