Ultraviolet A-12661-funny car racing

Source: Internet
Author: User

Start with the question:

12661 funny car racing
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
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,,
B, T (1 ≤ u, v ≤ n, 1 ≤ a, B, T ≤ 105
), That means there is a road starting from junction U ending
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 cocould 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

A directed graph is given. Each edge has three values. The time interval is switched on, the time interval is closed, and the time required by the edge is cyclical. Let's give you the start and end points and ask you at least how long it will take to reach the end point from the start point (make sure this path exists ).

This is actually to find the shortest short circuit, use dij to find the shortest short circuit, and then analyze whether to update the next point that can be reached from a certain point, you need to judge whether the current time is open. If you open it, you can check if it is not enough time, and then update the minimum value based on these situations. Note that directed graphs are not undirected graphs.

 

Code:

 

 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define MAX 50002 5 #define ll long long 6 #define INF (((ll)1)<<60) 7 using namespace std; 8  9 typedef struct edge{10     int to,next;11     ll a,b,c,s;12 }edge;13 14 edge e[MAX<<1];15 int p[302],tot;16 int n,m,st,ed;17 ll dis[MAX];18 19 void dij(){20     bool f[302]={0};21     for(int i=1;i<=n;i++){22         dis[i]=INF;23     }24     dis[st]=0;25     for(int i=0;i<n;i++){26         int u;27         ll dd=INF;28         for(int j=1;j<=n;j++){29             if(!f[j] && dd>dis[j]) {30                 dd=dis[j];31                 u=j;32             }33         }34         if(dd==INF) break;35         f[u]=1;36         for(int j=p[u];j!=-1;j=e[j].next){37             ll t=dis[u];38             ll r=t%e[j].s;39             if(r>=e[j].a){40                 t+=(e[j].s-r)+e[j].c;41             }else{42                 if(e[j].a-r>=e[j].c) t+=e[j].c;43                 else t+=e[j].s-r+e[j].c;44             }45             dis[e[j].to]=min(t,dis[e[j].to]);46         }47     }48 }49 50 inline void add(int u,int v,int a,int b,int c){51     e[tot].to=v;    e[tot].next=p[u];52     e[tot].a=a;     e[tot].b=b;     e[tot].c=c;     e[tot].s=a+b;53     p[u]=tot++;54 }55 56 int main()57 {58     int t,u,v,a,b,c;59     //freopen("data.txt","r",stdin);60     t=1;61     while(scanf("%d %d %d %d",&n,&m,&st,&ed)!=EOF){62         memset(p,-1,sizeof(p));63         tot=0;64         for(int i=0;i<m;i++){65             scanf("%d %d %d %d %d",&u,&v,&a,&b,&c);66             if(c<=a){67                 add(u,v,a,b,c);68                 //add(v,u,a,b,c);69             }70         }71         dij();72         printf("Case %d: %lld\n",t++,dis[ed]);73 74     }75     return 0;76 }
/X 12661 */

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.