Problem F

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

Output for the Sample Input

Case 1: 20
Case 2: 9

The Ninth Hunan Collegiate Programming Contest (2013) Problemsetter: Rujia Liu Special Thanks: Md. Mahbubul Hasan

   很明显的快速最短路,满足最优性,用堆优化,扩展边的时候判断2种情况。注意边是有向边。万变不离其宗。

#include <iostream>
#include <stdio.h>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <queue>
#include <set>
#include <algorithm>
#include <map>
#include <stack>
#include <math.h>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std ;
typedef long long LL ;
const int Max_N= ;
const LL inf=(LL) ;
struct Road{
int V ;
LL open_time ;
LL close_time ;
LL T ;
Road(){} ;
Road(int v ,LL o ,LL c ,LL t):V(v),open_time(o),close_time(c),T(t){} ;
};
vector<Road>vec[Max_N] ;
int N ,M ,S ,T ;
struct Node{
int u ;
LL Time ;
Node(){} ;
Node(int U ,LL t):u(U),Time(t){} ;
friend bool operator < (const Node A ,const Node B){
return A.Time>B.Time ;
}
};
LL dist[Max_N] ;
void spfa(){
fill(dist,dist++N,inf) ;
priority_queue<Node>que ;
que.push(Node(S,)) ;
dist[S]= ;
while(!que.empty()){
Node now = que.top() ;
que.pop() ;
if(now.u==T){
return ;
}
int u = now.u ;
for(int i= ; i<vec[u].size() ;i++){
Road r=vec[u][i] ;
int v = r.V ;
LL o_t = r.open_time ;
LL c_t = r.close_time ;
LL t = r.T ;
if(r.T > o_t)
continue ;
LL res = ( now.Time%(o_t+c_t)+(o_t+c_t) ) % (o_t+c_t) ;
if(res+t<=o_t&&now.Time+t<dist[v]){
dist[v]=now.Time+t ;
que.push(Node(v,dist[v]));
}
else if(now.Time+o_t+c_t-res+t<dist[v]){
dist[v] = now.Time+o_t+c_t-res+t ;
que.push(Node(v ,dist[v])) ;
}
}
}
}
int main(){
int k= ;
int u ,v ,open_t ,close_t ,t;
while(scanf("%d%d%d%d",&N,&M,&S,&T)!=EOF){
for(int i=;i<=N;i++)
vec[i].clear() ;
for(int i=;i<=M;i++){
scanf("%d%d%d%d%d",&u,&v,&open_t,&close_t,&t) ;
vec[u].push_back(Road(v,(LL)open_t,(LL)close_t,(LL)t)) ;
// vec[v].push_back(Road(u,(LL)open_t,(LL)close_t,(LL)t)) ;
}
spfa() ;
printf("Case %d: ",k++) ;
cout<<dist[T]<<endl ;
}
return ;
}

The Ninth Hunan Collegiate Programming Contest (2013) Problem F的更多相关文章

  1. The Ninth Hunan Collegiate Programming Contest (2013) Problem A

    Problem A Almost Palindrome Given a line of text, find the longest almost-palindrome substring. A st ...

  2. The Ninth Hunan Collegiate Programming Contest (2013) Problem H

    Problem H High bridge, low bridge Q: There are one high bridge and one low bridge across the river. ...

  3. The Ninth Hunan Collegiate Programming Contest (2013) Problem I

    Problem I Interesting Calculator There is an interesting calculator. It has 3 rows of button. Row 1: ...

  4. The Ninth Hunan Collegiate Programming Contest (2013) Problem J

    Problem J Joking with Fermat's Last Theorem Fermat's Last Theorem: no three positive integers a, b, ...

  5. The Ninth Hunan Collegiate Programming Contest (2013) Problem G

    Problem G Good Teacher I want to be a good teacher, so at least I need to remember all the student n ...

  6. The Ninth Hunan Collegiate Programming Contest (2013) Problem L

    Problem L Last Blood In many programming contests, special prizes are given to teams who solved a pa ...

  7. The Ninth Hunan Collegiate Programming Contest (2013) Problem C

    Problem C Character Recognition? Write a program that recognizes characters. Don't worry, because yo ...

  8. The 2019 China Collegiate Programming Contest Harbin Site F. Fixing Banners

    链接: https://codeforces.com/gym/102394/problem/F 题意: Harbin, whose name was originally a Manchu word ...

  9. 2019 China Collegiate Programming Contest Qinhuangdao Onsite F. Forest Program(DFS计算图中所有环的长度)

    题目链接:https://codeforces.com/gym/102361/problem/F 题意 有 \(n\) 个点和 \(m\) 条边,每条边属于 \(0\) 或 \(1\) 个环,问去掉一 ...

随机推荐

  1. java单例模式和双例模式

    今天朋友找我给做道题,双例模式,我是没听说过,都说是单例模式和多例模式, 也不知道双例模式什么时候用,就简单写了一个案例,不知道对不对,个人感觉蛮对的,双例就是单例+单例,废话不说了!!!! /* * ...

  2. SSH_框架整合2—查询显示

    4. 完成功能. (1)com.atguigu.ssh.actions包下新建EmployeeAction.java package com.atguigu.ssh.actions; import j ...

  3. ASP.NET动态加载Js代码到Head标签中(三种方法)

    方法一代码如下: HtmlGenericControl Include2 = new HtmlGenericControl("script"); Include2.Attribut ...

  4. Java的clone机制(及String的特殊性)

    1. Clone&Copy 假设现在有一个Employee对象,Employee tobby =new Employee(“CMTobby”,5000),通常我们会有这样的赋值Employee ...

  5. PLSQL_基础系列11_递归和层次查询CONNECT BY(案例)

    2015-05-31 Created By BaoXinjian

  6. [实变函数]2.3 开集 (open set), 闭集 (closed set), 完备集 (complete set)

    1        $$\beex \bea E\mbox{ 是开集}&\lra E^o=E\\        &\lra \forall\ P_0\in E,\ \exists\ U( ...

  7. 关于htmlspecialchars实体字符转码的问题

    php对post过来的数据进行实体字符转码,我的页面编码是gb2312,刚开始是这样: $post = htmlspecialchars ( $post); 取到的$post值为空,但是有时候是好的( ...

  8. STM32的串口

    一:2个状态位_itstatus与_flagstatus的区别: _flagstatus:只是读状态标志,不管中断是否使能或发生.例如使用查询方式发送数据就需要读改状态位. _itstatus:和中断 ...

  9. UEditor使用有感(黄色)

    UEditor 介绍 UEditor 是由百度「FEX前端研发团队」开发的所见即所得富文本web编辑器,具有轻量,可定制,注重用户体验等特点,开源基于MIT协议,允许自由使用和修改代码. 1 入门部署 ...

  10. CEO应向软件工程师学习的7个技能

    软件工程师的哪些技能是值得CEO学习的?显然,软件工程师是逻辑的,高效的,注重细节的,有计划的,并且大多数CEO也是如此.但是,软件工程师还有一些更微妙,甚至是令人懊恼的品质,那么CEO是否可以从中学 ...