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

4-InSange #15

Merged
merged 2 commits into from
Mar 26, 2024
Merged

4-InSange #15

merged 2 commits into from
Mar 26, 2024

Conversation

InSange
Copy link
Collaborator

@InSange InSange commented Mar 25, 2024

πŸ”— 문제 링크

νŒŒν‹°

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

2일

✨ μˆ˜λ„ μ½”λ“œ

이 λ¬Έμ œλŠ” μ΅œλ‹¨ 경둜λ₯Ό νƒμƒ‰ν•˜λŠ” 문제둜 λ‹€λ₯Έ λ¬Έμ œμ™€ 차별점은 μΆœλ°œμ μ—μ„œ 도착점(X)둜 κ°”λ‹€κ°€ 도착점(X)μ—μ„œ λ‹€μ‹œ 좜발점으둜 λŒμ•„μ™€μ£Όμ–΄μ•Ό ν•œλ‹€λŠ” μ μž…λ‹ˆλ‹€.
ν•΄λ‹Ή λ¬Έμ œλŠ” 2κ°€μ§€μ˜ λ°©λ²•μœΌλ‘œ μ ‘κ·Όν•˜μ˜€μŠ΅λ‹ˆλ‹€.

ν”Œλ‘œμ΄λ“œ μ›Œμ…œ

도착점(X)λŠ” λ§ˆμ„(N)에 ν¬ν•¨λ˜κΈ° λ•Œλ¬Έμ— λͺ¨λ“  λ§ˆμ„(X)κ°„μ˜ 경둜λ₯Ό νƒμƒ‰ν•˜μ—¬ λ§ˆμ„(N)끼리의 μ΅œλ‹¨ κ²½λ‘œλ“€μ„ λ‹€ κ΅¬ν•΄μ€˜μ„œ κ°€λŠ”κΈΈ, λŒμ•„μ˜€λŠ” 길의 합이 κ°€μž₯ 큰 값을 λ‹΅μœΌλ‘œ μ œμΆœν•΄μ£Όμ—ˆμŠ΅λ‹ˆλ‹€.
ν”Œλ‘œμ΄λ“œ μ›Œμ…œμ— κ΄€ν•œ μ„€λͺ… 및 λ‚΄μš©μ€ ν”Œλ‘œμ΄λ“œλ¬Έμ œ PRμ—μ„œ ν™•μΈν•˜μ‹€ 수 μžˆμŠ΅λ‹ˆλ‹€.

const int MAX_INT = 99999999;
int N, M, X, n, m, t, answer;
 vector<vector<int>> towns;
 <summary>
 ν”Œλ‘œμ΄λ“œ μ›Œμ…œ
 </summary>
void Floyd()
{
	// λ§ˆμ„(N), λ„λ‘œ(M), 도착지(X) μ„€μ •
	cin >> N >> M >> X;

	answer = 0;
	towns.assign(N+1, vector<int>(N+1, MAX_INT));

	while (M--)
	{       // κ°„μ„ λ“€ μž…λ ₯(λ„λ‘œ)
		cin >> n >> m >> t;
		towns[n][m] = t;
	}
        // ν”Œλ‘œμ΄λ“œ μ›Œμ…œλ‘œ K 지점에 ν•΄λ‹Ήν•˜λŠ” μ’Œν‘œλ“€μ˜ 값듀을 λΉ„κ΅ν•˜μ—¬
       // ν–‰κ³Ό 열이 λ§Œλ‚˜λŠ” μ’Œν‘œμ˜ 값을 μ΅œμ†Œλ‘œ μ„€μ •ν•΄μ€€λ‹€.
	for (int k = 1; k <= N; k++)
	{
		towns[k][k] = 0;
		for (int i = 1; i <= N; i++)
		{
			if (towns[i][k] == MAX_INT) continue;
			for (int j = 1; j <= N; j++)
			{
				towns[i][j] = min(towns[i][j], towns[i][k] + towns[k][j]);
			}
		}
	}
        // 각 λ§ˆμ„ iλ₯Ό λŒ€μƒμœΌλ‘œ μ™”λ‹€κ°”λ‹€ν•˜λŠ”λ° κ±Έλ¦¬λŠ” μ΅œλŒ€ κ°’
	for (int i = 1; i <= N; i++)
	{
		answer = max(answer, towns[i][X] + towns[X][i]);
	}

	cout << answer << "\n";
}

λ‹€μ΅μŠ€νŠΈλΌ

λ‹€μ΅μŠ€νŠΈλΌλŠ” 갈 수 μžˆλŠ” λͺ¨λ“  간선듀을 μš°μ„  μˆœμœ„ 큐에 μ €μž₯을 ν•˜κ³  μ΅œμ†Œ κ°’λ“€ μš°μ„  κΈ°μ€€μœΌλ‘œ ν˜„μž¬ λ§ˆμ„(cur)μ—μ„œ λ‹€μŒ λ§ˆμ„(next)둜 μ΄λ™ν•˜λŠ”λ° λ“œλŠ” μ΅œμ†Œ λΉ„μš©μ„ λΉ„κ΅ν•˜μ—¬ μ—…λ°μ΄νŠΈ μ‹œμΌœμ€λ‹ˆλ‹€.
단 λ§ˆμ„κ³Ό λ§ˆμ„μ„ μž‡λŠ” λ„λ‘œ(M)을 μ œμ™Έν•œ λͺ¨λ“  κ²½λ‘œλŠ” 갈 수 μ—†κΈ° λ•Œλ¬Έμ— μ΅œμ†Œ λΉ„μš©μ„ κ΅¬ν•΄μ•Όν•˜λŠ” μƒν™©μ—μ„œ λ‹€λ₯Έ κ²½λ‘œλ“€μ€ 쳐닀도보지 λͺ»ν•˜κ²Œ λ¬΄ν•œνžˆ 큰 κ°’μœΌλ‘œ μ„€μ •ν•΄λ‘‘λ‹ˆλ‹€.

const int MAX_INT = 99999999;
int N, M, X, n, m, t, answer;
vector<vector<pair<int, int>>> towns;
vector<int> dist;
int result, temp;
/// <summary>
/// λ‹€μ΅μŠ€νŠΈλΌ
/// </summary>
void dijkstra(int start, int end)
{       // ν˜„μž¬ λ§ˆμ„ startμ—μ„œ λ‹€λ₯Έ λ§ˆμ„κΉŒμ§€ κ°€λŠ”λ° κ±Έλ¦¬λŠ” 거리
	dist.clear();
	dist.resize(N + 1, MAX_INT);
	dist[start] = 0;
        // μš°μ„  μˆœμœ„ 큐 ν™œμš©. first = λΉ„μš©, second = λ§ˆμ„ 번호
        // λ§ˆμ„λ‘œ μ΄λ™ν•˜λŠ”λ° μ΅œμ†Œ λΉ„μš©μ„ 확인해주기 μœ„ν•΄μ„œ μ˜€λ¦„μ°¨μˆœ μ„€μ •
	priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
	pq.push({ 0, start });
        // ν˜„μž¬ λ§ˆμ„κ³Ό λ‹€μŒ λ§ˆμ„μ— κ±Έλ¦¬λŠ” λΉ„μš©λ“€μ„ dist 배열에 μ—…λ°μ΄νŠΈ ν•΄μ€€λ‹€!
        // λΉ„μš©μ„ μ—…λ°μ΄νŠΈμ‹œ ν•΄λ‹Ή λ§ˆμ„μ„ κΈ°μ€€μœΌλ‘œ 또 λ‹€λ₯Έ ꡬ역을 νƒμƒ‰ν•˜λŠ”λ°λ“œλŠ” μ΅œμ†Œ λΉ„μš©μ„ κΌ­ ν•„μ‹œ 확인해주어야함
	while (!pq.empty())
	{
		int cur = pq.top().second;
		int cost = pq.top().first;
		pq.pop();

		if (cur == end)
		{
			temp = dist[cur];
			break;
		}
		for (auto road : towns[cur])
		{
			int next = road.first;
			int next_cost = road.second + cost;

			if (next_cost < dist[next])
			{
				dist[next] = next_cost;
				pq.push({ next_cost, next });
			}
		}
	}
}

void Solve()
{
	cin >> N >> M >> X;

	answer = 0;
	towns.assign(N + 1, vector<pair<int ,int>>());

	while (M--)
	{
		cin >> n >> m >> t;
		towns[n].push_back({ m, t });
	}
        // μ‹œμž‘ i μ—μ„œ 도착 X κΉŒμ§€ κ°€λŠ”λ° λΉ„μš©κ³Ό
        // μ‹œμž‘ Xμ—μ„œ 도착 i κΉŒμ§€ λ“œλŠ” λΉ„μš©μ„ ν•©ν•΄ answer와 비ꡐ ν›„ μ—…λ°μ΄νŠΈ
	for (int i = 1; i <= N; i++)
	{
		result = 0;
		dijkstra(i, X);
		result += temp;
		dijkstra(X, i);
		result += temp;
		answer = max(answer, result);
	}

	cout << answer;
}

ν›„κΈ°

μ†”μ§νžˆ λ§ν•˜λ©΄ ν•΄λ‹Ή λ¬Έμ œλŠ” ν”Œλ‘œμ΄λ“œ μ›Œμ…œ 15λΆ„, λ‹€μ΅μŠ€νŠΈλΌλŠ” 30λΆ„ 밖에 μ•ˆκ±Έλ Έλ‹€.
κ·ΈλŸ¬λ‚˜ ν‘ΈλŠ”λ° 2일이 κ±Έλ¦° μ΄μœ λŠ”

for (int i = 1; i <= n; i++)
	{
		int depart_cost, return_cost;
		dijkstra(i, X);
		depart_cost = dist[X];
		dijkstra(X, i);
		return_cost = dist[i];
		answer = max(answer, depart_cost + return_cost);
	}

각 λ§ˆμ„μ— λŒ€ν•œ 비ꡐλ₯Ό λ§ˆμ„ 수(N)이 μ•„λ‹Œ μ‹œμž‘ 지점(n)으둜 탐색을 ν•΄μ€˜λ²„λ ΈκΈ° λ•Œλ¬Έμ— 4%μ—μ„œ 계속 ν‹€λ Έλ‹€κ³  λ‚˜μ˜¨ 것 μ΄μ—ˆλ‹€..!!!
λ³€μˆ˜ 선언을 μ „μ—­ λ³€μˆ˜λ‘œ ν•΄μ£Όλ‹€λ³΄λ‹ˆ 차라리 n이 지역 λ³€μˆ˜μ˜€λ‹€λ©΄ 였λ₯˜λΌλ„ λ°˜ν™˜ν–ˆμ„ 터인데 μž…λ ₯이 λ‹€ λ“€μ–΄κ°„ μƒνƒœλ‹€ λ³΄λ‹ˆ 디버깅을 해도 λ¬Έμ œκ°€ μ—†λ‹€κ³  λ–΄μ—ˆλ‹€...
κ·Έλ‘œλΆ€ν„° λΈ”λ‘œκ·Έμ™€ 비ꡐλ₯Ό 해도 λ‚˜μ˜ 풀이가 ν‹€λ¦°κ²Œ μ—†μ—ˆμŒμ—λ„ 이 ν•œ κΈ€μžμ°¨μ΄ λ•Œλ¬Έμ— ν•˜λ£¨λ™μ•ˆ 내내생각을 ν•˜κ²Œ λ˜μ—ˆμ—ˆλ‹€..
image

μΆ”κ°€λ‘œ ν”Œλ‘œμ΄λ“œ μ›Œμ…œμ˜ 경우 μ΅œλŒ€ 연산이 λ§ˆμ„ 수(N)에 κ²°μ • λ˜λ‹€λ³΄λ‹ˆ 1000^3으둜 C++μ΄λ‚˜ Cλ₯Ό μ œμ™Έν•œ λ‹€λ₯Έ μ–Έμ–΄μ—μ„œ μ‹œκ°„ μ΄ˆκ³Όκ°€ 많이 λ‚œλ‹€κ³ λ“€ ν•œλ‹€.
λ‚˜μ˜ 경우 ν”Œλ‘œμ΄λ“œ μ›Œμ…œμ—μ„œ νƒμƒ‰ν•˜λŠ” kκ°€ λ¬΄ν•œλŒ€μ΄λ©΄ 쳐닀도 보지도 μ•Šκ³  μŠ€ν‚΅ν•˜μ˜€κΈ° λ•Œλ¬Έμ— μƒκ΄€μ—†μ—ˆμ§€λ§Œ 이런 뢀뢄도 꼼꼼히 잘 체크해야 κ² λ‹€κ³  생각이 λ“€μ—ˆλ‹€.

if (towns[i][k] == MAX_INT) continue;

ν”Œλ‘œμ΄λ“œ μ›Œμ…œλ‘œ ν’€μ—ˆμ„ μ‹œ 500ms 반면 λ‹€μ΅μŠ€νŠΈλΌμ—μ„œλŠ” 80ms ν™•μ—°ν•œ 차이λ₯Ό λ³Ό 수 μžˆμ—ˆλ‹€.

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

λ³€μˆ˜λͺ…을 잘 ν™•μΈν•˜μž ^^

Copy link
Contributor

@dhlee777 dhlee777 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

@yuyu0830 yuyu0830 left a comment

Choose a reason for hiding this comment

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

μ•„! 제 μ½”λ“œλž‘ λΉ„κ΅ν–ˆλŠ”λ° 제 μ½”λ“œκ°€ μ™œμΌ€ λ”λŸ½λ‚˜ ν–ˆλ”λ‹ˆ λ‹€μ΅μŠ€νŠΈλΌλ₯Ό 2번 μ‹€ν–‰ν•˜λŠ” 뢀뢄을 묢을 수 μžˆμ—ˆλ„€μš”.. κΉ”λ”ν•˜κ³  이해가 μ‰½λ„€μš” γ…Žγ…Ž
λ³€μˆ˜ λͺ…은 λ˜λ„λ‘ κ²ΉμΉ˜μ§€ μ•Šκ²Œ μ‚¬μš©ν•©μ‹œλ‹€..

Copy link
Collaborator

@seongwon030 seongwon030 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.

4 participants