Transfer from http://aikilis.tk/
If the scope of the data in the problem is clear, then the infinite setting is not a problem, in the ambiguous case, many programmers take 0x7fffffff as infinity, because this is the maximum value of 32-bit int. If this infinity is used only for general comparisons (such as the initial value of the min variable at the minimum), then 0x7fffffff is a perfect choice, but in more cases, 0x7fffffff is not a good choice. Most of the time we do not simply compare the infinity, but we will do the comparison after the operation, for example, in most of the shortest path algorithm will be used in the relaxation operation:
if (D[u]+w[u][v]<d[v]) d[v]=d[u]+w[u][v];
We know that if there is no edge between u,v, then W[u][v]=inf, if our INF takes 0x7fffffff, then d[u]+w[u][v] will overflow and become negative, our relaxation operation is wrong, more generally speaking, 0x7fffffff can not meet the " Infinity plus a poor number is still infinite ", it becomes a very small negative. In addition to being satisfied plus a constant is still infinity, our constants should also meet "infinity plus infinity is still infinite", at least two infinite sum should not be catastrophic error, this point 0x7fffffff still can not satisfy us.
So we need a better guy to replace 0x7fffffff, the most rigorous way of course is to deal with Infinity in particular rather than find a big big constant to replace it (or simulate it), but this will make our programming process cumbersome. In the code I've read, the most ingenious infinity constant value is 0x3f3f3f3f, I don't know who first started using this subtle constant to do infinity, but I did learn from a blog that I didn't know about Acmer (Id:staginner). This constant was used in many of her code, so I tried it myself and found it very useful, and when I did a more in-depth analysis of the constant, I found it was really ingenious. 0x3f3f3f3f decimal is 1061109567, that is, 10^9 level (and 0x7fffffff an order of magnitude), and the general situation of the data is less than 10^9, so it can be used as infinity without the case of data greater than infinity. On the other hand, since the general data is not greater than 10^9, when we add infinity to a data, it does not overflow (this satisfies the "infinity plus a poor number is still infinite"), in fact 0x3f3f3f3f+0x3f3f3f3f= 2122219134, this is very large but not more than the 32-bit int range, so 0x3f3f3f3f also meet our "Infinity plus infinity or infinity" demand. Finally, 0X3F3F3F3F can bring us an unexpected additional benefit: if we want to clear an array, we usually use code like memset (A,0,sizeof (a)) to do it (convenient and efficient), But when we want to assign an array to infinity (for example, to initialize the adjacency matrix when solving graph theory problems), you can't use the memset function to write loops (it's really painful to write these unimportant code), and we know that because Memset is in bytes, It can clear the array 0 because 0 of each byte is 0, now well, if we set infinity as 0x3f3f3f3f, then the miracle happened, 0x3f3f3f3f each byte is 0x3f. So to set a memory to infinity, we only need memset (A,0x3f,sizeof (a)).
So in the usual situation, 0x3f3f3f3f really is a great choice.