先上题目:

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的更多相关文章

  1. UVa 12661 Funny Car Racing - spfa

    很简单的一道最短路问题.分情况处理赛道的打开和关闭. Code /** * UVa * Problem#12661 * Accepted * Time:50ms */ #include<iost ...

  2. UVa 12661 - Funny Car Racing(Dijkstra)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  3. UVA 12661 Funny Car Racing 有趣的赛车比赛(最短路,变形)

    题意:赛道有n个交叉点,和m条单向路径(有重边),每条路都是周期性关闭的,且通过仍需一段时间.在比赛开始时,所有道路刚好打开,选择进入该道路必须满足“在打开的时间段进入,在关闭之前出来”,即不可在路上 ...

  4. UVa 12661 Funny Car Racing (dijkstra)

    题意:给定一个有向图,每条路有5个整数修饰,u, v, a, b, t,表示起点为u,终点为v,打开时间a,关闭时间为b,通过时间为t,打开关闭是交替进行的, 问你从s到t最短时间是多少. 析:使用d ...

  5. UVA - 12661 Funny Car Racing (Dijkstra算法)

    题目: 思路: 把时间当做距离利用Dijkstra算法来做这个题. 前提:该结点e.c<=e.a,k = d[v]%(e.a+e.b); 当车在这个点的1处时,如果在第一个a这段时间内能够通过且 ...

  6. UVa 12661 Funny Car Racing【 dijkstra 】

    题意:给出n个点,m条路,每条路用5个整数表示u,v,a,b,t u表示这条路的起点,v表示终点,a表示打开时间,b表示关闭时间,t表示通过这条道路需要的时间 看的紫书,因为边权不再仅仅是路上的时间, ...

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

    题意: 有一个赛车跑道,可以看做一个加权有向图.每个跑道(有向边)还有一个特点就是,会周期性地打开a秒,然后关闭b秒.只有在赛车进入一直到出来,该跑道一直处于打开状态,赛车才能通过. 开始时所有跑道处 ...

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

    题意:赛车背景,给你n个节点,m条边的图以及起点和终点:其中每条边的信息包括u(起点),v(终点),a(开启的时间),b(关闭的时间),d(通过这条道路的时间):求最短通过的时间,其中车在进的时候,保 ...

  9. 紫书 例题11-11 UVa 12661 (dihkstra变形)

    这道题主要比较权值的时候要改变一下,其他地方基本一样. 比较权值的时候要考虑边的时间与a, b 可以设相对于当前边的时间now, 则now = d[u] % (a+b), 也就是当前这个边进行到整个a ...

随机推荐

  1. TypeScript `unknown` 类型

    unknown 字面理解和 any 其实没差,任何类型都可赋值给它,但有一点, Anything is assignable to unknown, but unknown isn't assigna ...

  2. 吝啬的国度 ---用vector 来构图

    根据题目可以看出来  有n 个城市 只有 n-1  条路线 那么  就可以确定这个图中  不存在 圆  所以从一个点到另一个点 只有一条唯一的路  所以从一个节点到另一个节点 那么 这个节点只有一个唯 ...

  3. 虚拟机下安装VM

    Linux(CentOS 7)命令行模式安装VMware Tools 详解 [日期:2017-05-02] 来源:Linux社区  作者:Linux [字体:大 中 小] 本篇文章主要介绍了如何在Li ...

  4. Codeforces 903G 巧妙的线段树

    思路: 巧妙的线段树 想方法将网络流往数据结构方向转化 http://www.cnblogs.com/yyf0309/p/8724558.html //By SiriusRen #include &l ...

  5. 【LeetCode】 -- 68.Text Justification

    题目大意:给定一个数组容器,里面存有很多string: 一个int maxWith.让你均匀安排每一行的字符串,能够尽可能的均匀. 解题思路:字符串+贪心.一开始想复杂了,总觉的题意描述的不是很清楚, ...

  6. 【转】Linux账号管理之useradd

    转自:http://www.jb51.net/article/45848.htm Linux 系统是一个多用户多任务的分时操作系统,任何一个要使用系统资源的用户,都必须首先向系统管理员申请一个账号,然 ...

  7. [转]linux之awk命令

    转自:http://blog.chinaunix.net/uid-23302288-id-3785105.html awk是行处理器: 相比较屏幕处理的优点,在处理庞大文件时不会出现内存溢出或是处理缓 ...

  8. 巧用Eclipse Java编辑器调试

    在使用Eclipse开发Java Web应用时,使用的编辑器不但能够为开发者提供代码编写.辅助提示和实时编译等常用功能,而且还能够对Java源代码进行快捷修改.重构和语法纠错等高级操作.通过Eclip ...

  9. 如何用putty链接服务器端,并安装wdcp

    首先把自己阿里云的磁盘格式化然后重启 自己下载一个PuTTY 打开后输入自己的Ip地址端口号默认是22 会跳出一个yes 跟no界面,点击yes 会进入一个类似cmd界面 直接输入root,然后会提示 ...

  10. css3背景渐变色代码

    从上到下 #grad {  background: -webkit-linear-gradient(red, blue);  background: -o-linear-gradient(red, b ...