Memset gives WA!! Ciel and Tomya

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.

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

2 Likes

@prabhu_3095:Thank you

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

1 Like

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).

1 Like