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. jquery.find()

    http://www.365mini.com/page/jquery-find.htm

  2. Flex外包团队—开发工具:Flex4.6新特性介绍

    在今年初,Adobe发布了其第一个支持移动应用程序开发的Flex SDK和Flash Builder版本.Flex 4.5引入了一组移动优化的组件和移动优化的应用程序框架,而Flash Builder ...

  3. VS2013 启动时遇到空白窗口

    安装了VS2013 ,然后打开,遇到空白窗口,等了10几秒无果. 我先是修复了VS2013,然后再打开VS2013,则显示登录微软账号的窗口.修复完2013时正好18:08,可能是下班了可以上网,才正 ...

  4. ios的runtime为什么可以动态添加方法

    一句话概括 每个instance都有一个isa,这个isa,里面含有所有的方法列表,ios提供库函数增加,修改,即实现了动态添加方法

  5. M2的来源很简单

     http://bbs.tianya.cn/post-develop-412703-1.shtml M2的来源很简单,一部分是外汇占款,这个是我们国家的缺陷,如果外汇不管制,其实是不会发生增长人民币的 ...

  6. C# DEBUG 调试信息打印及输出详解

    转载自: http://blog.csdn.net/aaaaatiger/article/details/5583301 1.debug只在[debug模式下才执行](运行按钮后面的下拉框可选) 2. ...

  7. .NET中值得体验的精妙设计

    转自: http://developer.51cto.com/art/201104/255455_all.htm .NET 是 Microsoft XML Web services 平台.MEF是.N ...

  8. 黄聪:wordpress如何开启文章格式post format

    发现很多“古老”的WordPress主题使用量非常大,虽然部分也在随着WordPress版本的升级而“升级”,只不过是修复了bug而已,wordpress的新特性并没有使用.而且多数国内的wordpr ...

  9. 关于c语言中qsort函数的一点心得

    今天写c时无意间用到了排序,便想着使用c语言标准库中提供的排序函数,即qsort函数(c++stl中提供了sort函数用于排序),首先是介绍qsort函数的一些基本用法(以下内容转自: http:// ...

  10. codeforces 439 E. Devu and Birthday Celebration 组合数学 容斥定理

    题意: q个询问,每一个询问给出2个数sum,n 1 <= q <= 10^5, 1 <= n <= sum <= 10^5 对于每一个询问,求满足下列条件的数组的方案数 ...