Scale-free networks from a Hamiltonian dynamics