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
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;
}