Thailand and Cambodia have signed a ceasefire agreement after weeks of deadly border clashes, following three days of talks.