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

Sample Output

Case 1: 20

Case 2: 9

#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<math.h>
#include<cstdio>
#include<sstream>
#include<numeric>//STL数值算法头文件
#include<stdlib.h>
#include <ctype.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<functional>//模板类头文件
using namespace std; typedef long long ll;
const int maxn=1100;
const int INF=0x3f3f3f3f; struct node
{
ll to, next ;
ll a, b, c, d ;
} E[50010]; ll id, head[maxn] ;
ll dis[maxn] ;
bool vis[maxn]; void init()
{
id = 0;
memset(head, -1, sizeof(head));
for(ll i = 0 ; i < maxn ; i++)
dis[i] = INF ;
} void addEdg(ll u, ll v, ll a, ll b, ll c)
{
E[id].to = v ;
E[id].a = a ;
E[id].b = b ;
E[id].c = c ;
E[id].d = a + b ;
E[id].next = head[u] ;
head[u] = id++;
}
void spfa(ll s, ll t)
{
memset(vis,0,sizeof(vis));
queue<ll>q;
dis[s] = 0 ;
vis[s]=1;
if(s != t )
q.push( s ) ;
while(!q.empty())
{
ll u = q.front() ;
q.pop() ;
vis[u] = 0 ;
for(ll i = head[u] ; i!=-1; i=E[i].next)
{
ll v = E[i].to ;
ll tt = E[i].a - (dis[u]%E[i].d);
if(tt<E[i].c)
tt += E[i].b ;
else
tt = 0 ;
if(dis[v] > dis[u] + tt +E[i].c)
{
dis[v] = dis[u] + tt +E[i].c ;
if(!vis[v]&&v!=t)
{
vis[v] = 1;
q.push(v);
}
}
}
}
} int main()
{
ll n, m, s, t, u, v, a, b, c ;
ll T = 0;
while(~scanf("%lld%lld%lld%lld",&n,&m,&s,&t))
{
init();
while(m--)
{
scanf("%lld%lld%lld%lld%lld",&u, &v, &a, &b, &c) ;
if(c<=a)
addEdg( u, v, a, b, c );
}
spfa(s, t ) ;
if(dis[t]==INF)
dis[t] = -1;
printf("Case %lld: %lld\n",++T,dis[t]);
}
}

Funny Car Racing CSU - 1333 (spfa)的更多相关文章

  1. 模板C++ 03图论算法 1最短路之单源最短路(SPFA)

    3.1最短路之单源最短路(SPFA) 松弛:常听人说松弛,一直不懂,后来明白其实就是更新某点到源点最短距离. 邻接表:表示与一个点联通的所有路. 如果从一个点沿着某条路径出发,又回到了自己,而且所经过 ...

  2. 最短路(SPFA)

    SPFA是Bellman-Ford算法的一种队列实现,减少了不必要的冗余计算. 主要思想是: 初始时将起点加入队列.每次从队列中取出一个元素,并对所有与它相邻的点进行修改,若某个相邻的点修改成功,则将 ...

  3. Bellman-Ford算法及其队列优化(SPFA)

    一.算法概述 Bellman-Ford算法解决的是一般情况下的单源最短路径问题.所谓单源最短路径问题:给定一个图G=(V,E),我们希望找到从给定源结点s属于V到每个结点v属于V的最短路径.单源最短路 ...

  4. CSU 1659: Graph Center(SPFA)

    1659: Graph Center Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 63  Solved: 25 [id=1659"> ...

  5. sgu 240 Runaway (spfa)

    题意:N点M边的无向图,边上有线性不下降的温度,给固定入口S,有E个出口.逃出去,使最大承受温度最小.输出该温度,若该温度超过H,输出-1. 羞涩的题意 显然N*H的复杂度dp[n][h]表示到达n最 ...

  6. codevs 1021 玛丽卡(spfa)

    题目描述 Description 麦克找了个新女朋友,玛丽卡对他非常恼火并伺机报复. 因为她和他们不住在同一个城市,因此她开始准备她的长途旅行. 在这个国家中每两个城市之间最多只有一条路相通,并且我们 ...

  7. 【POJ】1062 昂贵的聘礼(spfa)

    http://poj.org/problem?id=1062 此题一开始果断想到暴力.. 但是n<=100果断不行. 一看题解,噗!最短路... 构图很巧妙. 每一个物品对应的所需物品相当于一个 ...

  8. POJ1860Currency Exchange(SPFA)

    http://poj.org/problem?id=1860 题意:  题目中主要是说存在货币兑换点,然后现在手里有一种货币,要各种换来换去,最后再换回去的时候看能不能使原本的钱数增多,每一种货币都有 ...

  9. 【NOIP 2013 DAY2 T3】 华容道(spfa)

    题目描述 [问题描述] 小 B 最近迷上了华容道,可是他总是要花很长的时间才能完成一次.于是,他想到用编程来完成华容道:给定一种局面, 华容道是否根本就无法完成,如果能完成, 最少需要多少时间. 小 ...

随机推荐

  1. 【51NOD-0】1058 N的阶乘的长度

    [算法]数学 [题解]n!的位数相当于ans=log10(n!)上取整,然后就可以拆出来加了. 可以用log10(i)或log(i)/log(10) 阶乘好像有个斯特林公式…… #include< ...

  2. WP8.1 Windows Phone 8.1开发:何如定义Pivot头部样式、定义Pivot头部颜色

    Windows Phone 8.1 ,如何自定义Pivot头部样式?用Pivot控件完成这样的效果. 网上找了好久,只找到了windows phone 8的解决方案. 终于一个大神给支了招,我觉得我有 ...

  3. es6异步操作

    异步编程对 JavaScript 语言太重要.JavaScript 只有一根线程,如果没有异步编程,根本没法用,非卡死不可. ES6 诞生以前,异步编程的方法,大概有下面四种. 回调函数 事件监听 发 ...

  4. 第三周main参数传递-1 课堂测试

    课堂测试 main参数传递-1 测试 参考 http://www.cnblogs.com/rocedu/p/6766748.html#SECCLA 在Linux下完成"求命令行传入整数参数的 ...

  5. Coursera助学金申请模板

    讲真,我觉得coursera的课还挺贵的.但是它有助学金系统,非常对我们穷学生友好了,而且基本上申请的都会批.不过现在助学金需要15个工作日才有答复,所以注意要提前申请. 有两大段要填. 虽然写的挺烂 ...

  6. Centos7编译安装Xen环境(vtpm)

    编译xen环境(http://www.lvtao.net/server/574.html#comment-1882): yum update yum groupinstall "Develo ...

  7. Win10打开照片提示“无效的注册表值”解决方法

    1.点开开始菜单,右键单击,选择“以管理员运行”[键盘win键+R]输入PowerShell. 2.输入Get-AppxPackage *photo* | Remove-AppxPackage后回车. ...

  8. C++中STL容器的比较

    基本参考 https://blog.csdn.net/qq_14898543/article/details/51381642 容器特性: vector:典型的序列容器,C++标准严格要求次容器的实现 ...

  9. 2017多校第6场 HDU 6097 Mindis 计算几何,圆的反演

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6097 题意:有一个圆心在原点的圆,给定圆的半径,给定P.Q两点坐标(PO=QO,P.Q不在圆外),取圆 ...

  10. wifi驱动移植

    目标板:Hi3518 内核版本:linux3.0.8   1.修改makefile #PLATFORM = PC //注释掉 PLATFORM = HI3518 //支持平台 ifeq ($(PLAT ...