Chaitanya Gujarathi
Chaitanya Gujarathi

Reputation: 157

Got runtime error while using BFS algorithm

There are n cities connected by m flights. Each flight starts from city u and arrives at v with a price w.

Now given all the cities and flights, together with starting city src and the destination dst, your task is to find the cheapest price from src to dst with up to k stops. If there is no such route, output -1.

Ex:

Example 1:

Input:

n = 3, 

edges = [[0,1,100],[1,2,100],[0,2,500]]


src = 0, dst = 2, k = 1

Output: 200 Explanation: The graph looks like this:

enter image description here

Here is my code:

class Solution {
public:
    int ans=INT_MAX;
    int findCheapestPrice(int n, vector<vector<int>>& flights, int src, int dst, int K) {
        vector<vector<vector<int>>>g;
        for(auto f:flights)
        {
            int from=f[0];
            int to=f[1];
            int cost=f[2];
            g[from].push_back({to,cost});
        }
        queue<vector<int>>q;
        q.push({src,0,-1});
        while(!q.empty())
        {
             vector<int>curr=q.front();
            q.pop();
            int currCity=curr[0];
            int currCost=curr[1];
            int currK=curr[2];
            
            if(currCity == dst)
            {
                ans=min(currCost,ans);
                continue;
            }
            for(auto x:g[currCity])
            {
                if(currK+1<=K && currCost+x[1]<ans)
                {
                    q.push({x[0],currCost+x[1],currK+1});
                }
            }
            
        }
        if(ans == INT_MAX)
        {
            return -1;
        }
        return ans;
    }
};

I had use the BFS algorithm.

However I got the following error:

Line 924: Char 9: runtime error: reference binding to null pointer of type 'std::vector<std::vector<int, std::allocator >, std::allocator<std::vector<int, std::allocator > > >' (stl_vector.h) SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/stl_vector.h:933:9

I am not able to find out where I went wrong.

Thanks.

Upvotes: 0

Views: 99

Answers (2)

Dmitry Kuzminov
Dmitry Kuzminov

Reputation: 6584

Look into this code:

        vector<vector<vector<int>>>g;
        for(auto f:flights)
        {
            int from=f[0];
            int to=f[1];
            int cost=f[2];
            g[from].push_back({to,cost});
        }

Initially g is an empty vector. The first thing you are doing with it is accessing the non-existing element: g[from].

What you probably meant is:

vector<vector<vector<int>>>g(n);

Here you create a 3D vector with the first dimention properly initialized.

Other considerations: you use vectors where that is not needed. The fact that you are using known fixed number of elements without checking actual size means that the vector is misused:

            int from=f[0];
            int to=f[1];
            int cost=f[2];

Try to avoid that by using a struct, tuple, etc. The struct is more appropriate because you even know the role of each element: from, to, cost.

This code is very inefficient:

for(auto x:g[currCity])
    ...

As long as g is a 3D vector, auto x becomes a full copy of each 2D element. Try that instead: for(const auto &x:g[currCity]).

Upvotes: 0

Nilesh Solanki
Nilesh Solanki

Reputation: 356

vector<vector<vector<int>>>g; should be `vector<vector<vector<int>>>g(n);` 

where n can be any number. because you trying to get specific index. you have to intialisze your vector.

Upvotes: 0

Related Questions