팡세영
Log sey
팡세영
전체 방문자
오늘
어제
  • 분류 전체보기 (73)
    • PS (45)
      • programmers (13)
      • 백준 (29)
    • Android (16)
    • Daily (0)
    • Kotlin (6)
    • Design Pattern (1)
    • Java (1)
    • Flutter (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • Kotlin
  • LEVEL2
  • mvvm
  • 정렬
  • 완전탐색
  • 골드
  • compose
  • 구현
  • 프로그래머스
  • java
  • 자바
  • DFS
  • 안드로이드
  • 실버
  • Android
  • programmers #프로그래머스
  • 해쉬맵
  • ArcitecturePattern
  • programmers
  • 백준
  • flutter
  • binding
  • BFS
  • TestCode
  • CustomView
  • 하단네비게이션바
  • 문자열
  • 의존성 주입
  • 코틀린
  • 이분탐색

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
팡세영

Log sey

백준 - Packet Routing 6951 java
PS/백준

백준 - Packet Routing 6951 java

2023. 3. 9. 18:39


https://www.acmicpc.net/problem/6951

 

6951번: Packet Routing

The date is October 29th, 1969. Today, scientists at UCLA made history by exchanging data between two computers over a network. The transmission wasn't very spectacular: only the first two letters of the word login were received before the system crashed.

www.acmicpc.net


[문제 해설]

  • 해당 문제는 패킷 라우팅에 관한 설명입니다.
  • 패킷에서 패킷까지의 이동 거리가 주어지고 입력으로 들어오는 패킷의 이동 거리의 최소값을 출력하는 문제입니다.
  • 벨만포드, 플로이드-워셜 알고리즘을 통해 해당 문제를 해결 할 수 있을거 같습니다.

저는 플로이드-워셜 최단경로 알고리즘을 이용해 문제를 해결했습니다.

 

[Java]

import java.io.*;
import java.util.*;

public class Main {

    public static void main(String args[]) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringTokenizer st = new StringTokenizer(br.readLine());

        int n = Integer.parseInt(st.nextToken());
        int w = Integer.parseInt(st.nextToken());
        int p = Integer.parseInt(st.nextToken());

        int map[][] = new int[n+1][n+1];

        for(int i=0; i<=n; i++) {
            for(int j=0; j<=n; j++){
                if(i==j) map[i][j] = 0;
                else map[i][j] = 10000001;
            }
        }

        for(int i=0; i<w; i++) {
            st = new StringTokenizer(br.readLine());
            int a = Integer.parseInt(st.nextToken());
            int b = Integer.parseInt(st.nextToken());
            int cost = Integer.parseInt(st.nextToken());

            map[a][b] = Math.min(cost, map[a][b]);
            map[b][a]  = Math.min(cost, map[b][a]);
        }

        for(int k=1; k<=n; k++){
            for(int i=1; i<=n; i++){
                for(int j=1; j<=n; j++)
                    map[i][j] = Math.min(map[i][j], map[i][k] + map[k][j]);
            }
        }

        for(int i=1; i<=p; i++){
            st = new StringTokenizer(br.readLine());
            int start = Integer.parseInt(st.nextToken());
            int end = Integer.parseInt(st.nextToken());
            bw.write(map[start][end] + "\n");
        }

        bw.close();
    }

}

'PS > 백준' 카테고리의 다른 글

백준 - 친구비 16562  (1) 2023.03.08
백준 - 연속합 1912 java  (0) 2023.03.06
백준 - 문자열 집합 조합하기 25328  (0) 2023.03.03
백준 - 캠프 준비 16938  (0) 2023.03.01
[백준] - 도영이가 만든 맛있는 음식 2691  (0) 2023.02.28
    'PS/백준' 카테고리의 다른 글
    • 백준 - 친구비 16562
    • 백준 - 연속합 1912 java
    • 백준 - 문자열 집합 조합하기 25328
    • 백준 - 캠프 준비 16938
    팡세영
    팡세영
    Android, CS, PS

    티스토리툴바