UVa 12661 Funny Car Racing

本文介绍了一种解决特殊场景下最短路径问题的方法:在存在周期性开放关闭的道路网络中寻找从起点到终点的最短行驶时间。通过SPFA算法进行改进,在考虑道路周期性的基础上实现了有效的路径搜索。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

There is a funny car racing in a city with n junctions and m directed roads.
The funny part is: each road is open and closed periodically. Each road is associate with two
integers (a, b), that means the road will be open for a seconds, then closed for b seconds, then open for
a seconds. . . All these start from the beginning of the race. You must enter a road when it’s open, and
leave it before it’s closed again.
Your goal is to drive from junction s and arrive at junction t as early as possible. Note that you
can wait at a junction even if all its adjacent roads are closed.
Input
There will be at most 30 test cases. The first line of each case contains four integers n, m, s, t
(1 ≤ n ≤ 300, 1 ≤ m ≤ 50, 000, 1 ≤ s, t ≤ n). Each of the next m lines contains five integers u, v, a,
b, t (1 ≤ u, v ≤ n, 1 ≤ a, b, t ≤ 105
), that means there is a road starting from junction u ending with
junction v. It’s open for a seconds, then closed for b seconds (and so on). The time needed to pass this
road, by your car, is t. No road connects the same junction, but a pair of junctions could be connected
by more than one road.
Output
For each test case, print the shortest time, in seconds. It’s always possible to arrive at t from s.
Sample Input
3 2 1 3
1 2 5 6 3
2 3 7 7 6
3 2 1 3
1 2 5 6 3
2 3 9 5 6
Sample Output
Case 1: 20

Case 2: 9

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

最短路~

在每次更新最短路的时候判断一下应该用来更新的时间,然后就是正常的SPFA了~

加边之前一定要先判断这条边是否能用,否则会WA得很惨!debug上的数据根本测不出这个错!!!


#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
using namespace std;

int n,m,st,ed,x,y,fi[301],ne[50001],w[50001],v[50001],aa[50001],bb[50001],cnt,dis[301],totnumans,aaa,bbb,vvv;
bool b[301];

int findd()
{
	memset(dis,127,sizeof(dis));
	queue<int> q;q.push(st);b[st]=1;dis[st]=0;
	while(!q.empty())
	{
		int k=q.front();q.pop();b[k]=0;
		for(int i=fi[k];i;i=ne[i])
		{
			int kkz=dis[k]%(aa[i]+bb[i]);
			if(kkz+v[i]<=aa[i])
			{
				if(dis[k]+v[i]<dis[w[i]])
				{
					dis[w[i]]=dis[k]+v[i];
					if(!b[w[i]])
					{
						q.push(w[i]);b[w[i]]=1;
					}
				}
			}
			else
			{
				kkz=(dis[k]/(aa[i]+bb[i])+1)*(aa[i]+bb[i])+v[i];
				if(kkz<dis[w[i]])
				{
					dis[w[i]]=kkz;
					if(!b[w[i]])
					{
						q.push(w[i]);b[w[i]]=1;
					}
				}
			}
		}
	}
	return dis[ed];
}

int main()
{
	while(scanf("%d%d%d%d",&n,&m,&st,&ed)!=EOF)
	{
		cnt=0;
		memset(fi,0,sizeof(fi));
		for(int i=1;i<=m;i++)
		{
			scanf("%d%d%d%d%d",&x,&y,&aaa,&bbb,&vvv);
			if(vvv<=aaa)
			{
				aa[++cnt]=aaa;bb[cnt]=bbb;v[cnt]=vvv;w[cnt]=y;ne[cnt]=fi[x];fi[x]=cnt;
			}
		}
		printf("Case %d: %d\n",++totnumans,findd());
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值