Problem Description
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<=10^5), 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
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<=10^5), 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最短路径算法就A了。
#include<stdio.h>
#include<string.h>
#define INF 1<<30
struct edge
{
int last,v,a,b,t;
}edge[50010];
int node[310];
int que[50010];
int m,n,cnt_edge,st,ed;
int u,v,a,b,t;
void init()
{
cnt_edge=0;
memset(node,-1,sizeof(node));
}
void add_edge(int u,int v,int a,int b,int t)
{
edge[cnt_edge].last=node[u];
edge[cnt_edge].v=v;
edge[cnt_edge].a=a;
edge[cnt_edge].b=b;
edge[cnt_edge].t=t;
node[u]=cnt_edge++;
}
int SPFA(int x)
{
int i,now,next;
int vis[310],d[310];
memset(vis,0,sizeof(vis));
memset(que,0,sizeof(que));
int front = 0, rear = 1;
for(i=1;i<=m;i++) d[i] = INF;
que[front] = x;
vis[x] = true;
d[x] = 0;
while(front<rear)
{
int now=que[front];
vis[now] = false;
for(i=node[now];i!=-1;i=edge[i].last)
{
next=edge[i].v;
int sum=0;
int x=d[now]%(edge[i].a+edge[i].b);
if(x>=0&&x<edge[i].a)
{
if(edge[i].a-x<edge[i].t)//如果剩下的实践不够过桥,等待桥重新开启
sum=(edge[i].a-x)+edge[i].b+edge[i].t;
else sum=edge[i].t;//否则直接通过
}
else sum=(edge[i].a+edge[i].b-x+edge[i].t);
if (d[next]>d[now]+ sum)
{
d[next]= d[now]+sum;
if( !vis[next] )
{
vis[next] = true;
que[rear++] = next;
}
}
}
front++;
}
return d[ed];
}
/*void dfs(int now,int tot)
{
//print f("%d\n",tot);
int i,next,j,k,sum;
if(now==ed)
{
if(tot<minn) minn=tot;
return;
}
for(i=node[now];i!=-1;i=edge[i].last)
{
next=edge[i].v;
sum=0;
int x=tot%(edge[i].a+edge[i].b);
if(x>=0&&x<edge[i].a)
{
if(edge[i].a-x<edge[i].t)
sum=(edge[i].a-x)+edge[i].b+edge[i].t;
else sum=edge[i].t;
}
else sum=(edge[i].a+edge[i].b-x+edge[i].t);
dfs(next,tot+sum);
}
}*/
int main()
{
//freopen("b.txt","r",stdin);
int i,a,b,cas=0;
while(scanf("%d %d %d %d",&m,&n,&st,&ed)==4)
{
init();
for(int i=1;i<=n;i++)
{
scanf("%d %d %d %d %d",&u,&v,&a,&b,&t);
//print f("%d %d %d %d %d\n",u,v,a,b,t);
add_edge(u,v,a,b,t);//建立邻接表
}
//dfs(st,0);
printf("Case %d: %d\n",++cas,SPFA(st));
}
return 0;
}