회원가입
로그인
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
문제집
대회
채점 현황
게시판
그룹
더 보기
재채점 기록
블로그
도움말
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
82프로 메모리 초과
1865번 - 웜홀
skuksyzkzk
2년 전
0
자꾸 82프로에서 메모리 초과가 발생하는 데 이유 아시는 분 있으신가요 ?? ㅜㅜ
import java.util.*; import java.io.*; class Node { int dest, cost; public Node(int dest, int cost) { this.dest = dest; this.cost = cost; } } public class Main { static StringBuilder sb = new StringBuilder(); static int[] dist; static ArrayList<Node>[] adj; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st; int tc = Integer.parseInt(br.readLine()); for (int i = 0; i < tc; i++) { st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); int w = Integer.parseInt(st.nextToken()); adj = new ArrayList[n + 1]; for (int j = 0; j <= n; j++) { adj[j] = new ArrayList<>(); } // 양방향 도로 입력 for (int j = 0; j < m; j++) { st = new StringTokenizer(br.readLine()); int source = Integer.parseInt(st.nextToken()); int dest = Integer.parseInt(st.nextToken()); int cost = Integer.parseInt(st.nextToken()); adj[source].add(new Node(dest, cost)); adj[dest].add(new Node(source, cost)); } // 단방향 웜홀 입력 for (int j = 0; j < w; j++) { st = new StringTokenizer(br.readLine()); int source = Integer.parseInt(st.nextToken()); int dest = Integer.parseInt(st.nextToken()); int cost = Integer.parseInt(st.nextToken()); adj[source].add(new Node(dest, -cost)); // 웜홀은 음수 가중치로 추가 } boolean hasNegativeCycle = false; dist = new int[n + 1]; outer: for (int start = 1; start <=n;start ++){ Arrays.fill(dist, Integer.MAX_VALUE); dist[start] = 0; // 벨만-포드 알고리즘 적용 for (int j = 0; j < n - 1; j++) { for (int idx = 1; idx <= n; idx++) { for (Node node : adj[idx]) { if (dist[idx] != Integer.MAX_VALUE && dist[node.dest] > dist[idx] + node.cost) { dist[node.dest] = dist[idx] + node.cost; } } } } for (int idx = 1; idx <= n; idx++) { for (Node node : adj[idx]) { if (dist[idx] != Integer.MAX_VALUE && dist[node.dest] > dist[idx] + node.cost) { hasNegativeCycle = true; break outer; } } } } if (hasNegativeCycle) { sb.append("YES\n"); } else { sb.append("NO\n"); } } System.out.print(sb); } }
댓글을 작성하려면
로그인
해야 합니다.
skuksyzkzk 2년 전
자꾸 82프로에서 메모리 초과가 발생하는 데 이유 아시는 분 있으신가요 ?? ㅜㅜ