UVA 12661 Funny Car Racing

E - 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 long
14 #define pii pair<int,int>
15 #define INF 0x3f3f3f3f
16 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 }

时间: 2024-10-10 05:59:38

UVA 12661 Funny Car Racing的相关文章

[题解]UVa 12661 Funny Car Racing - spfa

很简单的一道最短路问题.分情况处理赛道的打开和关闭. Code 1 /** 2 * UVa 3 * Problem#12661 4 * Accepted 5 * Time:50ms 6 */ 7 #include<iostream> 8 #include<fstream> 9 #include<sstream> 10 #include<cstdio> 11 #include<cstdlib> 12 #include<cstring>

UvA 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 twointegers (a; b), that means the road will be open for a seconds, then closed for

UVa 12661 - Funny Car Racing(Dijkstra)

链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4399 题意: 在一个赛车比赛中,赛道有n(n≤300)个路口和m(m≤50000)条单向道路.有趣的是:每条路都是周期性关闭的.每条路用5个整数u, v, a, b, t表示(1≤u,v≤n,1≤a,b,t≤1e5),表示起点是u,终点是v,通过时间为t秒.另外,这条路会打开a秒,

UVa 12661 Funny Car Racing【 dijkstra 】

题意:给出n个点,m条路,每条路用5个整数表示u,v,a,b,t u表示这条路的起点,v表示终点,a表示打开时间,b表示关闭时间,t表示通过这条道路需要的时间 看的紫书,因为边权不再仅仅是路上的时间,还需要处理一下是否需要等待 如果不需要等待的话,这条路上的权值就为t 如果需要等待的话,这条路的权值就为t+wait 再用dijkstra就可以了 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #

UVa 12661 Funny Car Racing (dijkstra)

题意:给定一个有向图,每条路有5个整数修饰,u, v, a, b, t,表示起点为u,终点为v,打开时间a,关闭时间为b,通过时间为t,打开关闭是交替进行的, 问你从s到t最短时间是多少. 析:使用dijkstra算法,从每个结点出发,求最短路,并维护时间的最小值,这个可以用优先队列,然后考虑能不能通过这条路,如果t<a,可以在输入时处理. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include

UVA 12661 Funny Car Racing 有趣的赛车比赛(单源最短路,SSSP,变形)

题意:赛道有n个交叉点,和m条单向路径(有重边),每条路都是周期性关闭的,且通过仍需一段时间.在比赛开始时,所有道路刚好打开,选择进入该道路必须满足“在打开的时间段进入,在关闭之前出来”,即不可在路上逗留,但是可以在交叉点逗留.问到达终点的时间要多少? 思路:最短路,而且正权,用Dijkstra+优先队列够了.主要的难点在计算是否可以进入该路段,画图清晰点. 1 #include <bits/stdc++.h> 2 #define LL long long 3 #define pii pair

UVA - 12661 Funny Car Racing (Dijkstra算法)

题目: 思路: 把时间当做距离利用Dijkstra算法来做这个题. 前提:该结点e.c<=e.a,k = d[v]%(e.a+e.b); 当车在这个点的1处时,如果在第一个a这段时间内能够通过且更小的话,那时间就更新为d[e.to] = d[v]+e.a-k+e.c; 当车在这个点的1处时,如果在第一个a这段时间内不能通过,但等待之后再通过时间更短的话,那时间更新为d[e.to]=d[v]+e.a+e.b-k+e.to 如果在这个点的2处时,如果在等待之后通过的时间更短的话,时间更新和第二种情况

UVa 12661 (单源最短路) Funny Car Racing

题意: 有一个赛车跑道,可以看做一个加权有向图.每个跑道(有向边)还有一个特点就是,会周期性地打开a秒,然后关闭b秒.只有在赛车进入一直到出来,该跑道一直处于打开状态,赛车才能通过. 开始时所有跑道处于刚打开的状态,求从起点到终点的最短时间. 分析: 设d[i]为起点到节点i的最短时间. 和普通的单源最短路问题一样,只不过在进行松弛操作的时候分两种情况.松弛的前提是,赛道打开的时间不短于赛车通过的时间. 赛车从进入直到出跑道,一直是打开状态.则d[v] = min(d[v], d[u] + t)

UVA 12661(动态权值+最短路,dij)

题意:赛车背景,给你n个节点,m条边的图以及起点和终点:其中每条边的信息包括u(起点),v(终点),a(开启的时间),b(关闭的时间),d(通过这条道路的时间):求最短通过的时间,其中车在进的时候,保证时间足够能走出去:否则需要等一些分钟: 思路:dij真是万能的,把固定权值改成动态的即可: 其中改变权值的语句很关键,并且推出的规律有个前提就是保证道路打开的时间一定大于等于通过该条道路的时间,否则相当于道路不通,被我忽略了,WA了3遍,可惜: 1 #include <iostream> 2 #