The Fermat Principle in General Relativity and Applications