🙇‍♀️[Gold V] 최소비용 구하기 - 1916

문제 링크

성능 요약

메모리: 4748 KB, 시간: 36 ms

분류

데이크스트라, 그래프 이론, 최단 경로

제출 일자

2024년 1월 5일 16:32:04

문제 설명

N개의 도시가 있다. 그리고 한 도시에서 출발하여 다른 도시에 도착하는 M개의 버스가 있다. 우리는 A번째 도시에서 B번째 도시까지 가는데 드는 버스 비용을 최소화 시키려고 한다. A번째 도시에서 B번째 도시까지 가는데 드는 최소비용을 출력하여라. 도시의 번호는 1부터 N까지이다.

입력

첫째 줄에 도시의 개수 N(1 ≤ N ≤ 1,000)이 주어지고 둘째 줄에는 버스의 개수 M(1 ≤ M ≤ 100,000)이 주어진다. 그리고 셋째 줄부터 M+2줄까지 다음과 같은 버스의 정보가 주어진다. 먼저 처음에는 그 버스의 출발 도시의 번호가 주어진다. 그리고 그 다음에는 도착지의 도시 번호가 주어지고 또 그 버스 비용이 주어진다. 버스 비용은 0보다 크거나 같고, 100,000보다 작은 정수이다.

그리고 M+3째 줄에는 우리가 구하고자 하는 구간 출발점의 도시번호와 도착점의 도시번호가 주어진다. 출발점에서 도착점을 갈 수 있는 경우만 입력으로 주어진다.

출력

첫째 줄에 출발 도시에서 도착 도시까지 가는데 드는 최소 비용을 출력한다.

🚀풀이

다익스트라를 그대로 구현하면 되는 문제였다.

먼저 정점 정보를 구조체로 만들었다.

struct Edge
{
	int v;
	int cost;

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

방향 가중치 그래프를 만들기.

int n, m, go, finish;
vector<vector<pair<int, int>>> graph;
vector<int> dist;
void setting()
{
	cin >> n >> m;
	graph = vector<vector<pair<int, int>>>(n + 1);
	dist = vector<int>(n + 1, 123456789);
	int a, b, c;
	for (int i = 0; i < m; ++i)
	{
		cin >> a >> b >> c;
		graph[a].push_back({ b, c });
	}

	cin >> go >> finish;
}

이제 다익스트라를 구현한다.

priority_queue<Edge> pq;
void dijkstra(int here, int distance)
{
	pq.push({ here, distance });
	dist[here] = 0;
	while (pq.empty() == false)
	{
		int here = pq.top().v;
		int cost = pq.top().cost;
		pq.pop();

		if (cost > dist[here])
			continue;

		for (int there = 0; there < graph[here].size(); ++there)
		{
			int next = graph[here][there].first;
			int nextCost = cost + graph[here][there].second;
			if (nextCost < dist[next])
			{
				dist[next] = nextCost;
				pq.push({ next, nextCost });
			}
		}
	}
}

우선순위 큐로 각 정점의 정보를 입력하고 최소값이 들어가게 값을 비교하면서 넣는다.

🚀전체 코드

#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 cost;

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

int n, m, go, finish;
vector<vector<pair<int, int>>> graph;
vector<int> dist;
void setting()
{
	cin >> n >> m;
	graph = vector<vector<pair<int, int>>>(n + 1);
	dist = vector<int>(n + 1, 123456789);
	int a, b, c;
	for (int i = 0; i < m; ++i)
	{
		cin >> a >> b >> c;
		graph[a].push_back({ b, c });
	}

	cin >> go >> finish;
}

priority_queue<Edge> pq;
void dijkstra(int here, int distance)
{
	pq.push({ here, distance });
	dist[here] = 0;
	while (pq.empty() == false)
	{
		int here = pq.top().v;
		int cost = pq.top().cost;
		pq.pop();

		if (cost > dist[here])
			continue;

		for (int there = 0; there < graph[here].size(); ++there)
		{
			int next = graph[here][there].first;
			int nextCost = cost + graph[here][there].second;
			if (nextCost < dist[next])
			{
				dist[next] = nextCost;
				pq.push({ next, nextCost });
			}
		}
	}
}

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

	setting();
	dijkstra(go, 0);
	cout << dist[finish];

	return 0;
}