描述

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.

输入

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.

输出

For each test case, print the shortest time, in seconds. It's always possible to arrive at t from s.

样例输入

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

样例输出

Case 1: 20
Case 2: 9

题目来源

湖南省第九届大学生程序设计竞赛

题解:将时间看成最短路的模型,上一层的时间已经是最优的解了,所以下一层在更新的结点也会是最优的,如果到达的当前边时间为 T,那么就要判断是否要在当前城市等待了。。

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int INF = ;
int n,m;
struct Edge{
int v,a,b,t,next;
}edge[];
int tot;
int head[];
void addEdge(int u,int v,int a,int b,int t,int &k){
edge[k].v = v,edge[k].a = a,edge[k].b = b,edge[k].t = t,edge[k].next = head[u],head[u]=k++;
}
void init(){
memset(head,-,sizeof(head));
tot = ;
}
int low[];
bool vis[];
int dijsktra(int s,int t){
memset(vis,false,sizeof(vis));
for(int i=;i<=n;i++){
low[i] = INF;
}
low[s] = ;
vis[s] = true;
for(int i=;i<n;i++){
int MIN = INF;
for(int j=;j<=n;j++){
if(low[j]<MIN&&!vis[j]){
MIN = low[j];
s = j;
}
}
vis[s] = true;
for(int k=head[s];k!=-;k=edge[k].next){
int v = edge[k].v,a=edge[k].a,b = edge[k].b,tim = edge[k].t ;
if(a>=tim){
int t0 = low[s]%(a+b);
if(t0+tim<=a) low[v] = min(low[s]+tim,low[v]);
else low[v] = min(low[s]+a+b-t0+tim,low[v]);
}
}
}
return low[t];
}
int main()
{
int s,t,cas=;
while(scanf("%d%d%d%d",&n,&m,&s,&t)!=EOF){
init();
for(int i=;i<=m;i++){
int u,v,a,b,t;
scanf("%d%d%d%d%d",&u,&v,&a,&b,&t);
addEdge(u,v,a,b,t,tot);
}
printf("Case %d: %d\n",cas++,dijsktra(s,t));
}
return ;
}

最新文章

  1. 40GbE网络之后
  2. Vijos1881闪烁的繁星 [线段树]
  3. debug和release之间的时间优化问题
  4. Docker on Microsoft Azure
  5. 移除NDK方法
  6. 实验12:Problem C: 重载字符的加减法
  7. hdu 1874(Dijkstra + Floyd)
  8. SQL Server 2008导出数据为SQL脚本的操作步骤
  9. 初识Redis
  10. java_DAO类基本设计
  11. jquery自调用匿名函数解析
  12. 如何去除 ckeditor 上传图片后在原码中留下的 style=&quot;width: 100%;height:100px&quot;之类的代码呢?
  13. UVA 193 Graph Coloring 图染色 DFS 数据
  14. poj 2492 a bug&#39;s life 简单带权并查集
  15. STM32F10XX存储器细节
  16. 21 RadioGroup ListFragment
  17. android Native堆
  18. VS2017离线安装入门与出家
  19. &lt;记录&gt; PHP读取命令行参数
  20. [转]Tomcat的部署

热门文章

  1. python打造12306余票实时监控
  2. Insert Node in Sorted Linked List
  3. redis的简单事务
  4. RDD 算子补充
  5. 【数据结构】【平衡树】treap
  6. maven使用ss代理
  7. vim,删除所有
  8. NOIP 2015 提高组 Day1
  9. 分享一个数据库sql_mode 引起的坑
  10. 如何构建一个很棒网站页脚(Website Footer)