반응형
https://www.acmicpc.net/problem/1800
1800번: 인터넷 설치
첫 번째 줄에 N(1 ≤ N ≤ 1,000), 케이블선의 개수 P(1 ≤ P ≤ 10,000), 공짜로 제공하는 케이블선의 개수 K(0 ≤ K < N)이 주어진다. 다음 P개의 줄에는 케이블이 연결하는 두 컴퓨터 번호와 그 가격이 차
www.acmicpc.net

[ 문제풀이 ]
1. visited[ N ][ K ] 배열을 만들어 인터넷을 K번 공짜로 연결했을 때 해당 노드까지의 연결 값 중 최댓값을 저장합니다.
2. dijkstra를 통해 공짜로 연결했을 때와 하지 않았을 때 각각 priority_queue에 넣어줍니다.
3. N번 도시에 도착했을 때 dist를 출력합니다.
[ 소스코드 ]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | #include<iostream> #include<queue> #include<vector> using namespace std; struct node { int cur; int cost; int cnt; }; struct cmp { bool operator()(node right, node left) { return left.cost < right.cost; } }; int N, P, K; vector<pair<int, int>> list[1001]; int visited[1001][1001]; int main() { scanf("%d %d %d", &N, &P, &K); for (int i = 1; i <= N; i++) { for (int j = 0; j < N; j++) { visited[i][j] = 1111111111; } } for (int i = 0; i < P; i++) { int a, b, c; scanf("%d %d %d", &a, &b, &c); list[a].push_back({ b,c }); list[b].push_back({ a,c }); } priority_queue<node, vector<node>, cmp> pq; vector<int> temp; pq.push({ 1,0,0 }); while (!pq.empty()) { const int cur = pq.top().cur; const int cost = pq.top().cost; const int cnt = pq.top().cnt; pq.pop(); if (visited[cur][cnt] < cost) continue; visited[cur][cnt] = cost; if (cur == N) { printf("%d", cost); return 0; } for (auto& next : list[cur]) { const int nNode = next.first; const int nCost = next.second; if (cnt < K) { if (visited[nNode][cnt + 1] > cost) { pq.push({ nNode,cost,cnt+1 }); } } if (visited[nNode][cnt] > max(cost, nCost)) { pq.push({ nNode,max(cost,nCost),cnt }); } } } printf("-1"); } | cs |
반응형
'백준' 카테고리의 다른 글
[ 백준 ] 9879번 - Cross Country Skiing (C++) (0) | 2023.05.16 |
---|---|
[ 백준 ] 2310번 - 어드벤처 게임 (C++) (0) | 2023.05.15 |
[ 백준 ] 2820번 - 자동차 공장 (C++) (0) | 2023.05.13 |
[ 백준 ] 16404번 - 주식회사 승범이네 (C++) (0) | 2023.05.12 |
[ 백준 ] 14288번 - 회사 문화 4 (C++) (0) | 2023.05.11 |