CSES - Datatähti 2023 alku - Results
Submission details
Task:Sadonkorjuu
Sender:Ihminen
Submission time:2022-12-05 21:22:40 +0200
Language:C++17
Status:COMPILE ERROR

Compiler report

input/code.cpp:22:5: error: 'vector' does not name a type
   22 |     vector<vector<Edge>> edges; // Adjacency list that stores the edges for each node
      |     ^~~~~~
input/code.cpp: In constructor 'Graph::Graph(int)':
input/code.cpp:25:24: error: class 'Graph' does not have any field named 'edges'
   25 |         Graph(int n) : edges(n) {}
      |                        ^~~~~
input/code.cpp: In member function 'void Graph::addEdge(int, int, int)':
input/code.cpp:30:9: error: 'edges' was not declared in this scope
   30 |         edges[u].push_back(Edge(v, cost));
      |         ^~~~~
input/code.cpp: In function 'void bfs(const Graph&, int*, int)':
input/code.cpp:40:19: error: 'const class Graph' has no member named 'edges'
   40 |     int n = graph.edges.size();
      |                   ^~~~~
input/code.cpp:41:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   41 |     for (int i = 0; i < n; i++)
      |     ^~~
input/code.cpp:45:9: note: ...this state...

Code

#include <iostream>
#include <deque>
#include <unordered_set>

using namespace std;

// Edge class that represents an edge in the graph
class Edge
{
public:
    int to; // The index of the node that this edge leads to
    int cost; // The cost of traveling along this edge

        // Constructor to initialize the edge
        Edge(int to, int cost) : to(to), cost(cost) {}
};

// Graph class that represents a graph with nodes and edges
class Graph
{
public:
    vector<vector<Edge>> edges; // Adjacency list that stores the edges for each node

        // Constructor to initialize the graph with n nodes
        Graph(int n) : edges(n) {}

    // Adds a new edge to the graph from node u to node v with the given cost
    void addEdge(int u, int v, int cost)
    {
        edges[u].push_back(Edge(v, cost));
        edges[v].push_back(Edge(u, cost));
    }
};

// A utility function to find the shortest distances from the source node to all other nodes in the graph
// using breadth-first search
void bfs(const Graph& graph, int* distances, int source)
{
    // Initialize distances to all nodes as infinite
    int n = graph.edges.size();
    for (int i = 0; i < n; i++)
        distances[i] = -1;

        // Create a deque to store the nodes that need to be processed
        deque<int> q;

    // Add the source node to the deque with distance 0
    distances[source] = 0;
    q.push_back(source);

    // Process nodes in the deque until it is empty
    while (!q.empty())
    {
        // Get the next node in the deque
        int u = q.front();
        q.pop_front();

        // Iterate through
            for (Edge edge : graph.edges[u])
            {
                // Calculate the new distance to the adjacent node
                int v = edge.to;
                int new_distance = distances[u] + edge.cost;

                    // If the new distance is smaller than the current distance, update the distance
                    if (distances[v] == -1 || new_distance < distances[v])
                    {
                        distances[v] = new_distance;
                        q.push_back(v);
                    }
            }
    }
}

int main()
{
    int n;
    // Read the number of cities
    cin >> n;
        // Read the type of each city
        vector<int> city_types(n);
    for (int& type : city_types)
        cin >> type;

    // Create a graph with n nodes
    Graph graph(n);

    // Read the roads and add them to the graph
    for (int i = 0; i < n - 1; i++)
    {
        int u, v, cost;
        cin >> u >> v >> cost;
        graph.addEdge(u - 1, v - 1, cost);
    }

    // Find the indices of the sunflower fields and ports
    unordered_set<int> sunflower_fields;
    unordered_set<int> ports;
    for (int i = 0; i < n; i++)
    {
        if (city_types[i] == 0)
            ports.insert(i);
        else if (city_types[i] == 1)
            sunflower_fields.insert(i);
    }

    // Calculate the shortest distance from each sunflower field to a port
    int total_distance = 0;
    for (int field : sunflower_fields)
    {
        // Calculate the shortest distances from the current sunflower field to all nodes in the graph
        int* distances = new int[n];
        bfs(graph, distances, field);

        // Find the port with the smallest distance to the sunflower field
        int min_distance = -1;
        for (int port : ports)
        {
            if (distances[port] != -1 && (min_distance == -1 || distances[port] < min_distance))
                min_distance = distances[port];
        }

        // Add the distance to the total distance
        total_distance += min_distance;

        // Free the memory used by the array
        delete[] distances;
    }

    // Print the total distance
    cout << total_distance << endl;

    return 0;
}