Asymptotics of the partition function for random matrices via Riemann-Hilbert techniques and applications to graphical enumeration