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. WPF Image控件使用本地图片

    BitmapImage bi = new BitmapImage(); // BitmapImage.UriSource must be in a BeginInit/EndInit block. b ...

  2. android学习笔记35——AnimationDrawable资源

    AnimationDrawable资源 AnimationDrawable,代表一个动画. android既支持传统的逐帧动画(类似于电影方式,一张图片一张图片的切换),也支持通过平移.变换计算出来的 ...

  3. (一)java的由来

    java的诞生:每一次设计语言的革新都是为了解决先前语言所遇到的不能解决的问题,B语言导致C语言的诞生,C语言演变成C++,java则继承了这两种语言的大部分特性.java最初的推动力是为了找到一种能 ...

  4. PLSQL_数据泵导入导出数据Impdp/ Expdp(概念)

    2014-08-31 Created By BaoXinjian

  5. Shell_Oracle Erp基于主机文件Host开发详解(案例)

    2014-06-20 Created By BaoXinjian

  6. 转载__直接拿来用!最火的Android开源项目(一)

    http://www.csdn.net/article/2013-05-03/2815127-Android-open-source-projects 已分类汇总到 https://github.co ...

  7. REST架构实质(转)

    REST(Representational State Transfer) 曾经被误解为只是CRUD(增删改查),从这个层面上,好像REST只是和RPC一个层面的东西,没有什么了不起,其实这些都是对R ...

  8. 并发容器之ConcurrentSkipListSet

    概要 本章对Java.util.concurrent包中的ConcurrentSkipListSet类进行详细的介绍.内容包括:ConcurrentSkipListSet介绍ConcurrentSki ...

  9. Mac 终端常用命令备忘

    Tab 补全 pwd 显示路径 一 .ls ls -lh   查看当前路径详细文件 ls ..     返回上级目录 ls -a   显示隐藏文件 ls -a -l 以详细列表显示 ls ../../ ...

  10. 原密码忘了,重置MAC开机密码

    如果登陆密码忘了,或者你接手了一台前任同事的MAC,而他设了密码没告诉你,你可以这样: 1.重启MAC,然后在启动时按下:CMD+S 2.进入命令行格式后输入:fsck -y (file system ...