회원가입
로그인
Toggle navigation
문제
문제
전체 문제
문제 출처
단계별로 풀어보기
알고리즘 분류
추가된 문제
문제 순위
문제
푼 사람이 한 명인 문제
아무도 못 푼 문제
최근 제출된 문제
최근 풀린 문제
랜덤
출처
ICPC
Olympiad
한국정보올림피아드
한국정보올림피아드시․도지역본선
전국 대학생 프로그래밍 대회 동아리 연합
대학교 대회
카카오 코드 페스티벌
Coder's High
ICPC
Regionals
World Finals
Korea Regional
Africa and the Middle East Regionals
Europe Regionals
Latin America Regionals
North America Regionals
South Pacific Regionals
문제집
대회
채점 현황
랭킹
게시판
그룹
더 보기
재채점 기록
블로그
강의
실험실
도움말
BOJ Stack
BOJ Book
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
[JAVA] 왜 틀렸는지 모르겠습니다...
1865번 - 웜홀
wkdtnwp
3년 전
0
벨만포드로 풀었다고 생각이 드는데 틀린이유를 모르겠습니다.
import java.util.*; public class Q1865 { static int n,m,w; static boolean visited[]; static int dist[]; static ArrayList<Node>[] list; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int Tc = sc.nextInt(); while(Tc-- >0) { n = sc.nextInt(); m = sc.nextInt(); w = sc.nextInt(); visited = new boolean[n+1]; dist = new int[n+1]; list = new ArrayList[n+1]; Arrays.fill(dist,987654321); dist[1] = 0; for(int i =1; i < n+1;i++) { list[i] = new ArrayList<Node>(); } for(int i = 0 ;i < m; i++) { int start = sc.nextInt(); int end = sc.nextInt(); int weight = sc.nextInt(); list[start].add(new Node(end,weight)); list[end].add(new Node(start,weight)); } for(int i = 0 ;i < w; i++) { int start = sc.nextInt(); int end = sc.nextInt(); int weight = sc.nextInt(); list[start].add(new Node(end,(-1) * weight)); } belmanford(); } } public static void belmanford() { boolean update = false; a:for(int i = 1; i <n+1;i++) { update = false; for(int j = 0 ; j < list[i].size();j++) { Node node = list[i].get(j); if(dist[node.end] > dist[i] + node.weight) { dist[node.end] = dist[i] + node.weight; update = true; if( i == n ) { update = true; break a; } } } if(!update) { break; } } if(update) { System.out.println("YES"); }else { System.out.println("NO"); } } public static class Node implements Comparable<Node>{ int end; int weight; public Node(int end, int weight) { this.end = end; this.weight = weight; } public int compareTo(Node o) { return this.weight - o.weight; } } }
댓글을 작성하려면
로그인
해야 합니다.
wkdtnwp 3년 전
벨만포드로 풀었다고 생각이 드는데 틀린이유를 모르겠습니다.