회원가입
로그인
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
글쓰기
질문 도움말
자주묻는 질문
지푸라기라도 잡는심정으로.. 문제좀 도와주세요.. ㅠ 다익스트라 문제입니다..
ysjkmk01
1년 전
0
import java.io.*; import java.util.*; // 통행료 계산 문제 public class Practice02 { static class Node implements Comparable<Node> { int num, cost, cityCount; Node(int num, int cost, int cityCount) { this.num = num; this.cost = cost; this.cityCount = cityCount; } @Override public int compareTo(Node o) { // 비용이 적은 순으로 먼저 if (this.cost != o.cost) return Integer.compare(this.cost, o.cost); // 비용이 같으면 도시 수가 적은 순으로 return Integer.compare(this.cityCount, o.cityCount); } } static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st; static int n, m, k; static int s, dest; static ArrayList<Node> al[]; // 다익스트라 알고리즘 static void djikstra(int start, int dest) { int[] dist = new int[n + 1]; // 최소 비용 int[] cityCount = new int[n + 1]; // 방문한 도시 수 Arrays.fill(dist, Integer.MAX_VALUE); Arrays.fill(cityCount, Integer.MAX_VALUE); PriorityQueue<Node> pq = new PriorityQueue<>(); pq.add(new Node(start, 0, 1)); // 시작 도시, 비용 0, 도시 수 1 dist[start] = 0; cityCount[start] = 1; while (!pq.isEmpty()) { Node now = pq.poll(); // 이미 더 적은 비용으로 처리된 경우 무시 if (dist[now.num] < now.cost) continue; // 현재 도시와 연결된 모든 도로 확인 for (Node next : al[now.num]) { int nextCost = now.cost + next.cost; int nextCityCount = now.cityCount + 1; // 더 적은 비용일 경우 if (dist[next.num] > nextCost) { dist[next.num] = nextCost; cityCount[next.num] = nextCityCount; pq.add(new Node(next.num, nextCost, nextCityCount)); } // 비용이 같고, 도시 수가 더 적을 경우 else if (dist[next.num] == nextCost && cityCount[next.num] > nextCityCount) { cityCount[next.num] = nextCityCount; pq.add(new Node(next.num, nextCost, nextCityCount)); } } } // 도달할 수 없는 경우 if (dist[dest] == Integer.MAX_VALUE) { System.out.println("-1"); } else { // 최종 목적지의 최소 비용과 최소 도시 수 출력 System.out.println(dist[dest] + " " + cityCount[dest]); } } public static void main(String[] args) throws IOException { // 입력 받기 st = new StringTokenizer(br.readLine()); n = Integer.parseInt(st.nextToken()); m = Integer.parseInt(st.nextToken()); k = Integer.parseInt(st.nextToken()); st = new StringTokenizer(br.readLine()); s = Integer.parseInt(st.nextToken()); // 시작 도시 dest = Integer.parseInt(st.nextToken()); // 목표 도시 al = new ArrayList[n + 1]; for (int i = 1; i <= n; i++) { al[i] = new ArrayList<>(); } // 도로 정보 입력 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()); // 기본 요금 int D = Integer.parseInt(st.nextToken()); // 1분당 추가 요금 int E = Integer.parseInt(st.nextToken()); // 소요 시간 // 도로 정보 저장 int totalCost = C + D * E; // 총 비용 = 기본 요금 + (시간 * 추가 요금) al[A].add(new Node(B, totalCost, 0)); // A -> B로 가는 도로 } // 기본 다익스트라 실행 djikstra(s, dest); // k번의 가격 인상 처리 for (int i = 0; i < k; i++) { int incr = Integer.parseInt(br.readLine()); // 각 도로의 비용을 증가시킴 for (int j = 1; j <= n; j++) { for (int p = 0; p < al[j].size(); p++) { Node road = al[j].get(p); road.cost += incr; // 추가 요금 증가 } } // 증가 후 다시 다익스트라 실행 djikstra(s, dest); } } } //이렇게 저렇게 해도 도저히 해결이 안되네요.. 제발 도움좀 부탁드립니다 ㅠ
tlsgusrb314
1년 전
0
총 비용부분에서 문제가 있는거 같은데요 C+(D*(max(E-10,0))) 이 되어야 하지 않을까요?
댓글을 작성하려면
로그인
해야 합니다.
ysjkmk01 1년 전