先上题目:

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*/

最新文章

  1. 完整的分页存储过程以及c#调用方法
  2. 无索引状态下比较DataTable的几种过滤方法效率
  3. 【Python】实现5!+4!+3!+2!+1!
  4. LuaTinker的bug和缺陷
  5. 重温CSS:Border属性
  6. 第二百三十五天 how can I 坚持
  7. Codeforces Round #307 (Div. 2) B. ZgukistringZ 暴力
  8. WCF之实例模型
  9. SQL 中的游标实例
  10. Websocket协议数据帧传输和关闭连接
  11. [读书笔记]算法(Sedgewick著)&#183;第一章(2)
  12. 武汉科技大学ACM :1004: A+B for Input-Output Practice (IV)
  13. python 在 for i in range() 块中改变 i 的值的效果
  14. Arcgis for javascript不同的状态下自己定义鼠标样式
  15. aix下java程序运行问题
  16. 一个简单的java贷款程序
  17. svn to git
  18. 数据库索引------Btree索引的使用限制
  19. Elasticsearch简介和安装对比
  20. LintCode Sqrt(x)

热门文章

  1. 9.15NOIP模拟题
  2. RocketMQ(1)--helloworld
  3. ex41习题 41: 来自 Percal 25 号行星的哥顿人(Gothons)
  4. Spring Boot (25) RabbitMQ消息队列
  5. Appium Python API 汇总
  6. CSS——个人资料demo
  7. SQL基本操作——约束
  8. 六时出行 App iOS隐私政策
  9. 怎样用Fiddler模拟网络超时
  10. 扩增子图表解读1箱线图:Alpha多样性