首页 > 代码库 > UVA 12661 Funny Car Racing
UVA 12661 Funny Car Racing
Time Limit:1000MS Memory Limit:0KB 64bit 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. The ?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 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
解题: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 }
UVA 12661 Funny Car Racing