A necessary and sufficient condition for the inequality of generalized weighted means