Optimal control of finite-valued networks