Va 12661 funny car racing

Source: Internet
Author: User

E-funny car racing

Time limit:1000 msMemory limit:0 KB64bit Io format:% LLD & % LlU

 

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.? RST 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? Ve 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 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

 

Problem solving: Dijkstra...

 

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 const int maxn = 310;18 struct arc{19     int to,a,b,t,next;20     arc(int o = 0,int aa = 0,int bb = 0,int tt = 0,int z = -1){21         to = o;22         a = aa;23         b = bb;24         t = tt;25         next = z;26     }27 };28 arc e[51000];29 int head[maxn],tot,d[maxn],n;30 bool vis[maxn];31 void add(int u,int v,int a,int b,int t){32     e[tot] = arc(v,a,b,t,head[u]);33     head[u] = tot++;34 }35 priority_queue< pii,vector< pii >,greater< pii > >q;36 void dijkstra(int s){37     for(int i = 0; i <= n; ++i){38         d[i] = INF;39         vis[i] = false;40     }41     while(!q.empty()) q.pop();42     d[s] = 0;43     q.push(make_pair(d[s],s));44     while(!q.empty()){45         int u = q.top().second;46         q.pop();47         if(vis[u]) continue;48         vis[u] = true;49         for(int i = head[u]; ~i; i = e[i].next){50             int res = d[u]%(e[i].a + e[i].b);51             if(e[i].a - res >= e[i].t && d[e[i].to] > d[u] + e[i].t){52                 d[e[i].to] = d[u] + e[i].t;53                 q.push(make_pair(d[e[i].to],e[i].to));54             }else if(e[i].t <= e[i].a && d[e[i].to] > d[u] + e[i].t + e[i].a + e[i].b - res){55                 d[e[i].to] = d[u] + e[i].t + e[i].a + e[i].b - res;56                 q.push(make_pair(d[e[i].to],e[i].to));57             }58         }59     }60 61 }62 int main() {63     int m,s,t,a,b,u,v,w,cs = 1;64     while(~scanf("%d %d %d %d",&n,&m,&s,&t)){65         memset(head,-1,sizeof(head));66         for(int i = tot = 0; i < m; i++){67             scanf("%d %d %d %d %d",&u,&v,&a,&b,&w);68             add(u,v,a,b,w);69         }70         dijkstra(s);71         printf("Case %d: %d\n",cs++,d[t]);72     }73     return 0;74 }
View code

 

Va 12661 funny car racing

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.