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. ajax简单案例:返回json型

    主页: <!--输入代号点击查询查到本代号的人名--> <body> <div> 请输入代号:<input type="text" id= ...

  2. ORACLE的表被 另一个用户锁定,如何解除..

    SELECT object_name, machine, s.sid, s.serial# FROM gv$locked_object l, dba_objects o, gv$session s W ...

  3. sqlserver 2005列转行

    isnull(stuff((select ',' + d.comname from projemp a left outer join emps c on a.empid = c.empidleft ...

  4. 去掉IntelliJ IDEA的拼写检查

    Settings→Editor→Inspections→Spelling 去掉Spelling下的Typo复选框即可 来自为知笔记(Wiz)

  5. Linux中升级更新命令yum upgrade和yum update的区别

    区别 Linux升级命令有两个分别是yum upgrade和yum update, 这个两个命令是有区别的: 代码如下: yum -y update #升级所有包,同时也升级软件和系统内核 yum - ...

  6. TMDS协议

    1 概述 1.1    连接结构 图1 TMDS连接结构 数据流中包含了像素和控制数据,发送器在任何给定的输入时钟周期,到底是编码像素数据还是控制数据取决于数据使能信号DE,DE有效时,指示像素数 据 ...

  7. extern 修饰符

    extern(C# 参考) extern 修饰符用于声明在外部实现的方法. extern 修饰符的常见用法是在使用 Interop 服务调入非托管代码时与 DllImport 特性一起使用.在这种情况 ...

  8. shell下root用户切换其他用户运行程序

    工作中,一些程序,需要随机启动,但是不是以root用户运行,于是需要在rc.local中通过shell,从root用户切换到其他用户运行程序,命令如下: su -c 'command' - user ...

  9. [运维-服务器 – 2A] – nginx下绑定域名

    这个篇文章今天(2016-01-21)才有幸写了,因为自己对nginx部署以前没玩过,还得感谢下我们数字化的总经理.在这里记录下自己成长的经验,与遇到的问题. 因为自己的域名是在万网上买的,解析无法直 ...

  10. JVM知识学习与巩固

    JVM是Java Virtual Machine(Java虚拟机)的缩写,JVM是一种用于计算设备的规范,它是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟各种计算机功能来实现的. 我们运行和调 ...