Can't stop this integer overflow from giving me a negative number
I have a formula below which gives me weights to every other vertex so that i can use dijkstra's algorithm to find the shortest path. The problem is that when i do the calculation some overflow and create negative numbers. I was told if i mod each step it would stop the overflow vs mod the entire value , however neither are working. I'v tried starting with a long then casting to a int still get negatives. If someone could help me that be great!
public void setweights()
for(int row=0; row < 20 ;row++ )
for( int col=0 ; col < 20; col++)