17396. 백도어 (Java)

2023. 11. 3. 23:01·Algorithm & Data Structures/BOJ

별것없는 다익스트라 문제지만

다익스트라 알고리즘을 해결법을 보지않고 혼자 스스로 기억을 짜내어 구현하다 보니

isVisited를 빠트려 시간초과가여러번 났던 문제..

package BOJ;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
class Node implements Comparable<Node>{
	int end;
	long val;
	
	public Node(int end, long val) {
		this.end = end;
		this.val = val;
	}

	@Override
	public int compareTo(Node o) {
		return this.val>o.val?1:-1;
	}
	
}
public class Main {
	static int N,M;
	static long INF=Long.MAX_VALUE;
	static boolean[] check ;
	static ArrayList<Node>[] list;
	static long[] dist;
	
	public static long Dijkstra(int start, int arrive) {
		PriorityQueue<Node> pq = new PriorityQueue<Node>();
		pq.add(new Node(start,0));
		boolean[] isVisited = new boolean[N];
		dist[start] = 0;
		while(!pq.isEmpty()) {
			Node now = pq.poll();
			if(isVisited[now.end]) continue;
			isVisited[now.end]=true;
			for(Node next : list[now.end]) {
			
				if(next.end!=arrive && check[next.end]) continue;
				
				if(dist[next.end] > dist[now.end]+next.val)
				{
					dist[next.end] = dist[now.end]+next.val;
					pq.offer(new Node(next.end,dist[next.end]));
				}
			}
		}
		if(dist[arrive]==INF)
			return -1;
		return dist[arrive]; 
	}
	public static void main(String[] args) throws IOException {

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		
		N = Integer.parseInt(st.nextToken());
		M = Integer.parseInt(st.nextToken());
		
		check = new boolean[N];
		list = new ArrayList[N];
		dist = new long[N];
		
		for(int i = 0 ; i < N;i++) {
			list[i] = new ArrayList<>();
			dist[i] = INF;
		}
		st = new StringTokenizer(br.readLine());
		
		for(int i =0  ; i <N ; i ++) {
			if(st.nextToken().equals("1")) {
				check[i] = true;
			}
		}
		for(int i =0 ; i<M ;i++) {
			
			st = new StringTokenizer(br.readLine());
			int a = Integer.parseInt(st.nextToken());
			int b = Integer.parseInt(st.nextToken());
			int c = Integer.parseInt(st.nextToken());
			
			list[a].add(new Node(b,c));
			list[b].add(new Node(a,c));
			
		}
		
		System.out.println(Dijkstra(0,N-1));
	}
}

 

'Algorithm & Data Structures > BOJ' 카테고리의 다른 글

2665. 미로만들기 (Java)  (0) 2023.11.08
11779. 최소비용 구하기2 (Java)  (0) 2023.11.07
14284. 간선이어가기2 (Java)  (1) 2023.11.01
1504. 특정한 최단경로 (Java)  (0) 2023.10.31
2294. 동전2 (Java)  (0) 2023.10.30
'Algorithm & Data Structures/BOJ' 카테고리의 다른 글
  • 2665. 미로만들기 (Java)
  • 11779. 최소비용 구하기2 (Java)
  • 14284. 간선이어가기2 (Java)
  • 1504. 특정한 최단경로 (Java)
Geisha
Geisha
개발 일기
  • Geisha
    Geisha
    Geisha
  • 전체
    오늘
    어제
    • 분류 전체보기 (307) N
      • Algorithm & Data Structures (233) N
        • BOJ (91) N
        • SWEA (1)
        • Programers (137) N
        • Data Structures (3)
      • DB (21)
        • SQL (15)
        • RDBMS (2)
      • Java (1)
        • Class (1)
      • Spring (5)
        • Spring MVC (1)
        • Annotations (1)
      • CS (36)
        • 운영체제 (13)
        • 네트워크 (5)
      • Tool (6)
        • Git (5)
        • AWS (1)
      • Project (1)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    유니온파인드
    baekjoon
    골드
    Dijkstra
    알고리즘
    후위순회
    dp
    투포인터
    프로그래머스
    unionfind
    스택
    Stack
    algorithm
    PriorityQueue
    백트래킹
    Union-Find
    Java
    이분탐색
    SQL
    programmers
    전위순회
    구현
    binarySearch
    다익스트라
    BFS
    동적계획법
    DynamicProgramming
    경로압축
    dfs
    백준
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.0
Geisha
17396. 백도어 (Java)
상단으로

티스토리툴바