๊ฐœ๋ฐœ/๐Ÿค– ์•Œ๊ณ ๋ฆฌ์ฆ˜

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;  // ์ค‘์š” 
            }
        }
    }
}

 

๋ฐ˜์‘ํ˜•