Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

19-miniron-v #70

Merged
merged 2 commits into from
Mar 6, 2024
Merged

19-miniron-v #70

merged 2 commits into from
Mar 6, 2024

Conversation

miniron-v
Copy link
Member

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

https://www.acmicpc.net/problem/1753
1753๋ฒˆ: ์ตœ๋‹จ๊ฒฝ๋กœ
๋ถ„๋ฅ˜: ๊ทธ๋ž˜ํ”„, ๋‹ค์ต์ŠคํŠธ๋ผ(๋ฐ์ดํฌ์ŠคํŠธ๋ผ)

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

1์‹œ๊ฐ„

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

๋‹ค์ต์ŠคํŠธ๋ผ๋ฅผ ์ œ๋Œ€๋กœ ๋‹ค๋ฃจ์ง€ ๋ชป ํ•ด์„œ, ๋ธ”๋กœ๊ทธ๋ฅผ ๋ณด๋ฉฐ ๊ตฌํ˜„ํ•ด๋ดค์Šต๋‹ˆ๋‹ค.

https://velog.io/@717lumos/์•Œ๊ณ ๋ฆฌ์ฆ˜-๋‹ค์ต์ŠคํŠธ๋ผDijkstra-์•Œ๊ณ ๋ฆฌ์ฆ˜
https://suyeon96.tistory.com/31

์ด ๋‘ ๋ธ”๋กœ๊ทธ๋ฅผ ์ฐธ๊ณ ํ•ด, ์šฐ์„ ์ˆœ์œ„ ํ ๋ฐฉ์‹์œผ๋กœ ๋‹ค์ต์ŠคํŠธ๋ผ๋ฅผ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ“š ์ „์ฒด ์ฝ”๋“œ

#include <iostream>
#include <vector>
#include <queue>

const int INF = 3000001;

std::vector<int> dijkstra(int start, int n, std::vector<std::vector<std::pair<int, int>>>& graph) {
	std::vector<int> min_weight(n + 1, INF);
	std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int>>, std::greater<std::pair<int, int>>> pq;

	min_weight[start] = 0;
	pq.push({ 0, start });

	while (pq.empty() == false) {
		int cur_weight = pq.top().first;
		int cur_node = pq.top().second;
		pq.pop();

		for (auto& pair : graph[cur_node]) {
			int next_node = pair.first;
			int next_weight = cur_weight + pair.second;

			if (next_weight < min_weight[next_node]) {
				min_weight[next_node] = next_weight;
				pq.push({ next_weight, next_node});
			}
		}
	}

	return min_weight;
}

int main() {
	// ์ž…๋ ฅ
	int V, E, start;
	std::cin >> V >> E >> start;

	std::vector<std::vector<std::pair<int, int>>> graph(V + 1);
	for (int i = 0; i < E; ++i) {
		int u, v, w;
		std::cin >> u >> v >> w;

		graph[u].push_back({v, w});
	}
	
	// ๊ณ„์‚ฐ
	std::vector<int> min_weight = dijkstra(start, V, graph);

	// ์ถœ๋ ฅ
	for (int i = 1; i < min_weight.size(); ++i) {
		if (min_weight[i] == INF) {
			std::cout << "INF\n";
			continue;
		}

		std::cout << min_weight[i] << "\n";
	}
}

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

C++์—์„œ pair๋ฅผ ๋งŒ๋“ค ๋•Œ std::make_pair(A, B);๋ฅผ ์“ฐ์ง€ ์•Š๊ณ  {A, B}๋งŒ ํ•ด๋„ ํŽ˜์–ด๊ฐ€ ๋งŒ๋“ค์–ด์ง„๋‹ค. (๋ฒ„์ „ ๋‚ฎ์•„์„œ ์•ˆ ๋˜๋Š” ์ค„ ์•Œ์•˜๋Š”๋ฐ ์‹ ์„ธ๊ณ„)

์ถ”๊ฐ€๋กœ { ๊ฐ€์ค‘์น˜, ๋…ธ๋“œ }๋กœ ๋„ฃ์œผ๋ฉด ์„ฑ๊ณต, { ๋…ธ๋“œ, ๊ฐ€์ค‘์น˜ }๋กœ ๋„ฃ์œผ๋ฉด ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋–ด๋Š”๋ฐ, pq์— ์ปค์Šคํ…€ ํ•จ์ˆ˜๋ฅผ ์“ฐ์ง€ ์•Š๊ณ  greater<std::pair<int, int>>๋ฅผ ์ผ๊ธฐ ๋•Œ๋ฌธ์œผ๋กœ ๋ณด์ธ๋‹ค. ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•ด์•ผ ์ตœ์†Ÿ๊ฐ’์ด ๋‚˜๊ฐ€๋Š” ๊ฑธ๋ณด๋ฉด, ์šฐ์„ ์ˆœ์œ„ ํ์˜ pop์ด ๋’ค์—์„œ ์ผ์–ด๋‚˜๋Š” ๋“ฏ.

greater<std::pair<int, int>>๋กœ intํ˜•์ฒ˜๋Ÿผ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ์ด๋•Œ ๋น„๊ต ๊ธฐ์ค€์€ first๋‹ค.


std::vector<int> dijkstra(int start, int n, std::vector<std::vector<std::pair<int, int>>>& graph) {
std::vector<int> min_weight(n + 1, INF);
std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int>>, std::greater<std::pair<int, int>>> pq;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::greater<>

pair<int, int> ํƒ€์ž…์ด ์ถ”๋ก ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๊ตณ์ด pair<int, int>๋ฅผ ์ ์–ด์ฃผ์ง€ ์•Š์•„๋„ ๋œ๋‹ต๋‹ˆ๋‹น

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๊ทธ๋Ÿผ์š”~

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์œ ๋‹ˆํ‹ฐ๋„ new Vector3()๋กœ ์ ๋˜ ๊ฑฐ new()๋กœ ์ƒ์„ฑ๋˜๊ฒŒ ํ•ด๋†จ๋˜๋ฐ... ์ ์  ์ฝ”๋“œ๊ฐ€ ์งง์•„์ง€๋Š” ์ชฝ์œผ๋กœ ๋ฐœ์ „ํ•˜๋„ค์š”.

int cur_weight = pq.top().first;
int cur_node = pq.top().second;
pq.pop();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if(min_weight[cur_node] < cur_weight) {
    continue;
}

๋ณดํ†ต ์—ฌ๊ธฐ์— ์ด๋Ÿฐ ์‹์œผ๋กœ ์ธ์ ‘ ๋…ธ๋“œ์— ๋Œ€ํ•ด ๊ฒ€์‚ฌํ•  ํ•„์š”๋„ ์—†๋Š” ๊ฒฝ์šฐ๋ฅผ ๊ฑธ๋Ÿฌ์ฃผ๋Š” ์ผ€์ด์Šค๋ฅผ ๋„ฃ์–ด์ฃผ๊ธฐ๋„ ํ•˜๋”๋ผ๊ตฌ์š”. ์ผ๋ถ€ ๋ฌธ์ œ๋Š” ์ด ์กฐ๊ฑด๋ฌธ์ด ์—†์œผ๋ฉด ํ„ฐ์ง€๋”๋ผ๊ตฌ์š”...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์•„๋งˆ ์ฝ”๋”ฉ ์Šคํƒ€์ผ์˜ ์ฐจ์ด์ธ ๊ฒƒ ๊ฐ™์ง€๋งŒ,

for (auto& pair : graph[cur_node]) {
	int next_node = pair.first;
	int next_weight = cur_weight + pair.second;

	if (next_weight < min_weight[next_node]) {
		min_weight[next_node] = next_weight;
		pq.push({ next_weight, next_node});
	}
}

์ด ๊ตฌ๋ฌธ์—์„œ ๊ฒ€์‚ฌํ•  ํ•„์š”๊ฐ€ ์—†๋Š” ๋…ธ๋“œ๋Š” ํ์— push ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ„ฐ์ง€์ง€ ์•Š๋Š” ์ด์œ !

@9kyo-hwang
Copy link

2021 KAKAO BLIND RECRUITMENT ํ•ฉ์Šน ํƒ์‹œ ์š”๊ธˆ
1504 ํŠน์ •ํ•œ ์ตœ๋‹จ ๊ฒฝ๋กœ
1238 ํŒŒํ‹ฐ

๊ฐœ์ธ์ ์œผ๋กœ ๊ดœ์ฐฎ๊ฒŒ ์ƒ๊ฐํ•˜๋Š” ๋‹ค์ต์ŠคํŠธ๋ผ ์‘์šฉ ๋ฌธ์ œ๋“ค์ž…๋‹ˆ๋‹ค. ํ•œ ๋ฒˆ ๊ธฐํšŒ๋˜๋ฉด ํ’€์–ด๋ณด์„ธ์š”~

@miniron-v miniron-v changed the title 19 miniron v 19-miniron-v Feb 29, 2024
Copy link
Collaborator

@Redish03 Redish03 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๊ณ ์ƒํ•˜์…จ์Šต๋‹ˆ๋‹ค :)

๋‹ค์ต์ŠคํŠธ๋ผ์˜ ๊ธฐ๋ณธ ๊ฐ™์€ ๋ฌธ์ œ๋ผ ์ €๋„ ๋ณต์Šต ํ•˜๊ณ  ๊ฐ‘๋‹ˆ๋‹ค..,ใ…Žใ…Ž


std::vector<int> dijkstra(int start, int n, std::vector<std::vector<std::pair<int, int>>>& graph) {
std::vector<int> min_weight(n + 1, INF);
std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int>>, std::greater<std::pair<int, int>>> pq;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๊ทธ๋Ÿผ์š”~

std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int>>, std::greater<std::pair<int, int>>> pq;

min_weight[start] = 0;
pq.push({ 0, start });
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ œ PR์—์„œ ๊ตํ™ฉ์ฟค์ด ์–˜๊ธฐํ•ด์คฌ๋˜ ๊ฑฐ ๊ฐ™์€๋ฐ make_pair()์ด๋‚˜ ๋ฉ”์„œ๋“œ ๋ณด๋‹ค {x, y}๋กœ ๋„ฃ๋Š”๊ฑธ ์ถ”์ฒœ๋“œ๋ฆฌ๋”๋ผ๊ตฌ์š” :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋„ˆ๋ฌด ํŽธํ•˜๋”๋ผ๊ณ ์š”... std::make_pair(x, y)๋ฅผ ์•ˆ ์จ๋„ ๋„๋”˜๋‹ค๋‹ˆ

Copy link
Member

@bomik0221 bomik0221 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ €๋„ ๋‹ค์ต์ŠคํŠธ๋ผ ๋ฌธ์ œ๋Š” ์•ˆ ํ•ด๋ดค๋Š”๋ฐ, ๋•๋ถ„์— ์•Œ์•„๊ฐ‘๋‹ˆ๋‹ค! ๊ณ ์ƒํ•˜์…จ์Šต๋‹ˆ๋‹ค :)

Copy link
Collaborator

@2secondag 2secondag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ฝ”๋“œ ์ž˜ ๋ดค์Šต๋‹ˆ๋‹ค~!
๋‹ค์ต์ŠคํŠธ๋ผ ๋ฌธ์ œ๋Š” ์ฒ˜์Œ ๋ณด๋Š” ๊ฒƒ ๊ฐ™์€๋ฐ ์‹ ๊ธฐํ•˜๋„ค์š” ๋ฐฐ์›Œ๊ฐ‘๋‹ˆ๋‹ค...!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants