質問

is it possible to create a dynamic array with all entries in it equal to 0?

 int** adjMatrix;
 adjMatrix= new int*[numOfVertices];
for(int i=0; i<numOfVertices; i++){
    adjMatrix[i]=new int[numOfVertices];
}

Also, I was asked to find the longest path in a graph. I was thinking about using Dijsktra's algorithm after multiplying all the weights with -1 and run the program in normal way. And then I'll multiply with -1 again and get the longest path. I think this should give me the longest path, do you think it would work? And also, I'm dealing with considerably big data, such as 1.000.000 nodes etc. and I have a time limit of 2 seconds and memory limit of 128mb. Can you suggest any other data structure instead of Adjacency Matrix? Because I'm pretty sure it will exceed the limits. Any help would be much appreciated.

役に立ちましたか?

解決

is it possible to create a dynamic array with all entries in it equal to 0?

Use value initialization:

for(int i=0; i<numOfVertices; i++){
    adjMatrix[i]=new int[numOfVertices]();
    //                                 ^^
}

他のヒント

The fastest way would be to use memset, but only if the array is contiguous in memory. So, for this to work you should initially allocate a 1D array.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top