[백준] 1939번 중량제한 (C++)

2025. 9. 27. 22:56·Algorithm
728x90

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

 

 

이 문제는 N개의 노드가 입력되고 M개의 노드를 잇는 간선이 주어질때 각 간선마다 최대 중량 제한이 존재한다.

이때 시작점에서 끝점까지 이동할때 한번의 이동에서 옮길 수 있는 물품의 중량의 최댓값을 구하는 문제이다.

 

간선의 가중치가 있으며 시작점에서 끝점까지 최단거리에서 최대 용량을 구하기 위해서 다익스트라 알고리즘을 사용해야겠다고 생각을 했다.

 

또한 최대를 구하는 문제이므로 최대힙을 사용하여 문제를 해결하였다.

 

 

정답코드

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
#define endl "\n"
#define MAX 1e9

struct coordinate {
    int x;
    int y;
    int r;
};

int dx[] = {-1, 1, 0, 0, 1, -1, -1, 1};
int dy[] = {0, 0, -1, 1, -1, 1, -1, 1};
int N, M, start, en;
const ll INF = 1e11;
vector<vector<pii>> v(10001);

void dijkstra(int a, vector<ll> &dist) {
    priority_queue<pair<ll, ll>, vector<pair<ll, ll>>> pq;
    pq.push({INF, a});
    dist[a] = INF;

    while(!pq.empty()) {
        int Node = pq.top().second;
        ll cost = pq.top().first;
        pq.pop();

        if(dist[Node] > cost) continue;

        for(auto it : v[Node]) {
            int nextNode = it.first;
            int nextCost = (cost > it.second)? it.second : cost;

            if(dist[nextNode] < nextCost) {
                pq.push({nextCost, nextNode});
                dist[nextNode] = nextCost;
            }
        }
    }
}

void solve() {
    vector<ll> dist(N+1);
    dijkstra(start, dist);

    cout << dist[en];
}

void input() {
    cin >> N >> M;

    for(int i = 0; i < M; i++) {
        int a, b, c;
        cin >> a >> b >>c;
        v[a].push_back({b, c});
        v[b].push_back({a, c});
    }

    cin >> start >> en;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);

    input();
    solve();
}

 

 

728x90
저작자표시 (새창열림)

'Algorithm' 카테고리의 다른 글

[백준] 12033번 김인천씨의 식료품가게 (Small) (C++)  (0) 2025.09.29
[백준] 14607번 피자 (Large) (C++)  (0) 2025.09.28
[백준] 16234번 인구 이동 (C++)  (0) 2025.09.26
[백준] 15903번 카드 합체 놀이 (C++)  (0) 2025.09.25
[백준] 9421번 소수상근수 (C++)  (0) 2025.09.24
'Algorithm' 카테고리의 다른 글
  • [백준] 12033번 김인천씨의 식료품가게 (Small) (C++)
  • [백준] 14607번 피자 (Large) (C++)
  • [백준] 16234번 인구 이동 (C++)
  • [백준] 15903번 카드 합체 놀이 (C++)
쿨쿨.
쿨쿨.
  • 쿨쿨.
    All of the life
    쿨쿨.
  • 전체
    오늘
    어제
    • 분류 전체보기 (281) N
      • Programming (49) N
        • C, C++ (18)
        • Python (6)
        • Java (1)
        • HTML,CSS,JS (3)
        • SQL(DB) (13)
        • SpringBoot (5) N
        • Android (2)
        • CI,CD (1)
      • Algorithm (173)
        • Review (4)
      • Security (14)
        • WebHacking (3)
        • Websecurity (11)
      • OS (19)
        • Linux (12)
        • Mac os (2)
      • 머신러닝 (1)
      • CS(Computer Science) (12)
        • 컴퓨터 네트워크 (3)
        • 컴퓨터 구조 (1)
        • 인공지능 (8)
      • Docker (1)
      • Dev Book Review (3)
        • Clean Code (1)
        • Effective Java (0)
        • Real MySQL (2)
      • SWM (1)
      • Review (6)
      • AWS (2)
  • 블로그 메뉴

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

  • 공지사항

  • 인기 글

  • 태그

    백트래킹
    브루트포스
    누적합
    다익스트라
    에라토스테네스의 체
    백준
    트리
    Bandit
    우선순위큐
    우선순위 큐
    그래프 이론
    WebSecurity
    BFS
    비트마스킹
    구현
    다이나믹 프로그래밍
    깊이우선탐색
    이분탐색
    wargame
    linux
    DP
    c언어
    DFS
    재귀
    투포인터
    코딩
    그리디
    정렬
    시뮬레이션
    Leviathan
  • 최근 댓글

  • 최근 글

  • 250x250
  • hELLO· Designed By정상우.v4.10.6
쿨쿨.
[백준] 1939번 중량제한 (C++)
상단으로

티스토리툴바