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.
输入
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.
输出
For each test case, print the shortest time, in seconds. It's always possible to arrive at t from s.
样例输入
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
样例输出
Case 1: 20
Case 2: 9
题目来源
题解:将时间看成最短路的模型,上一层的时间已经是最优的解了,所以下一层在更新的结点也会是最优的,如果到达的当前边时间为 T,那么就要判断是否要在当前城市等待了。。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int INF = ;
int n,m;
struct Edge{
int v,a,b,t,next;
}edge[];
int tot;
int head[];
void addEdge(int u,int v,int a,int b,int t,int &k){
edge[k].v = v,edge[k].a = a,edge[k].b = b,edge[k].t = t,edge[k].next = head[u],head[u]=k++;
}
void init(){
memset(head,-,sizeof(head));
tot = ;
}
int low[];
bool vis[];
int dijsktra(int s,int t){
memset(vis,false,sizeof(vis));
for(int i=;i<=n;i++){
low[i] = INF;
}
low[s] = ;
vis[s] = true;
for(int i=;i<n;i++){
int MIN = INF;
for(int j=;j<=n;j++){
if(low[j]<MIN&&!vis[j]){
MIN = low[j];
s = j;
}
}
vis[s] = true;
for(int k=head[s];k!=-;k=edge[k].next){
int v = edge[k].v,a=edge[k].a,b = edge[k].b,tim = edge[k].t ;
if(a>=tim){
int t0 = low[s]%(a+b);
if(t0+tim<=a) low[v] = min(low[s]+tim,low[v]);
else low[v] = min(low[s]+a+b-t0+tim,low[v]);
}
}
}
return low[t];
}
int main()
{
int s,t,cas=;
while(scanf("%d%d%d%d",&n,&m,&s,&t)!=EOF){
init();
for(int i=;i<=m;i++){
int u,v,a,b,t;
scanf("%d%d%d%d%d",&u,&v,&a,&b,&t);
addEdge(u,v,a,b,t,tot);
}
printf("Case %d: %d\n",cas++,dijsktra(s,t));
}
return ;
}
Funny Car Racing(最短路变形)的更多相关文章
- POJ-2253.Frogger.(求每条路径中最大值的最小值,最短路变形)
做到了这个题,感觉网上的博客是真的水,只有kuangbin大神一句话就点醒了我,所以我写这篇博客是为了让最短路的入门者尽快脱坑...... 本题思路:本题是最短路的变形,要求出最短路中的最大跳跃距离, ...
- POJ 3635 - Full Tank? - [最短路变形][手写二叉堆优化Dijkstra][配对堆优化Dijkstra]
题目链接:http://poj.org/problem?id=3635 题意题解等均参考:POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]. 一些口胡: ...
- POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]
题目链接:http://poj.org/problem?id=3635 Description After going through the receipts from your car trip ...
- POJ-1797Heavy Transportation,最短路变形,用dijkstra稍加修改就可以了;
Heavy Transportation Time Limit: 3000MS Memory Limit: 30000K Description Background Hugo ...
- HDOJ find the safest road 1596【最短路变形】
find the safest road Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- HN0I2000最优乘车 (最短路变形)
HN0I2000最优乘车 (最短路变形) 版权声明:本篇随笔版权归作者YJSheep(www.cnblogs.com/yangyaojia)所有,转载请保留原地址! [试题]为了简化城市公共汽车收费系 ...
- 天梯杯 PAT L2-001. 紧急救援 最短路变形
作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市的救援队数量和每一条连接两个城市的快速道路长度都标在地图上.当其他城市有紧急求 ...
- Heavy Transportation POJ 1797 最短路变形
Heavy Transportation POJ 1797 最短路变形 题意 原题链接 题意大体就是说在一个地图上,有n个城市,编号从1 2 3 ... n,m条路,每条路都有相应的承重能力,然后让你 ...
- POJ 2253 Frogger ( 最短路变形 || 最小生成树 )
题意 : 给出二维平面上 N 个点,前两个点为起点和终点,问你从起点到终点的所有路径中拥有最短两点间距是多少. 分析 : ① 考虑最小生成树中 Kruskal 算法,在建树的过程中贪心的从最小的边一个 ...
随机推荐
- 基于Memcached分布式系统DRDoS拒绝服务攻击技术研究(转)
本次反射式拒绝服务攻击技术基于全球互联网分布式的Memcached服务器,需要储备一定得安全攻防知识,网络协议知识和python代码编程技术.希望在学习本篇文章知识前自行学习相关的基础知识,文章后面同 ...
- 洛谷10月月赛R2·浴谷八连测R3题解
早上打一半就回家了... T1傻逼题不说了...而且我的写法比题解要傻逼很多T T T2可以发现,我们强制最大值所在的块是以左上为边界的倒三角,然后旋转4次就可以遍历所有的情况.所以二分极差,把最大值 ...
- 服务器上的 Git - 在服务器上搭建 Git
http://git-scm.com/book/zh/v2/%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%B8%8A%E7%9A%84-Git-%E5%9C%A8%E6%9C%8D%E ...
- openresty--centos7下开发环境安装
1. 安装依赖的软件包 yum install readline-devel pcre-devel openssl-devel gcc 2. 安装openresty -- 1. 下载openresty ...
- [Java多线程]-Thread和Runable源码解析之基本方法的运用实例
前面的文章:多线程爬坑之路-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类) 多线程爬坑之路-Thread和Runable源码解析 前面 ...
- jq的图片放大镜效果
<div class="imgbox"> <div class="probox"> <img src="" a ...
- DialogFragment 将数据传回Activity的onActivityResult方法
在MyActivity中 弹出一个DialogFragment (某一个控件的点击事件) search= findViewById(R.id.search); search.setOnClickLis ...
- Debian9(8)下python3和python2同时安装如何使用pip
在bash下Python会调用python2.x python3会调用python3.x 如果使用pip install命令安装模块会安装python2的模块. 而python3的pip命令使用的是p ...
- 「LibreOJ β Round #4」游戏
https://loj.ac/problem/524 题目描述 qmqmqm和sublinekelzrip要进行一场游戏,其规则是这样的: 首先有一个序列,其中每个位置是一个整数或是X.双方轮流将X的 ...
- HDU6128 二次剩余/二次域求二次剩余解/LL快速乘法取模
LINK 题意:求满足模p下$\frac{1}{a_i+a_j}\equiv\frac{1}{a_i}+\frac{1}{a_j}$的对数,其中$n,p(1\leq n\leq10^5,2\leq p ...