The federal government is spending billions of dollars on bridges, tunnels and other infrastructure to route traffic over, under and around railroads tracks. But to many residents and local officials, that is an imperfect way to alleviate congestion on roads that are frequently blocked by freight trains.

To take advantage of the federal money, communities must find a way to cover a share of the cost of expensive upgrades. In addition, it can be difficult or impossible to build bridges and tunnels.

Some towns and cities have successfully worked with railroads to reschedule operations or move tracks away from busy roads. But many local officials complain that railroads are often unwilling to help, leaving communities with few options.

“Everybody loves trains and we appreciate the economic benefit of it, but we’re tired of being held hostage,” said Brad Rogers, a member of the Elkhart County Commission in Indiana.

A decade ago, when he was sheriff, Mr. Rogers dispatched deputies to issue tickets to Norfolk Southern crews whose trains were impeding traffic. The tickets helped draw attention to blocked crossings, and the congestion decreased for a time. But the railroad sued the state, and Indiana’s Supreme Court overturned the law that authorized local officials to fine railroads for blocking crossings.

The Association of American Railroads, which represents the major freight railroads, has said that its members work with local officials to alleviate crossing congestion when they can, but that the problem is complex and the result of years of limited public funding for infrastructure improvements.

“When the railroads started connecting the country, people put down roots and built communities beside them,” John Gray, a senior vice president at the association, said in a statement. “Railroads allowed the roads to cross the tracks utilizing grade crossings rather than grade separations, as was the norm in populated areas of most other developed regions of the world. The public entities, always eager to save a few dollars, readily agreed.”

Most states regulate blocked crossings, but courts have thrown out several of those laws, determining that only the federal government can enact and enforce such rules. Indiana and nearly 20 other states recently joined Ohio in asking the U.S. Supreme Court to determine that states can issues such regulations.

Congress provided about $3 billion in 2021 to help fund projects that would alleviate congestion at frequently blocked railroad crossings. In June, the Biden administration awarded the first round of grants from that fund, about $570 million, to make improvements at more than 400 crossings.

Houston will receive $37 million to build four underpasses and eliminate seven crossings. Pelham, Ala., near Birmingham, will get almost $42 million to build a bridge and eliminate two rail crossings along a road that divides the city. Olathe, Kan., near Kansas City, will receive about $18 million to build an overpass with a sidewalk that will allow children to get to school and connect cyclists and walkers to a trail system.

“What’s exciting about this moment is for the first time there’s specific, dedicated funding — and quite a lot of it — to address this,” the transportation secretary, Pete Buttigieg, said in an interview.

Even before Congress made this money available, some local governments had found ways to reduce the impact of trains blocking crossings. In Utah, community groups are building a pedestrian bridge that will cross three Union Pacific rail lines and two local transit lines near a high school in Salt Lake City. Congestion there frequently blocks roads, forcing some residents to squeeze through or crawl under trains.

But some communities cannot come up with the matching funds for bridge and tunnel projects or to pay for maintenance. In some areas, building overpasses or underpasses might not be practical.

So many communities have opted for cheaper solutions.

Officials in West Springfield and Agawam, in Massachusetts, sought federal funds to build a bridge over a rail crossing along a road that connects the towns, but did not win a grant. So officials are left to rely on signs with flashing lights that warn people when a train is crossing the road.

Those lights have helped but have increased congestion on other roads. And emergency medical workers are still forced to drive farther to avoid blocked crossings.

“We can’t even gauge the harm that it may have caused,” Mayor Bill Sapelli of Agawam said. “If they went around rather than going the shortest route and somebody didn’t make it, and it was a matter of minutes, that makes a difference.”

Mr. Rogers, the Indiana commissioner, recently visited a city that is using a system developed by Trainfo, a Canadian firm. The company uses acoustic sensors and software to identify oncoming and stopped trains. That information can be sent to road signs, emergency dispatchers or social media feeds.

“We wanted to solve this with the train companies, but that’s not apparently going to happen,” Mr. Rogers said. “So we’re trying to think outside of the box.”

Mark Walker contributed reporting.