[백준/C++] 1162번: 도로포장

-inn·2022년 1월 26일
0

백준

목록 보기
15/28

방법

다익스트라 알고리즘
1. dist[N][K]로 설정
2. 도로 포장하는 경우 vs 안하는 경우 조건 추가

주의사항
INF 987654321로 잡았는데 계속 66%에서 오류발생 -> 더 큰 값으로 잡고 해야 정답

코드

#include<bits/stdc++.h>
#define INF 9223372036854775800
using namespace std;
typedef long long ll;

struct info {	// 포장도로 수 추가
	ll n, w, k;
	info() {};
	info(ll n, ll w, ll k) :n(n), w(w), k(k) {};
	bool operator < (const info i) const {
		return w > i.w;
	}
};

int N, M, K;
vector<info> g[10001];
ll dist[10001][21];
bool check[10001][21];

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

	cin >> N >> M >> K;
	for (int i = 0; i <= N; i++) {
		for (int j = 0; j <= K; j++) {
			dist[i][j] = INF;
		}
	}
	for (int i = 0, u, v, w; i < M; i++) {	// 양방향 도로 만들기
		cin >> u >> v >> w;
		g[u].push_back({ v,w,0 });
		g[v].push_back({ u,w,0 });
	}

	priority_queue<info> pq;
	pq.push({ 1,0,0 });
	dist[1][0] = 0;
	while (!pq.empty()) {
		info cur = pq.top();
		pq.pop();
		ll cnt = cur.k;
		if (check[cur.n][cur.k]) continue;
		check[cur.n][cur.k] = true;
		for (int i = 0; i < g[cur.n].size(); i++) {
			ll nxt_n = g[cur.n][i].n;
			ll nxt_w = g[cur.n][i].w;
			// 포장 안하는 경우
			if (dist[nxt_n][cnt] > dist[cur.n][cnt] + nxt_w) {
				dist[nxt_n][cnt] = dist[cur.n][cnt] + nxt_w;
				pq.push({ nxt_n,dist[nxt_n][cnt],cnt });
			}
			// 포장 하는 경우
			if (dist[nxt_n][cnt + 1] > dist[cur.n][cnt] && (cnt + 1) <= K) {
				dist[nxt_n][cnt + 1] = dist[cur.n][cnt];
				pq.push({ nxt_n,dist[nxt_n][cnt + 1],cnt + 1 });
			}
		}
	}
	// K개 "이하"의 도로를 포장 -> 문제 똑바로 읽기
	ll ans = INF;
	for (int i = 0; i <= K; i++) {
		ans = min(ans, dist[N][i]);
	}
	cout << ans;

	return 0;
}
profile
☁️

0개의 댓글