Memset gives WA!! Ciel and Tomya

floyd-warshall
memset
wa

#1

Recently i was trying out this question http://www.codechef.com/problems/CIELTOMY .The question is basically asking us to calculate the number of shortest paths from source to destination in a directed graph.I have used Floyd Warshall method to calculate the number of shortest paths.But during initialising my adjacency matrix and shortest path count matrix(answer matrix),I was using memset which yielded me WA( http://www.codechef.com/viewsolution/7233655 ). When i replaced the memset with 2 for loop initialisations it gave me AC(http://www.codechef.com/viewsolution/7233667). Can anyone please explain me this behavior of memset function.


#2

memset can be used only to initialize with values 0 and -1.


#3

@prabhu_3095:Thank you


#4

And for STL containers, we can use fill() for initialization. See here. Hope this helps. :slight_smile:


#5

memset sets every 8 bits the value you pass. So for a 32 bit integer, it is filling it like :
00000001000000010000000100000001 : which is not 1
That’s why there is a mistake.
For zero it will work fine and for -1 too (Binary 11111111).