The best way to find a cheap fare is to book your ticket as far in advance as you can and to avoid traveling at rush hour.
Find all the dates and times for this journey
4 h 24 m
6 h 51 m
Trains per day
Of the 5 trains that leave Rome for Polignano a Mare every day 5 travel direct so it’s quite easy to avoid journeys where you’ll have to change along the way.
Here are some other resources that might have the information you need