๐Ÿ™‡โ€โ™€๏ธ[Gold IV] ์ตœ๋‹จ๊ฒฝ๋กœ - 1753

๋ฌธ์ œ ๋งํฌ

์„ฑ๋Šฅ ์š”์•ฝ

๋ฉ”๋ชจ๋ฆฌ: 9192 KB, ์‹œ๊ฐ„: 100 ms

๋ถ„๋ฅ˜

๋ฐ์ดํฌ์ŠคํŠธ๋ผ, ๊ทธ๋ž˜ํ”„ ์ด๋ก , ์ตœ๋‹จ ๊ฒฝ๋กœ

์ œ์ถœ ์ผ์ž

2024๋…„ 1์›” 3์ผ 20:07:13

๋ฌธ์ œ ์„ค๋ช…

๋ฐฉํ–ฅ๊ทธ๋ž˜ํ”„๊ฐ€ ์ฃผ์–ด์ง€๋ฉด ์ฃผ์–ด์ง„ ์‹œ์ž‘์ ์—์„œ ๋‹ค๋ฅธ ๋ชจ๋“  ์ •์ ์œผ๋กœ์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ๋‹จ, ๋ชจ๋“  ๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜๋Š” 10 ์ดํ•˜์˜ ์ž์—ฐ์ˆ˜์ด๋‹ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์ •์ ์˜ ๊ฐœ์ˆ˜ 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 ์ดํ•˜์˜ ์ž์—ฐ์ˆ˜์ด๋‹ค. ์„œ๋กœ ๋‹ค๋ฅธ ๋‘ ์ •์  ์‚ฌ์ด์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฐ„์„ ์ด ์กด์žฌํ•  ์ˆ˜๋„ ์žˆ์Œ์— ์œ ์˜ํ•œ๋‹ค.

์ถœ๋ ฅ

์ฒซ์งธ ์ค„๋ถ€ํ„ฐ V๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ, i๋ฒˆ์งธ ์ค„์— i๋ฒˆ ์ •์ ์œผ๋กœ์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ์˜ ๊ฒฝ๋กœ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค. ์‹œ์ž‘์  ์ž์‹ ์€ 0์œผ๋กœ ์ถœ๋ ฅํ•˜๊ณ , ๊ฒฝ๋กœ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์—๋Š” INF๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค.

๐Ÿš€ํ’€์ด

๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ’€์–ด์•ผํ•œ๋‹ค.

์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๋ฐ ๊ทธ ์ด์œ ๋Š” ๋งค๋ฒˆ ์ตœ์†Œ๊ฐ’์„ ์ฐพ์•„์•ผํ•˜๋Š”๋ฐ ์šฐ์„ ์ˆœ์œ„ํ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด O(N)์‹œ๊ฐ„์ด ๊ฑธ๋ฆฌ๋Š”๋ฐ ์šฐ์„ ์ˆœ์œ„ ํ๋Š” O(logN)์ด ๊ฑธ๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

์ตœ์†Œ๊ฐ’์„ ์ฐพ์œผ๋ฉด ๊ทธ ์ •์ ์—์„œ ์—ฐ๊ฒฐ๋œ ์ •์ ๋“ค์„ ์‚ดํ•€๋‹ค.

์ด ๋•Œ ์ตœ์†Œ๊ฐ’์€ ์–ด๋–ค ๊ฒฝ์šฐ์—๋„ ๋” ์ž‘์•„์งˆ์ˆ˜์—†๋Š” ๊ฐ’์ด๋ผ๋Š”๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค.

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

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include<iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;

struct Edge
{
	int v;
	int dist;

	bool operator<(const Edge& other) const
	{
		return dist > other.dist;
	}
};

int n, m, k, a, b, c;
int ch[30];
priority_queue<Edge> pq;
vector<vector<pair<int, int>>> graph;
vector<int> dist;
void solve()
{
	cin >> n >> m >> k;

	// ๋ชจ๋“  ์ •์ ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๋Š” ๋งค์šฐ ํฐ ์ˆซ์ž๋กœ ์ดˆ๊ธฐํ™”
	dist = vector<int>(n + 1, 123456789);
	graph = vector<vector<pair<int, int>>>(n + 1);

	// ๊ฐ€์ค‘์น˜ ๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„ ์ƒ์„ฑ
	for (int i = 1; i <= m; ++i)
	{
		cin >> a >> b >> c;
		graph[a].push_back({ b, c });
	}

	// ์šฐ์„ ์ˆœ์œ„ ํ ์‹œ์ž‘ ์ •์  ์‚ฝ์ž…
	pq.push({ k, 0 });
	dist[k] = 0; // ์ฒ˜์Œ ์‹œ์ž‘์€ ๊ฑฐ๋ฆฌ๊ฐ€ 0
	while (pq.empty() == false)
	{
		int cur = pq.top().v;
		int cost = pq.top().dist;
		pq.pop();

		// ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ๊ณณ์ธ๋ฐ ๋” ์ž‘์€ ๊ฐ’์ด ์กด์žฌํ•œ๋‹ค๋ฉด ์Šคํ‚ต
		if (cost > dist[cur])
			continue;

		// ์ •์ ๊ณผ ์—ฐ๊ฒฐ๋œ ์ •์ ๋“ค ์ˆœํšŒ
		for (int i = 0; i < graph[cur].size(); ++i)
		{
			int next = graph[cur][i].first;
			int nextCost = cost + graph[cur][i].second; // ์ด์ „๊นŒ์ง€์˜ ๋น„์šฉ๊ณผ ๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜์˜ ํ•ฉ
			if (nextCost < dist[next]) // ์ฐ๋˜๋ฐฐ๊ธฐ ์ž‘์€ ๊ฐ’
			{
				dist[next] = nextCost; // ๊ฑฐ๋ฆฌ๊ฐ’ ๊ฐฑ์‹ 
				pq.push({ next, nextCost }); // ์šฐ์„ ์ˆœ์œ„ ํ์— ์‚ฝ์ž…
			}
		}
	}

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

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	//freopen("input.txt", "rt", stdin);

	solve();

	return 0;
}

ํƒœ๊ทธ:

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ: