1333:funny Car Racing Time limit: 1 Sec Memory Limit: MB
Submit: 482 Solved: 116
[Submit] [Status] [Web Board] 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 was associate with a integers (a, b), that means the road would be an open for a seconds , then closed for b seconds and 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 the can wait at the a junction even if all its adjacent roads is closed.
Input
There'll is at the most 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), which means there is A road starting from junction u ending with Junction v. It's open for a seconds, then closed to 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 is connected by more than one road.
Output
For each test case, print the shortest time, in seconds. It's always possible-arrive at t from S.
Sample Input
3 2 1 31 2 5 6 32 3 7 7 63 2 1 31 2 5 6 32 3 9 5 6
Sample Output
Case 1:20case 2:9
Hintsource
The nineth session of Hunan Province College students computer Program design Contest
#include <stdio.h> #include <string.h> #include <queue>using namespace std; #define LL Long Longconst ll INF = 1ll<<60; Const LL N = 305; struct edg{ll to, next; ll A, B, C, D;} Edg[50010];ll Eid, Head[n]; ll dis[n]; void Init () {eid = 0; Memset (Head,-1, sizeof (head)); for (ll i = 0; i < N; i++) dis[i] = INF;} void Addedg (ll u, ll V, ll A, ll B, ll c) {edg[eid].to = v; Edg[eid].next = Head[u]; EDG[EID].A = A; edg[eid].b = b; EDG[EID].C = C; EDG[EID].D = a + b; Head[u] = eid++;} void Spfa (ll s, ll T) {queue<ll>q; BOOL Inq[n] = {0}; ll u, v; Dis[s] = 0; if (s! = t) Q.push (s); while (!q.empty ()) {u = Q.front (); Q.pop (); Inq[u] = 0; for (ll i = head[u]; i!=-1; i=edg[i].next) {v = edg[i].to; ll tt = EDG[I].A-(DIS[U]%EDG[I].D); if (TT<EDG[I].C) TT + = edg[i].b; ELSE TT = 0; if (Dis[v] > Dis[u] + tt +edg[i].c) {Dis[v] = Dis[u] + TT +edg[i].c; if (inq[v]==0&&v!=t) inq[v] = 1, Q.push (v); }}}}int Main () {ll n, m, S, T, U, V, a, B, C; ll T = 0; while (scanf ("%lld%lld%lld%lld", &n,&m,&s,&t) >0) {init (); while (m--) {scanf ("%lld%lld%lld%lld%lld", &u, &v, &a, &b, &c); if (c<=a) addedg (U, V, a, B, c); } SPFA (S, t); if (dis[t]==inf) dis[t] =-1; printf ("Case%LLD:%lld\n", ++t,dis[t]); }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
CSU 1333:funny Car Racing (SPFA) 13 province race question