반응형
HackerRank링크
https://www.hackerrank.com/challenges/dijkstrashortreach/problem
코드 풀이
일반적인 다익스트라 풀이였다.
- 결과를 저장할 Result벡터, 각 노드간 최종 거리를 저장할 Dist벡터, Graph를 새로 저장하기위한 벡터 선언
- 다익스트라를 실행하기위한 우선순위큐 선언
- 초기값 push
- Graph 새로 연결
- 기본적인 다익스트라 코드 구현
- 최종적인 결과값들 Result로 push_back
코드
#include <bits/stdc++.h>
using namespace std;
string ltrim(const string &);
string rtrim(const string &);
vector<string> split(const string &);
/*
* Complete the 'shortestReach' function below.
*
* The function is expected to return an INTEGER_ARRAY.
* The function accepts following parameters:
* 1. INTEGER n
* 2. 2D_INTEGER_ARRAY edges
* 3. INTEGER s
*/
#define INF 2000000000
vector<int> shortestReach(int n, vector<vector<int>> edges, int s)
{
vector<int> Result;
vector<int> vecDist(n + 1, INF);
vector<pair<int, int>> Graph[3001];
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({0, s});
vecDist[s] = 0;
for(int i = 0; i < edges.size(); ++i)
{
int NodeA = edges[i][0];
int NodeB = edges[i][1];
int Cost = edges[i][2];
Graph[NodeA].push_back({NodeB, Cost});
Graph[NodeB].push_back({NodeA, Cost});
}
while(!pq.empty())
{
int CurNode = pq.top().second;
int CurCost = pq.top().first;
pq.pop();
if(vecDist[CurNode] < CurCost)
{
continue;
}
size_t Size = Graph[CurNode].size();
for(size_t i = 0; i < Size; ++i)
{
int NextCost = Graph[CurNode][i].second + CurCost;
int NextNode = Graph[CurNode][i].first;
if(vecDist[NextNode] > NextCost)
{
pq.push({NextCost, NextNode});
vecDist[NextNode] = NextCost;
}
}
}
for(int i = 1; i < vecDist.size(); ++i)
{
if(i == s)
continue;
int Value = vecDist[i] == INF ? -1 : vecDist[i];
Result.push_back((Value));
}
return Result;
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
string t_temp;
getline(cin, t_temp);
int t = stoi(ltrim(rtrim(t_temp)));
for (int t_itr = 0; t_itr < t; t_itr++) {
string first_multiple_input_temp;
getline(cin, first_multiple_input_temp);
vector<string> first_multiple_input = split(rtrim(first_multiple_input_temp));
int n = stoi(first_multiple_input[0]);
int m = stoi(first_multiple_input[1]);
vector<vector<int>> edges(m);
for (int i = 0; i < m; i++) {
edges[i].resize(3);
string edges_row_temp_temp;
getline(cin, edges_row_temp_temp);
vector<string> edges_row_temp = split(rtrim(edges_row_temp_temp));
for (int j = 0; j < 3; j++) {
int edges_row_item = stoi(edges_row_temp[j]);
edges[i][j] = edges_row_item;
}
}
string s_temp;
getline(cin, s_temp);
int s = stoi(ltrim(rtrim(s_temp)));
vector<int> result = shortestReach(n, edges, s);
for (size_t i = 0; i < result.size(); i++) {
fout << result[i];
if (i != result.size() - 1) {
fout << " ";
}
}
fout << "\n";
}
fout.close();
return 0;
}
string ltrim(const string &str) {
string s(str);
s.erase(
s.begin(),
find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
);
return s;
}
string rtrim(const string &str) {
string s(str);
s.erase(
find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
s.end()
);
return s;
}
vector<string> split(const string &str) {
vector<string> tokens;
string::size_type start = 0;
string::size_type end = 0;
while ((end = str.find(" ", start)) != string::npos) {
tokens.push_back(str.substr(start, end - start));
start = end + 1;
}
tokens.push_back(str.substr(start));
return tokens;
}
반응형
'HackerRank' 카테고리의 다른 글
HakerRank : Revising the Select Query I (SQL) (0) | 2023.10.19 |
---|---|
HackerRank : Apple and Orange (c++) (0) | 2023.10.19 |
댓글