Generalized $M$-matrices and applications