Media playback is unsupported on your device Media caption Rail electrification will 'transform communities', the prime minister says

A deal to electrify railway lines in the south Wales valleys will have a "huge" impact, the prime minister says.

The UK government will fund the upgrade of the Swansea-London mainline by 2018, and will pay almost half an estimated £500m to electrify the Valley lines.

The Welsh government expects to meet its share from cost savings and increased revenue.

The agreement settles a bitter dispute between Westminster and Cardiff Bay over who should foot the bill.

The UK government is also handing over control of the Welsh rail franchise to the Welsh government.

First Minister Carwyn Jones said he was "delighted" with the deal.

Electrification of the Valleys lines, due to follow the £850m upgrade of the mainline, was thrown into doubt in March by a row over the financing of the project.

On and off the rails

Why electrification matters: BBC Wales economics correspondent Sarah Dickins

Economics is the driver for why Wales needs electrification of its rail links.

Firstly, improved services along the Valleys lines will help many thousands of Welsh workers get to work more quickly - and most importantly - more reliably.

Several years ago the insurance group Admiral told me that rail links were affecting its business because employees found the rail service so unreliable.

The running of the franchise will go to the Welsh government, and it will join together transport and economic policy.

Then there's the main line between Swansea and London Paddington.

We are all too often reminded that the economy of London is so much more buoyant than the Welsh economy.

Cutting more than 20 minutes from the journey between the two capitals not only gives a perception that Wales is no longer peripheral, but for many business people it will make it easier for Welsh firms to tie up with others and bid for business.

Being about two hours by train from one of the biggest international centres in the world surely opens doors.

Similarly, it will make it easier to tempt investment westwards out of the congested south east of England.

'Transform communities'

Image caption The prime minister visits a Cardiff rail depot with Mark Langman of Network Rail

In a speech at the UK Investment Summit Wales in Newport, Prime Minister David Cameron said: "After years of neglect, this part of Wales will finally get the infrastructure it needs - faster, modern, more efficient trains.

"And I believe the impact will be huge. It will spread employment opportunities from Cardiff and beyond.

"It will transform communities throughout the valleys, and it'll help people from all parts of this great nation to get on."

It is estimated that electrifying the Valley lines will cut journey times from both Merthyr Tydfil and Treherbert to Cardiff from just over an hour to around 50 minutes.

First Minister Carwyn Jones said: "Together with an agreement to fully devolve power over the Welsh rail franchise this will allow the Welsh government to move forward with its ambitious plans to create the efficient and reliable rail service Wales needs and deserves."

Welsh Liberal Democrat transport spokesperson Eluned Parrott AM said the experience of passengers would now be "transformed".

Plaid Cymru transport spokesman Rhun ap Iorwerth said the deal was a "good step in the right direction" but called for rail infrastructure to be devolved to Wales as well as franchising.

Arriva Trains Wales, which runs the Valley lines, welcomed the news which it said would "modernise" rail infrastructure and trains.

Analysis by Nick Servini, BBC Wales political editor

So who blinked first?

It genuinely appears that the position of both sides moved with the cost - of around £500m - being split roughly in half.

A senior Welsh government official told me the main concession from Westminster was putting up £230m.

The Welsh government's share of the cost will partly be funded by the increased revenue from more passengers who are expected to use the new electrified routes.

The risk for ministers in Cardiff Bay is whether those extra passengers materialise.

At this stage no one side is claiming victory over the other, although the arrival of a new Welsh Secretary in the form of Stephen Crabb was clearly a major factor in breaking the deadlock.

Politically, both governments clearly thought it wasn't in their interests for the row to continue.