Influence of optimal cavity shapes on the size of polymer molecules in random media