Funny Car Racing CSU - 1333 (spfa)
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)的更多相关文章
- 模板C++ 03图论算法 1最短路之单源最短路(SPFA)
3.1最短路之单源最短路(SPFA) 松弛:常听人说松弛,一直不懂,后来明白其实就是更新某点到源点最短距离. 邻接表:表示与一个点联通的所有路. 如果从一个点沿着某条路径出发,又回到了自己,而且所经过 ...
- 最短路(SPFA)
SPFA是Bellman-Ford算法的一种队列实现,减少了不必要的冗余计算. 主要思想是: 初始时将起点加入队列.每次从队列中取出一个元素,并对所有与它相邻的点进行修改,若某个相邻的点修改成功,则将 ...
- Bellman-Ford算法及其队列优化(SPFA)
一.算法概述 Bellman-Ford算法解决的是一般情况下的单源最短路径问题.所谓单源最短路径问题:给定一个图G=(V,E),我们希望找到从给定源结点s属于V到每个结点v属于V的最短路径.单源最短路 ...
- CSU 1659: Graph Center(SPFA)
1659: Graph Center Time Limit: 1 Sec Memory Limit: 128 MB Submit: 63 Solved: 25 [id=1659"> ...
- sgu 240 Runaway (spfa)
题意:N点M边的无向图,边上有线性不下降的温度,给固定入口S,有E个出口.逃出去,使最大承受温度最小.输出该温度,若该温度超过H,输出-1. 羞涩的题意 显然N*H的复杂度dp[n][h]表示到达n最 ...
- codevs 1021 玛丽卡(spfa)
题目描述 Description 麦克找了个新女朋友,玛丽卡对他非常恼火并伺机报复. 因为她和他们不住在同一个城市,因此她开始准备她的长途旅行. 在这个国家中每两个城市之间最多只有一条路相通,并且我们 ...
- 【POJ】1062 昂贵的聘礼(spfa)
http://poj.org/problem?id=1062 此题一开始果断想到暴力.. 但是n<=100果断不行. 一看题解,噗!最短路... 构图很巧妙. 每一个物品对应的所需物品相当于一个 ...
- POJ1860Currency Exchange(SPFA)
http://poj.org/problem?id=1860 题意: 题目中主要是说存在货币兑换点,然后现在手里有一种货币,要各种换来换去,最后再换回去的时候看能不能使原本的钱数增多,每一种货币都有 ...
- 【NOIP 2013 DAY2 T3】 华容道(spfa)
题目描述 [问题描述] 小 B 最近迷上了华容道,可是他总是要花很长的时间才能完成一次.于是,他想到用编程来完成华容道:给定一种局面, 华容道是否根本就无法完成,如果能完成, 最少需要多少时间. 小 ...
随机推荐
- 4 Values whose Sum is 0 POJ 2785 (折半枚举)
题目链接 Description The SUM problem can be formulated as follows: given four lists A, B, C, D of intege ...
- 【转】关于Scapy
关于Scapy Scapy的是一个强大的交互式数据包处理程序(使用python编写).它能够伪造 或者解码大量的网络协议数据包,能够发送.捕捉.匹配请求和回复包等等.它可以很容易地处理一些典型操作,比 ...
- HTTP响应码摘自apach官网
HTTP状态列表 响应码由三位十进制数字组成,它们出现在由HTTP服务器发送的响应的第一行. 响应码分五种类型,由它们的第一位数字表示: 1xx:信息,请求收到,继续处理 2xx:成功,行为被成功地接 ...
- 源码分析之tinyhttpd-0.1
1. 简介: tinyhttpd是使用c语言开发的超轻量级http服务器,通过代码流程可以了解http服务器的基本处理流程, 并且涉及了网络套接字,线程,父子进程,管道等等知识点: 项目地址:http ...
- 【Android framework】am命令启动Activity流程
源码基于Android 4.4. am start -W -n com.dfp.test/.TEstActivity -W:等目标Activity启动后才返回 -n:用于设置Intent的Comp ...
- FreeRADIUS + MySQL 安装配置笔记
FreeRADIUS + MySQL 安装配置笔记 https://www.2cto.com/net/201110/106597.html
- 图论-单源最短路-SPFA算法
有关概念: 最短路问题:若在图中的每一条边都有对应的权值,求从一点到另一点之间权值和最小的路径 SPFA算法的功能是求固定起点到图中其余各点的的最短路(单源最短路径) 约定:图中不存在负权环,用邻接表 ...
- C 实现一个简易的Http服务器 (二)
正文 - 直接搞起 C 实现一个简易的Http服务器 很久以前写过一个简易的http服务器, 后面和一个朋友交流, 反思后发现问题不少.在这里简单搞一下. 让其更加简单去表现httpd本质, 弱化协议 ...
- C基础 时间业务实战代码
引言 业务代码中遇到这样需求, 1. 二者是同一天吗, 2. 时间戳和时间串来回转, 3. 其它扩展需求 等. C写代码同样需要处理这方面时间问题. 本文就是为了解决这个问题. 相比其它时间库, 这里 ...
- FineReport——登录不到决策系统
在不断的测试过程中,可能会造成缓存数据的累积,所以在登录过程中可能会出现登录不到决策系统,而是跳转到某一模板页面 解决方法就是清理缓存或者换一个浏览器测试.