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 最近迷上了华容道,可是他总是要花很长的时间才能完成一次.于是,他想到用编程来完成华容道:给定一种局面, 华容道是否根本就无法完成,如果能完成, 最少需要多少时间. 小 ...
随机推荐
- 2017 JAVA神器 Btrace详细介绍
官网:https://github.com/btraceio/btrace 下载:https://github.com/btraceio/btrace/releases/tag/v1.3.9 文档:h ...
- Long Parameter List(过长参数列)---要重构的味道
一个函数,它的参数过多是不好的,不好维护和修改,易读性也差,容易出错. 消除过长参数的方法,有如下: 1.在面向对象中,你可以传递一个对象给函数,函数通过访问对象来获得参 ...
- 【转载】Quick-Cocos2d-x文件结构分析
在上一章我们讲过了Quick-Cocos2d-x中的环境搭建,这章我们分析下quick中的文件结构吧!打开quick的文件夹,可以看到如下的这些目录和文件: bin:存放各种与引擎相关的脚本 comp ...
- perl模拟登录(1)
use WWW::Mechanize; my $ua = WWW::Mechanize->new(); $ua->post('http://localhost/dvwa/DVWA-mast ...
- 【Linux学习】nohup后台运行程序以及输出重定向
Linux有两种命令使程序后台运行 第一种:支持后台运行,但是关闭终端的话,程序也会停止 command & 第二种:支持后台运行,关闭终端后,程序也会继续运行 nohup command & ...
- [Leetcode Week15]Populating Next Right Pointers in Each Node
Populating Next Right Pointers in Each Node 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/populati ...
- 函数导出在kvm_intel.ko,kvm.ko不共享
KVM一共包含了三个内核模块,kvm_intel.ko,kvm_amd.ko,kvm.ko.其中两个重要文件x86.c和vmx.c在编译后分别会生成kvm_intel.ko和kvm.ko两个内核模块, ...
- CentOS7手动编译安装内核4.11.7
1. 进入/usr/src/目录 cd /usr/src 2. 下载内核源码,网址:https://www.kernel.org wget https://cdn.kernel.org/pub/lin ...
- 增大dma的分配
前言 项目中需要通过驱动与fpga通讯,获取fpga往内存里写的数据.因为数据量比较大,需要驱动分配600多M的内存给fpga来写数据,且因为是与fpga通讯,需要连续的内存,还得是uncached的 ...
- centos6.5升级Linux内核步骤
centos6.5升级Linux内核步骤 http://www.jianshu.com/p/c75f00182b4c 使用的操作系统是是centos6.5,按照官方的推荐的配置,把linux内核升级到 ...