[C++] ์ตœ๋‹จ ๊ฒฝ๋กœ(๋‹ค์ต์ŠคํŠธ๋ผ, ํ”Œ๋กœ์ด๋“œ ์™€์ƒฌ, ๋ฒจ๋งŒ ํฌ๋“œ)

2020. 5. 1. 10:50ยท๐Ÿ“ Computer Science/โœ Algorithm

์ตœ๋‹จ ๊ฒฝ๋กœ

๋‘ ์ •์  ์‚ฌ์ด์˜ ๊ฒฝ๋กœ ์ค‘ ๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜ ํ•ฉ์ด ์ตœ์†Œ์ธ ๊ฒฝ๋กœ๋ฅผ ์ฐพ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค.

 

1. ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜

๊ฐ€์ค‘์น˜๊ฐ€ ์ ์€ ์ˆœ์„œ๋Œ€๋กœ ๊ทธ ์ •์ ์— ์—ฐ๊ฒฐ๋œ ์ •์ ์„ ๋ฐฉ๋ฌธํ•˜๋ฉด์„œ ๊ฐ’์„ ๊ฐฑ์‹ ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค.

  • ์Œ์˜ ๊ฐ€์ค‘์น˜๋ฅผ ๊ฐ€์ง„ ๊ฒฝ๋กœ๋Š” ๊ตฌํ•  ์ˆ˜ ์—†๋‹ค.
  • ๊ฐ€์ค‘์น˜ ํ•ฉ์˜ ์ตœ๋Œ“๊ฐ’์€ ๊ฐ€์ค‘์น˜ ์ตœ๋Œ“๊ฐ’ * (์ •์  ๊ฐœ์ˆ˜ - 1) ์ด๋‹ค.

 

1) ๋ฌธ์ œ

 

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

 

1753๋ฒˆ: ์ตœ๋‹จ๊ฒฝ๋กœ

์ฒซ์งธ ์ค„์— ์ •์ ์˜ ๊ฐœ์ˆ˜ V์™€ ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜ E๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1≤V≤20,000, 1≤E≤300,000) ๋ชจ๋“  ์ •์ ์—๋Š” 1๋ถ€ํ„ฐ V๊นŒ์ง€ ๋ฒˆํ˜ธ๊ฐ€ ๋งค๊ฒจ์ ธ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” ์‹œ์ž‘ ์ •์ ์˜ ๋ฒˆํ˜ธ K(1≤K≤V)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์…‹์งธ ์ค„๋ถ€ํ„ฐ E๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ ๊ฐ ๊ฐ„์„ ์„ ๋‚˜ํƒ€๋‚ด๋Š” ์„ธ ๊ฐœ์˜ ์ •์ˆ˜ (u, v, w)๊ฐ€ ์ˆœ์„œ๋Œ€๋กœ ์ฃผ์–ด์ง„๋‹ค. ์ด๋Š” u์—์„œ v๋กœ ๊ฐ€๋Š” ๊ฐ€์ค‘์น˜ w์ธ ๊ฐ„์„ ์ด ์กด์žฌํ•œ๋‹ค๋Š” ๋œป์ด๋‹ค. u์™€ v๋Š” ์„œ๋กœ ๋‹ค๋ฅด๋ฉฐ w๋Š” 10 ์ดํ•˜์˜ ์ž์—ฐ์ˆ˜์ด๋‹ค. ์„œ๋กœ ๋‹ค๋ฅธ ๋‘

www.acmicpc.net

#include <iostream>
#include <vector>
#include <queue>
using namespace std;

const int INF = 1e9;

vector<pair<int, int>> graph[20001];
int dist[20001];

int main()
{
	int V, E, K;
	cin >> V >> E >> K;
	for (int i = 1, u, v, w; i <= E; i++) {
		cin >> u >> v >> w;
		graph[u].push_back(make_pair(v, w));
	}

	for (int i = 1; i <= V; i++)
		dist[i] = INF;

	priority_queue<pair<int, int>> pq;
	pq.push(make_pair(0, K)); // ๊ฐ€์ค‘์น˜, ์ •์ 
	dist[K] = 0;
	while (!pq.empty()) {
		int tv = pq.top().second;
		int tw = -pq.top().first;
		pq.pop();

		for (int i = 0; i < graph[tv].size(); i++) {
			int nv = graph[tv][i].first;
			int nw = tw + graph[tv][i].second;

			if (nw < dist[nv]) {
				dist[nv] = nw;
				pq.push(make_pair(-nw, nv));
			}
		}
	}

	for (int i = 1; i <= V; i++) {
		if (dist[i] == INF)
			cout << "INF\n";
		else
			cout << dist[i] << "\n";
	}
}

 

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

 

1504๋ฒˆ: ํŠน์ •ํ•œ ์ตœ๋‹จ ๊ฒฝ๋กœ

์ฒซ์งธ ์ค„์— ์ •์ ์˜ ๊ฐœ์ˆ˜ N๊ณผ ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜ E๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (2 ≤ N ≤ 800, 0 ≤ E ≤ 200,000) ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ E๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ์„œ ์„ธ ๊ฐœ์˜ ์ •์ˆ˜ a, b, c๊ฐ€ ์ฃผ์–ด์ง€๋Š”๋ฐ, a๋ฒˆ ์ •์ ์—์„œ b๋ฒˆ ์ •์ ๊นŒ์ง€ ์–‘๋ฐฉํ–ฅ ๊ธธ์ด ์กด์žฌํ•˜๋ฉฐ, ๊ทธ ๊ฑฐ๋ฆฌ๊ฐ€ c๋ผ๋Š” ๋œป์ด๋‹ค. (1 ≤ c ≤ 1,000) ๋‹ค์Œ ์ค„์—๋Š” ๋ฐ˜๋“œ์‹œ ๊ฑฐ์ณ์•ผ ํ•˜๋Š” ๋‘ ๊ฐœ์˜ ์„œ๋กœ ๋‹ค๋ฅธ ์ •์  ๋ฒˆํ˜ธ v1๊ณผ v2๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (v1 ≠ v2, v1 ≠ N, v2 ≠ 1)

www.acmicpc.net

#include <iostream>
#include <vector>
#include <queue>
using namespace std;

const int INF = 1e9;

int V, E;
vector<pair<int, int>> graph[801];
long long dist[801];

void Dijkstra(int start)
{
	for (int i = 1; i <= V; i++)
		dist[i] = INF;

	priority_queue<pair<int, int>> pq;
	pq.push(make_pair(0, start));
	dist[start] = 0;
	while (!pq.empty()) {
		int tv = pq.top().second;
		int tw = -pq.top().first;
		pq.pop();

		for (int i = 0; i < graph[tv].size(); i++) {
			int nv = graph[tv][i].first;
			int nw = tw + graph[tv][i].second;

			if (nw < dist[nv]) {
				dist[nv] = nw;
				pq.push(make_pair(-nw, nv));
			}
		}
	}
}

int main()
{
	cin >> V >> E;
	for (int i = 0, a, b, c; i < E; i++) {
		cin >> a >> b >> c;
		graph[a].push_back({ b, c });
		graph[b].push_back({ a, c });
	}

	int v1, v2;
	cin >> v1 >> v2;

	// res1: 1 -> v1 -> v2 -> v
	// res2: 1 -> v2 -> v1 -> v
	long long res1 = 0, res2 = 0;
	Dijkstra(1);
	res1 += dist[v1], res2 += dist[v2];
	Dijkstra(v1);
	res1 += dist[v2], res2 += dist[V];
	Dijkstra(v2);
	res1 += dist[V], res2 += dist[v1];

	res1 = res1 < res2 ? res1 : res2;
	if (res1 >= INF)
		cout << -1;
	else
		cout << res1;
}

 

2. ํ”Œ๋กœ์ด๋“œ ์™€์ƒฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜

๊ฑฐ์ณ๊ฐ€๋Š” ์ง€์ ์ด ๋‹ฌ๋ผ์งˆ ๋•Œ๋งˆ๋‹ค ์ตœ์†Ÿ๊ฐ’์„ ๊ฐฑ์‹ ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค. A→B๋กœ ๊ฐˆ ๋•Œ, A→B๋ณด๋‹ค A→C→B์™€ ๊ฐ™์ด C๋ฅผ ๊ฑฐ์ณ๊ฐ€๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋” ์ตœ์†Ÿ๊ฐ’์ด๋ผ๋ฉด ๊ฐฑ์‹ ํ•œ๋‹ค.

 

1) ๋ฌธ์ œ

 

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

 

11404๋ฒˆ: ํ”Œ๋กœ์ด๋“œ

์ฒซ์งธ ์ค„์— ๋„์‹œ์˜ ๊ฐœ์ˆ˜ n(1 ≤ n ≤ 100)์ด ์ฃผ์–ด์ง€๊ณ  ๋‘˜์งธ ์ค„์—๋Š” ๋ฒ„์Šค์˜ ๊ฐœ์ˆ˜ m(1 ≤ m ≤ 100,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์…‹์งธ ์ค„๋ถ€ํ„ฐ m+2์ค„๊นŒ์ง€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฒ„์Šค์˜ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋จผ์ € ์ฒ˜์Œ์—๋Š” ๊ทธ ๋ฒ„์Šค์˜ ์ถœ๋ฐœ ๋„์‹œ์˜ ๋ฒˆํ˜ธ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋ฒ„์Šค์˜ ์ •๋ณด๋Š” ๋ฒ„์Šค์˜ ์‹œ์ž‘ ๋„์‹œ a, ๋„์ฐฉ ๋„์‹œ b, ํ•œ ๋ฒˆ ํƒ€๋Š”๋ฐ ํ•„์š”ํ•œ ๋น„์šฉ c๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค. ์‹œ์ž‘ ๋„์‹œ์™€ ๋„์ฐฉ ๋„์‹œ๊ฐ€ ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” ์—†๋‹ค. ๋น„์šฉ์€ 100,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค. ์‹œ์ž‘

www.acmicpc.net

#include <iostream> 
#include <vector>
#include <algorithm>
using namespace std;

const int INF = 1e9;

int graph[101][101];

int main()
{
	int N, M;
	cin >> N >> M;
	for (int i = 1; i <= N; i++)
		for (int j = 1; j <= N; j++)
			graph[i][j] = INF;
	for (int i = 0, a, b, c; i < M; i++) {
		cin >> a >> b >> c;
		if (c < graph[a][b])
			graph[a][b] = c;
	}

	for (int k = 1; k <= N; k++) // ๊ฑฐ์ณ๊ฐ€๋Š” ๋…ธ๋“œ
		for (int i = 1; i <= N; i++) // ์ถœ๋ฐœ
			for (int j = 1; j <= N; j++) // ๋„์ฐฉ
				if (graph[i][k] + graph[k][j] < graph[i][j])
					graph[i][j] = graph[i][k] + graph[k][j];

	for (int i = 1; i <= N; i++) {
		for (int j = 1; j <= N; j++) {
			if (i == j || graph[i][j] == INF)
				cout << "0 ";
			else
				cout << graph[i][j] << " ";
		}
		cout << "\n";
	}
}

 

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

 

1956๋ฒˆ: ์šด๋™

์ฒซ์งธ ์ค„์— V์™€ E๊ฐ€ ๋นˆ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ฃผ์–ด์ง„๋‹ค. (2<=V<=400, 0<=E<=V*(V-1)) ๋‹ค์Œ E๊ฐœ์˜ ์ค„์—๋Š” ๊ฐ๊ฐ ์„ธ ๊ฐœ์˜ ์ •์ˆ˜ a, b, c๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. a๋ฒˆ ๋งˆ์„์—์„œ b๋ฒˆ ๋งˆ์„๋กœ ๊ฐ€๋Š” ๊ฑฐ๋ฆฌ๊ฐ€ c์ธ ๋„๋กœ๊ฐ€ ์žˆ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค.

www.acmicpc.net

#include <iostream> 
#include <vector>
#include <algorithm>
using namespace std;

int INF = 1e9;

int graph[401][401];

int main()
{
	int V, E;
	cin >> V >> E;
	for (int i = 1; i <= V; i++)
		for (int j = 1; j <= V; j++)
			graph[i][j] = INF;
	for (int i = 1, a, b, c; i <= E; i++) {
		cin >> a >> b >> c;
		graph[a][b] = c;
	}

	for (int k = 1; k <= V; k++)
		for (int i = 1; i <= V; i++)
			for (int j = 1; j <= V; j++)
				if (graph[i][k] + graph[k][j] < graph[i][j])
					graph[i][j] = graph[i][k] + graph[k][j];

	int ans = INF;
	for (int i = 1; i <= V; i++)
		ans = min(ans, graph[i][i]);
	if (ans == INF) 
		cout << -1;
	else 
		cout << ans;
}

 

3. ๋ฒจ๋งŒ ํฌ๋“œ ์•Œ๊ณ ๋ฆฌ์ฆ˜

  • ์Œ์˜ ๊ฐ€์ค‘์น˜๋ฅผ ํ—ˆ์šฉํ•˜์ง€๋งŒ ์Œ์˜ ์‚ฌ์ดํด์€ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.
  • ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•˜๋Š” ๊ณผ์ •์„ N๋ฒˆ ๋” ๋ฐ˜๋ณตํ•˜๊ณ  N๋ฒˆ์งธ์— ์ตœ๋‹จ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐฑ์‹ ๋œ๋‹ค๋ฉด ์Œ์˜ ์‚ฌ์ดํด์ด๋ผ๊ณ  ๊ฐ„์ฃผํ•œ๋‹ค.

 

1) ๋ฌธ์ œ

 

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

 

11657๋ฒˆ: ํƒ€์ž„๋จธ์‹ 

์ฒซ์งธ ์ค„์— ๋„์‹œ์˜ ๊ฐœ์ˆ˜ N (1 ≤ N ≤ 500), ๋ฒ„์Šค ๋…ธ์„ ์˜ ๊ฐœ์ˆ˜ M (1 ≤ M ≤ 6,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ M๊ฐœ์˜ ์ค„์—๋Š” ๋ฒ„์Šค ๋…ธ์„ ์˜ ์ •๋ณด A, B, C (1 ≤ A, B ≤ N, -10,000 ≤ C ≤ 10,000)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. 

www.acmicpc.net

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
	int N, M;
	cin >> N >> M;

	vector<pair<int, int>> v[501];
	for (int i = 0, A, B, C; i < M; i++) {
		cin >> A >> B >> C;
		v[A].push_back({ B, C });
	}

	long long dist[501];
	fill(dist, dist + N + 1, 1e9);
	dist[1] = 0;

	bool flag = false;
	for (int i = 1; i <= N; i++)
		for (int j = 1; j <= N; j++) {
			if (dist[j] == 1e9)
				continue;

			for (auto& k : v[j]) {
				if (dist[j] + k.second < dist[k.first]) {
					dist[k.first] = dist[j] + k.second;

					if (i == N)
						flag = true;
				}
			}
		}

	if (flag)
		cout << -1;
	else {
		for (int i = 2; i <= N; i++)
			printf("%lld\n", dist[i] == 1e9 ? -1 : dist[i]);
	}
}

 

www.acmicpc.net/problem/1865

 

1865๋ฒˆ: ์›œํ™€

์ฒซ ๋ฒˆ์งธ ์ค„์—๋Š” ํ…Œ์ŠคํŠธ์ผ€์ด์Šค์˜ ๊ฐœ์ˆ˜ TC(1 ≤ TC ≤ 5)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‘ ๋ฒˆ์งธ ์ค„๋ถ€ํ„ฐ TC๊ฐœ์˜ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๊ฐ€ ์ฐจ๋ก€๋กœ ์ฃผ์–ด์ง€๋Š”๋ฐ ๊ฐ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค์˜ ์ฒซ ๋ฒˆ์งธ ์ค„์—๋Š” ์ง€์ ์˜ ์ˆ˜ N(1 ≤ N ≤ 500),

www.acmicpc.net

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
	int T;
	cin >> T;
	while (T--) {
		int n, m, w;
		cin >> n >> m >> w;

		vector<pair<int, int>> v[501];
		for (int i = 0; i < m; ++i) {
			int s, e, t;
			cin >> s >> e >> t;
			v[s].push_back({ e, t });
			v[e].push_back({ s, t });
		}
		for (int i = 0; i < w; ++i) {
			int s, e, t;
			cin >> s >> e >> t;
			v[s].push_back({ e, -t });
		}

		int dist[501];
		fill(dist, dist + 501, 1e9);
		dist[1] = 0;

		bool flag = false;
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++) {
				for (auto& k : v[j]) {
					if (dist[j] + k.second < dist[k.first]) {
						dist[k.first] = dist[j] + k.second;

						if (i == n)
							flag = true;
					}
				}
			}

		cout << (flag == true ? "YES\n" : "NO\n");
	}
}
์ €์ž‘์žํ‘œ์‹œ (์ƒˆ์ฐฝ์—ด๋ฆผ)
'๐Ÿ“ Computer Science/โœ Algorithm' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • [C++] next_permutation, prev_permutation
  • [C++] ๋น„ํŠธ ์—ฐ์‚ฐ์ž
  • [C++] ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰(DFS), ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰(BFS)
  • [C++] ๋™์  ๊ณ„ํš๋ฒ•(Dynamic Programming)
Blxxming
Blxxming
CS ์ง€์‹๊ณผ ๊ณต๋ถ€ํ•˜๋‹ค ๋ฐฐ์šด ๊ฒƒ, ๊ฒฝํ—˜ํ•œ ๊ฒƒ ๋“ฑ์„ ๊ธฐ๋กํ•˜๋Š” ๋ธ”๋กœ๊ทธ์ž…๋‹ˆ๋‹ค.
  • Blxxming
    ๐Ÿ’ก๋ฒˆ๋œฉ๐Ÿ’ก
    Blxxming
  • ์ „์ฒด
    ์˜ค๋Š˜
    ์–ด์ œ
  • ๊ณต์ง€์‚ฌํ•ญ

    • Tech Interview
    • ๐Ÿ“š Tech (246)
      • ๐Ÿ“ Computer Science (96)
        • โœ OS (12)
        • โœ Network & Web (10)
        • โœ Database (11)
        • โœ Data Structure (6)
        • โœ Algorithm (40)
        • โœ Design Pattern (9)
        • โœ Cloud Computing (3)
        • โœ (5)
      • ๐Ÿ“ Language (73)
        • โœ Language (6)
        • โœ C & C++ (11)
        • โœ C# (19)
        • โœ JAVA (37)
      • ๐Ÿ“ Game (43)
        • โœ Computer Graphics (2)
        • โœ Unity (14)
        • โœ Unreal (26)
        • โœ (1)
      • ๐Ÿ“ Book (34)
        • โœ Effective (3)
        • โœ Game Server (16)
        • โœ Clean Code (14)
        • โœ (1)
  • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.0
Blxxming
[C++] ์ตœ๋‹จ ๊ฒฝ๋กœ(๋‹ค์ต์ŠคํŠธ๋ผ, ํ”Œ๋กœ์ด๋“œ ์™€์ƒฌ, ๋ฒจ๋งŒ ํฌ๋“œ)
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”