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 ...
随机推荐
- vs2010 下使用C#开发activeX控件
1.创建一个类库 2.项目属性-应用程序-程序集信息-"使程序集COM可见"勾上; 3.项目属性-生成-"为COM互操作注册"勾上.(这个折腾一天,否则注册事件 ...
- JavaScript中什么是包装对象?
存取字符串.数字或布尔值的属性时,创建的临时对象称为包装对象.包装对象只是偶尔用来区分字符串值和字符串对象.数字和数值对象以及布尔值和布尔对象.由于字符串.数字和布尔值的属性都是只读的,并且不能给它们 ...
- mysql多表查询 查询排序
有 ask 问题表 和 answer回答表 回答表中的ask_id和 ask表中的id对应 1.查询 /*查询回答了的 */select a.id,a.title,count(b.ask_id) ...
- Patch 21352635 - Database Patch Set Update 11.2.0.4.8
一.CPU和PSU 近日,将数据库从9.2.0.6升级到11.2.0.4后,发现11.2.0.4通过DBLINK访问其他的9i库时发生ORA-02072错误,通过Google找到解决方案,即升级到PS ...
- java.net.URISyntaxException: Illegal character in query
java使用httpclient爬取一个网站的时候,请求:String url3="http://sh.58.com/ershoufang/33562546149042x.shtml?amp ...
- Python之NumPy实践之数组和矢量计算
Python之NumPy实践之数组和矢量计算 1. NumPy(Numerical Python)是高性能科学技术和数据分析的基础包. 2. NumPy的ndarray:一种对位数组对象.NumPy最 ...
- [转]STL之deque容器详解
Deque 容器 deque容器是C++标准模版库(STL,Standard Template Library)中的部分内容.deque容器类与vector类似,支持随机访问和快速插入删除,它在容器中 ...
- Singleton.java.ft not found 相关错误的解决办法
Entry fileTemplates//Singleton.java.ft not found in C:/Users/admin/Desktop/android-studio/lib/resour ...
- canves应用
canves用得好可以有好多效果: html:<canvas id="myCanvas" width="700" height="300&quo ...
- JS——arguments
1.只在函数中使用 2.返回的是实参的数组 <script> getNum(1, 2);//(2) [1, 2, callee: ƒ, Symbol(Symbol.iterator): ƒ ...