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. beeframework开发笔记1

    1.商品页弹出规格去掉(null) wzmzy/shop/model/extensions/GOOD_SPEC_VALUE+TagList.m 2.

  2. 剑指offer系列48---左旋转字符串

    [题目]对于一个给定的字符序列S,旋转指定位置左边的字符到右边.. * 例如,字符序列S=”abcXYZdef”,要求输出循环左移3位后的结果,即“XYZdefabc”. * [思路]先分成两个部分: ...

  3. C语言每日一题之No.3

    几天下来,感慨学习要坚持下来真的是件很难的事,本来说了每天一题,可是毕竟这是个细活,需要用心雕琢,有时候真的不能当天拿下来>_<.虽然说只是一题,却涉及到很多小细节,慢慢的琢磨直至完全摸透 ...

  4. ibatis CDATA

    在使用ibatis时,经常需要配置待执行的sql语句.使用过ibatis的朋友都知道,无可避免的都会碰到一些不兼容.冲突的字符,多数人也都知道用<![CDATA[   ]]>标记避免Sql ...

  5. aptana studio 3 自动换行(无需插件)

    菜单-Window-Preferences-Aptana Studio-Editors,勾选“Enable word wrap”,然后重启编辑器.

  6. 关于oc中出现的typedef的用法/定义函数指针

    typedef int (^calculateBlock)(int a,int b); 这里面typedef的作用只是给 calculateBlock取了一个 别名,说明以后可以直接使用. calcu ...

  7. RAC_Oracle集群服务安装Grid Infrastructure(案例)

    2015-01-24 Created By BaoXinjian Thanks and Regards

  8. 洛谷 P1060 开心的金明

    开心的金明 Problem Description: 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间他自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:"你的房间需要购买哪些 ...

  9. iis7设置404页面不生效的原因

    打开web.config <httpErrors errorMode="Custom" existingResponse="PassThrough"> ...

  10. 使用POI读取excel文件内容

    1.前言 项目中要求读取excel文件内容,并将其转化为xml格式.常见读取excel文档一般使用POI和JExcelAPI这两个工具.这里我们介绍使用POI实现读取excel文档. 2.代码实例: ...