UVa - 12661 - Funny Car Racing
先上题目:
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 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 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
), 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
题意:给出一个有向图,每条边都有三个值,打开时间间隔,关闭时间间隔,通过它需要多少时间,路的开关是循环往复的。给你起点和终点,问你最少需要多少时间才可以从起点到达终点(保证一定存在这种路)。
这其实是求最短路,用DIJ求最短路,然后对于从某个点开始可以到达的下一个点,在分析是否需要更新的时候,需要判断当前时刻路开了没有,如果开了看看够不够时间过去,然后根据这些情况更新最小值就可以了。这里需要注意的东西就是这事有向图不是无向图。
上代码:
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- #define MAX 50002
- #define ll long long
- #define INF (((ll)1)<<60)
- using namespace std;
- typedef struct edge{
- int to,next;
- ll a,b,c,s;
- }edge;
- edge e[MAX<<];
- int p[],tot;
- int n,m,st,ed;
- ll dis[MAX];
- void dij(){
- bool f[]={};
- for(int i=;i<=n;i++){
- dis[i]=INF;
- }
- dis[st]=;
- for(int i=;i<n;i++){
- int u;
- ll dd=INF;
- for(int j=;j<=n;j++){
- if(!f[j] && dd>dis[j]) {
- dd=dis[j];
- u=j;
- }
- }
- if(dd==INF) break;
- f[u]=;
- for(int j=p[u];j!=-;j=e[j].next){
- ll t=dis[u];
- ll r=t%e[j].s;
- if(r>=e[j].a){
- t+=(e[j].s-r)+e[j].c;
- }else{
- if(e[j].a-r>=e[j].c) t+=e[j].c;
- else t+=e[j].s-r+e[j].c;
- }
- dis[e[j].to]=min(t,dis[e[j].to]);
- }
- }
- }
- inline void add(int u,int v,int a,int b,int c){
- e[tot].to=v; e[tot].next=p[u];
- e[tot].a=a; e[tot].b=b; e[tot].c=c; e[tot].s=a+b;
- p[u]=tot++;
- }
- int main()
- {
- int t,u,v,a,b,c;
- //freopen("data.txt","r",stdin);
- t=;
- while(scanf("%d %d %d %d",&n,&m,&st,&ed)!=EOF){
- memset(p,-,sizeof(p));
- tot=;
- for(int i=;i<m;i++){
- scanf("%d %d %d %d %d",&u,&v,&a,&b,&c);
- if(c<=a){
- add(u,v,a,b,c);
- //add(v,u,a,b,c);
- }
- }
- dij();
- printf("Case %d: %lld\n",t++,dis[ed]);
- }
- return ;
- }
/*12661*/
UVa - 12661 - Funny Car Racing的更多相关文章
- UVa 12661 Funny Car Racing - spfa
很简单的一道最短路问题.分情况处理赛道的打开和关闭. Code /** * UVa * Problem#12661 * Accepted * Time:50ms */ #include<iost ...
- UVa 12661 - Funny Car Racing(Dijkstra)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 12661 Funny Car Racing 有趣的赛车比赛(最短路,变形)
题意:赛道有n个交叉点,和m条单向路径(有重边),每条路都是周期性关闭的,且通过仍需一段时间.在比赛开始时,所有道路刚好打开,选择进入该道路必须满足“在打开的时间段进入,在关闭之前出来”,即不可在路上 ...
- UVa 12661 Funny Car Racing (dijkstra)
题意:给定一个有向图,每条路有5个整数修饰,u, v, a, b, t,表示起点为u,终点为v,打开时间a,关闭时间为b,通过时间为t,打开关闭是交替进行的, 问你从s到t最短时间是多少. 析:使用d ...
- UVA - 12661 Funny Car Racing (Dijkstra算法)
题目: 思路: 把时间当做距离利用Dijkstra算法来做这个题. 前提:该结点e.c<=e.a,k = d[v]%(e.a+e.b); 当车在这个点的1处时,如果在第一个a这段时间内能够通过且 ...
- UVa 12661 Funny Car Racing【 dijkstra 】
题意:给出n个点,m条路,每条路用5个整数表示u,v,a,b,t u表示这条路的起点,v表示终点,a表示打开时间,b表示关闭时间,t表示通过这条道路需要的时间 看的紫书,因为边权不再仅仅是路上的时间, ...
- UVa 12661 (单源最短路) Funny Car Racing
题意: 有一个赛车跑道,可以看做一个加权有向图.每个跑道(有向边)还有一个特点就是,会周期性地打开a秒,然后关闭b秒.只有在赛车进入一直到出来,该跑道一直处于打开状态,赛车才能通过. 开始时所有跑道处 ...
- UVA 12661(动态权值+最短路,dij)
题意:赛车背景,给你n个节点,m条边的图以及起点和终点:其中每条边的信息包括u(起点),v(终点),a(开启的时间),b(关闭的时间),d(通过这条道路的时间):求最短通过的时间,其中车在进的时候,保 ...
- 紫书 例题11-11 UVa 12661 (dihkstra变形)
这道题主要比较权值的时候要改变一下,其他地方基本一样. 比较权值的时候要考虑边的时间与a, b 可以设相对于当前边的时间now, 则now = d[u] % (a+b), 也就是当前这个边进行到整个a ...
随机推荐
- bzoj2242 [SDOI2011]计算器——BSGS
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2242 第一次写BSGS,参考了好多好多博客: 然而看到的讲解和模板是一种写法,这道题的网上题 ...
- HIT1917Peaceful Commission(2-SAT)
Peaceful Commission Source : POI 2001 Time limit : 10 sec Memory limit : 32 M Submitted : 2112 ...
- [Swift通天遁地]六、智能布局-(1)给视图添加尺寸和中心点的约束
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- SpringBoot集成Swagger2 以及汉化 快速教程
(一) Swagger介绍 Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件 (二)为什么使用Swagger 在现在的开发过程中还有很大一部分公司都是以口口相传的方式来进行 ...
- 51nod 1222 莫比乌斯反演
思路: yhx找的反演题 题解已经烂大街了 #pragma GCC optimize("O3") //By SiriusRen #include <bits/stdc++.h ...
- day03_12/13/2016_bean的管理之依赖注入
- ORACLE SEQUENCE用法(转)
ORACLE SEQUENCE用法 在oracle中sequence就是序号,每次取的时候它会自动增加.sequence与表没有关系. 1.Create Sequence 首先要有CREATE ...
- 更新换代之requests库
好久不更新博客了... 之前的博文都是通过urllib2进行http访问,接下来我要说一个利器啊!requests模块,无法用语言对他进行赞扬了,需要的,有兴趣的,可以去了解下,移步官方中文文档: R ...
- C#入门经典 Chapter4 流程控制
4.1布尔逻辑 布尔比较运算符 == != < > <= >= 处理布尔值的布尔值运算符 ! & | ^(异或) 条件布尔运算符 &&am ...
- iOS UIWebView 访问https绕过证书验证的方法
@implementation NSURLRequest (NSURLRequestWithIgnoreSSL) + (BOOL)allowsAnyHTTPSCertificateForHost:(N ...