Optimal Hardy-type inequalities for elliptic operators