[ABC204E] Rush Hour 2 题解
题目大意
给定一张无向图,边带两个参数 \(c_i,d_i\),在 \(t\) 时间时经过第 \(i\) 条边所需的时间是 \(c_i+\lfloor\frac{d_i}{t+1}\rfloor\),求在时间 \(0\) 时出发,在每个点可以停留非负整数时间,从点 \(1\) 到点 \(n\) 所需的最短时间。
思路分析
首先,容易发现在时间 \(t\) 时经过第 \(i\) 条边后的总时间是:\(t+c_i+\lfloor\frac{d_i}{t+1}\rfloor\)。
那么不妨设函数 \(f(t)=t+c_i+\lfloor\frac{d_i}{t+1}\rfloor\),由均值不等式容易得 \(f(t)_{\min}=f(\sqrt{d_i}-1)\)。
也就是说,当我们想经过某一条边时,如果当前时间大于 \(\sqrt {d_i}-1\),那么不用等待直接通过,否则等到 \(\sqrt{d_i}-1\) 时再通过。
那么就可以使用普通的 Dijkstra 解决了。
代码
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
#define int long long
const int N=200100;
int n,m,idx=1,in1,in2,in3,in4;
int to[N],nxt[N],head[N];
int fa[N],vis[N];
int dis[N],c[N],d[N];
int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
void add(int u,int v,int x,int y){
idx++;to[idx]=v;nxt[idx]=head[u];head[u]=idx;c[idx]=x;d[idx]=y;
}
struct Node{
int x,time;
bool operator < (const Node &b) const{
return time>b.time;
}
}now;
priority_queue <Node> q;
void Dijkstra(){
memset(dis,0x3f,sizeof dis);
dis[1]=0;q.push(Node{1,0});
while(!q.empty()){
now=q.top();q.pop();
if(vis[now.x]) continue;
vis[now.x]=1;
for(int i=head[now.x];i;i=nxt[i]){
int v=to[i],t;
if(now.time<=round(sqrt(d[i]))-1) t=round(sqrt(d[i]))-1;
else t=now.time; //判断通过时间
if(dis[v]>t+c[i]+d[i]/(t+1)){
dis[v]=t+c[i]+d[i]/(t+1);
q.push(Node{v,dis[v]});
}
}
}
}
signed main(){
scanf("%lld%lld",&n,&m);
for(int i=1;i<=n;i++) fa[i]=i;
for(int i=1;i<=m;i++){
scanf("%lld%lld%lld%lld",&in1,&in2,&in3,&in4);
add(in1,in2,in3,in4);add(in2,in1,in3,in4);
fa[find(in1)]=find(in2);
}
if(find(1)!=find(n)){cout<<"-1\n";return 0;}//并查集判连通
Dijkstra();
cout<<dis[n]<<'\n';
return 0;
}
[ABC204E] Rush Hour 2 题解的更多相关文章
- Codeforces Round #102 (Div. 2) 题解
A. 解一个方程. 还是厚颜无耻地暴力吧~ #include <iostream> using namespace std; int r1, r2, c1, c2, d1, d2; boo ...
- 【第400篇题解纪念2016年10月28日】【28.10%】【codeforces 617E】XOR and Favorite Number
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【NOI 2019】同步赛 / 题解 / 感想
非常颓写不动题怎么办…… 写下这篇博客警示自己吧…… 游记 7.16 我并不在广二参加 NOI,而是在距离广二体育馆一公里远的包间打同步赛(其实就是给写不动题找个理由) 上午身体不舒服,鸽了半天才看题 ...
- Codeforces Round #624 (Div. 3) F. Moving Points 题解
第一次写博客 ,请多指教! 翻了翻前面的题解发现都是用树状数组来做,这里更新一个 线段树+离散化的做法: 其实这道题是没有必要用线段树的,树状数组就能够解决.但是个人感觉把线段树用熟了会比树状数组更有 ...
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
- noip2016十连测题解
以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...
- BZOJ-2561-最小生成树 题解(最小割)
2561: 最小生成树(题解) Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1628 Solved: 786 传送门:http://www.lyd ...
- Codeforces Round #353 (Div. 2) ABCDE 题解 python
Problems # Name A Infinite Sequence standard input/output 1 s, 256 MB x3509 B Restoring P ...
- 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解
题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...
- 2016ACM青岛区域赛题解
A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
随机推荐
- Kubernetes(k8s) Web-UI界面(一):部署和访问仪表板(Dashboard)
目录 一.系统环境 二.前言 三.仪表板(Dashboard)简介 四.部署Kubernetes仪表板(Dashboard) 五.访问Kubernetes仪表板(Dashboard) 5.1 使用to ...
- Collection 接口及其常用方法
Collection 接口的特点 Collection接口没有直接实现类,提供了更具体的子接口(如Set和List)的实现.Collection实现类(通常通过其中一个子接口间接实现Collectio ...
- Sentieon | 每周文献-Population Sequencing-第一期
群体基因组系列文章-1 标题(英文):The Impact of ACEs on BMI: An Investigation of the Genotype-Environment Effects o ...
- day-3 路由底层源码
1. 定义路由本质 比如在url.py定义以下路由,浏览器中输入http://192.168.0.1:8000/user/2003-04-21可以访问 意味着此url http://192.168.0 ...
- 最为常用的Laravel操作(1)-Eloquent模型
快速入门 更换表名 protected $table = 'my_flights'; 更换主键名称 protected $primaryKey = 'id'; 注意: Eloquent 默认主键字段是 ...
- Mybatis(日志工厂)
日志工厂 如果一个数据库操作,出现了异常,我们需要排错.所以日志就是最好的助手 曾经:sout.debug 现在:日志工厂 SLF4J[工作中,springboot] LOG4J[掌握] LOG4J2 ...
- 2021-7-30 MySql进阶2
创建临时表只需在table前面加temporary CREATE TEMPORARY TABLE mytable#创建临时表,在断开数据库连接时销毁 ( ID INT NOT NULL, userna ...
- 代码随想录算法训练营第三天| LeetCode 203.移除链表元素(同时也对整个单链表进行增删改查操作) 707.设计链表 206.反转链表
203.移除链表元素 题目链接/文章讲解/视频讲解::https://programmercarl.com/0203.%E7%A7%BB%E9%99%A4%E9%93%BE%E8%A1 ...
- Django:django.core.exceptions.ImproperlyConfigured: Specifying a namespace in include() without providing an app_name is not supported.
from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/' ...
- word中查找替换不能使用 解决方案
打开查找,然后点更多,最下面点不限定格式