rvcam
rvcam

Reputation: 157

Segmentation fault on size of vector

I am trying a simple Dijkstra problem and I chose to represent my adjacency list as an array of vectors, each one containing a pair of (vertex, distance). I declared it this way: vector<pair<int, int> > G[MAXV]; The problem is, when I try to get the number of edges connected to a given vertex (i.e., the size of a vector) I get a segmentation fault. This is the line where the fault occurs: vectorSize=G[currentVertex-1].size(); I dont think the problem is currentVertex because I already changed the argument between brackets to 0 (that is, the first vector) and I still get the seg fault. Thanks for all suggestions. Here is the complete source code:

#include <cstdio>
#include <vector>
#include <queue>
#include <limits>
#include <utility>
#define MAXV 10000
#define infinity std::numeric_limits<int>::max()

using namespace std;

int main()
{
    vector<pair<int, int> > G[MAXV];
    int numCases;
    int a, b, c;
    int A, B;
    int V, K;
    bool vis[MAXV];
    pair<int, int> temp;
    pair<int, int> vertexAndDistance[MAXV];
    priority_queue<pair<int, int>, vector< pair<int, int> >, greater<pair<int, int> > > heap;
    pair<int, int> top;
    int currentVertex;
    int currentDistance;
    int vectorSize;

    for (int i=0; i<MAXV; i++)
    {
        vertexAndDistance[i].second=i+1;
    }

    scanf(" %d", &numCases);
    for (int i=0; i<numCases; i++)
    {
        scanf(" %d %d", &V, &K);
        for (int j=0; j<K; j++)
        {
            scanf("%d %d %d", &a, &b, &c);
            temp.first=c;
            temp.second=b;
            G[a-1].push_back(temp);
        }
        scanf ("%d %d", &A, &B);

        for (int k=0; k<V; k++)
            vis[i]=false;
        for (int k=0; k<V; k++)
            vertexAndDistance[k].first=infinity;
        vertexAndDistance[A-1].first=0;
        heap.push(vertexAndDistance[A-1]);

        while(true)
        {
            top = heap.top();
            currentDistance = top.first;
            currentVertex = top.second;
            heap.pop();
            if (infinity == currentDistance || B==currentVertex) break;
//            vis[currentVertex-1]=true;
            vectorSize=G[currentVertex-1].size();
            for (unsigned int k=0;!heap.empty() && k<vectorSize; k++)
//            tr (G[currentVertex], it)
            {
                if (vertexAndDistance[G[currentVertex][k].second-1].first > vertexAndDistance[A-1].first + G[currentVertex][k].first)
                {
                    vertexAndDistance[G[currentVertex][k].second-1].first = vertexAndDistance[A-1].first + G[currentVertex][k].first;
                    heap.push(vertexAndDistance[G[currentVertex][k].second]);
                }
            }
        }
        if (infinity > vertexAndDistance[B-1].first)
            printf("%d", vertexAndDistance[B-1].first);
        else
            printf("NO");
    }
return 0;
}

Upvotes: 0

Views: 4078

Answers (1)

Ice
Ice

Reputation: 31

Usually when you have a seg fault, it is because you try to access to the wrong memory zone. It can happen, in you case, if G[currentVertex-1] does not exist (currentVertex > max or ==0) or if heap.top return a wrong value (maybe NULL)

Upvotes: 1

Related Questions