An exact algorithm with linear complexity for a problem of visiting megalopolises