CSES - Aalto Competitive Programming 2024 - wk6 - Homework - Results
Submission details
Task:Shortest Routes I
Sender:Rasse
Submission time:2024-10-04 07:43:10 +0300
Language:C++ (C++11)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:26:40: error: 'LLONG_MAX' was not declared in this scope
   26 |     vector<long long> dist(adj.size(), LLONG_MAX);
      |                                        ^~~~~~~~~
input/code.cpp:7:1: note: 'LLONG_MAX' is defined in header '<climits>'; did you forget to '#include <climits>'?
    6 | #include <queue>
  +++ |+#include <climits>
    7 | 
input/code.cpp:53:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   53 |     for (int i = 1; i < adj.size(); i++)
      |                     ~~^~~~~~~~~~~~

Code

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <unordered_map>
#include <queue>
 
using namespace std;
 
int main()
{
    int n, m;
    cin >> n >> m;

    vector<vector<pair<int, int>>> adj(n+1, vector<pair<int, int>>());

    for (int i = 0; i < m; i++)
    {
        int a, b, w;
        cin >> a >> b >> w;
        adj[a].push_back({b, w});
    }

    
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
    vector<long long> dist(adj.size(), LLONG_MAX);
    vector<int> visited(adj.size(), false);

    pq.push({0, 1});
    dist[1] = 0;

    while (!pq.empty()) {
        int u = pq.top().second;
        pq.pop();
        if (visited[u])
            continue;
        visited[u] = true;

        // Iterate through all adjacent vertices of the current vertex
        for (auto &neighbor : adj[u]) {
            int v = neighbor.first;
            int weight = neighbor.second;

            // If a shorter path to v is found
            if (!visited[v] && dist[v] > dist[u] + weight) {
                // Update distance and push new distance to the priority queue
                dist[v] = dist[u] + weight;
                pq.push({dist[v], v});
            }
        }
    }

    for (int i = 1; i < adj.size(); i++)
        cout << dist[i] << " ";
 

}