洛谷 2176 [USACO14FEB]路障Roadblock

【题意概述】
修改图中任一一条边的边权,使其加倍,问怎样使修改后图中的1~n的最短路最大。输出最短路的增量。
【题解】
先跑一遍dijkstra求出1~n的路径长度,记录下经过的边。枚举这些边进行修改,再跑dijkstra求出某条边修改后的最短路径的长度。
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
#define N 200010
#define rg register
using namespace std;
int n,m,tot,ans0,ans1,fa,son,cnt,last[N],pos[N],dis[N],fp[N],fe[N],st[N];
struct edge{int to,pre,d;}e[N];
struct heap{int poi,d;}h[N];
inline int read(){
int k=,f=; char c=getchar();
while(c<''||c>'')c=='-'&&(f=-),c=getchar();
while(''<=c&&c<='')k=k*+c-'',c=getchar();
return k*f;
}
inline void up(int x){
while((fa=(x>>))>&&h[fa].d>h[x].d)
swap(h[fa],h[x]),swap(pos[h[fa].poi],pos[h[x].poi]),x=fa;
}
inline void down(int x){
while((son=(x<<))<=tot){
if(h[son+].d<h[son].d&&son<tot) son++;
if(h[son].d<h[x].d) swap(h[son],h[x]),swap(pos[h[x].poi],pos[h[son].poi]),x=son;
else break;
}
}
inline void dijkstra(int x){
for(rg int i=;i<=n;i++) dis[i]=1e9,pos[i]=;
h[tot=pos[x]=]=(heap){x,dis[x]=};
while(tot){
int now=h[].poi; pos[h[tot].poi]=; h[]=h[tot--]; if(tot) down();
for(rg int i=last[now],to;i;i=e[i].pre)if(dis[to=e[i].to]>dis[now]+e[i].d){
dis[to]=dis[now]+e[i].d; fp[to]=now; fe[to]=i;
if(!pos[to]) h[pos[to]=++tot]=(heap){to,dis[to]};
else h[pos[to]].d=dis[to];
up(pos[to]);
}
}
}
int main(){
n=read(); m=read();
for(rg int i=;i<=m;i++){
int u=read(),v=read(),d=read();
e[++tot]=(edge){v,last[u],d}; last[u]=tot;
e[++tot]=(edge){u,last[v],d}; last[v]=tot;
}
dijkstra();
ans0=dis[n];
int now=n;
while(fp[now]){
st[++cnt]=fe[now];
// printf("now=%d\n",now);
now=fp[now];
}
// for(rg int i=1;i<=cnt;i++) printf("%d ",st[i]);
for(rg int i=;i<=cnt;i++){
int ed=st[i];
if(ed&) e[ed].d<<=, e[ed+].d<<=;
else e[ed].d<<=, e[ed-].d<<=;
dijkstra();
if(ed&) e[ed].d>>=, e[ed+].d>>=;
else e[ed].d>>=, e[ed-].d>>=;
ans1=max(ans1,dis[n]);
// printf("dis=%d\n",dis[n]);
}
printf("%d\n",ans1-ans0);
return ;
}
洛谷 2176 [USACO14FEB]路障Roadblock的更多相关文章
- 洛谷——P2176 [USACO14FEB]路障Roadblock
P2176 [USACO14FEB]路障Roadblock 题目描述 每天早晨,FJ从家中穿过农场走到牛棚.农场由 N 块农田组成,农田通过 M 条双向道路连接,每条路有一定长度.FJ 的房子在 1 ...
- 洛谷—— P2176 [USACO14FEB]路障Roadblock
https://www.luogu.org/problem/show?pid=2176 题目描述 每天早晨,FJ从家中穿过农场走到牛棚.农场由 N 块农田组成,农田通过 M 条双向道路连接,每条路有一 ...
- 洛谷 P2176 [USACO14FEB]路障Roadblock
题目描述 每天早晨,FJ从家中穿过农场走到牛棚.农场由 N 块农田组成,农田通过 M 条双向道路连接,每条路有一定长度.FJ 的房子在 1 号田,牛棚在 N 号田.没有两块田被多条道路连接,以适当的路 ...
- [USACO14FEB]路障Roadblock
题目:洛谷P2176. 题目大意:有n个点m条无向边,一个人要从1走到n,他会走最短路.现在可以让一条边的长度翻倍,求翻倍后这个人要多走多少距离. 解题思路:首先可以知道,翻倍肯定是在最短路上的某条边 ...
- 洛谷 P3102 [USACO14FEB]秘密代码Secret Code 解题报告
P3102 [USACO14FEB]秘密代码Secret Code 题目描述 Farmer John has secret message that he wants to hide from his ...
- 洛谷P3102 [USACO14FEB]秘密代码Secret Code
P3102 [USACO14FEB]秘密代码Secret Code 题目描述 Farmer John has secret message that he wants to hide from his ...
- 洛谷——P2865 [USACO06NOV]路障Roadblocks
P2865 [USACO06NOV]路障Roadblocks 题目描述 Bessie has moved to a small farm and sometimes enjoys returning ...
- 洛谷 P2237 [USACO14FEB]自动完成Auto-complete
P2237 [USACO14FEB]自动完成Auto-complete 题目描述 Bessie the cow has a new cell phone and enjoys sending text ...
- 洛谷 P3102 [USACO14FEB]秘密代码Secret Code
P3102 [USACO14FEB]秘密代码Secret Code 题目描述 Farmer John has secret message that he wants to hide from his ...
随机推荐
- python-----利用filecmp删除重复文件
以下代码素材自取:链接:https://pan.baidu.com/s/1fL17RjKyGjpvpeeUFONCaQ 提取码:zgiw # coding:utf-8 import os impor ...
- python 两个文件夹里的文件名对比
比如需要一个xml对应一个jpg时,有时候会不小心少了其中一个文件,这时可以用以下代码比较缺少的是哪个文件: # -*- coding: utf-8 -*- import os path1 = r'. ...
- AT2004 Anticube
https://www.zybuluo.com/ysner/note/1304774 题面 给定\(n\)个数\(s_i\),要求从中选出最多的数,满足任意两个数之积都不是完全立方数. \(n\leq ...
- centos7的systemd
系统启动流程 POST --> Boot Sequence --> Bootloader --> kernel+initramfs(initrd) --> rootfs --& ...
- P4971 断罪者
传送门 首先,不难看出可以给每个集合开一个可并堆,然后乱搞就可以了 主要的问题就是将罪恶值清零和减少罪恶值该怎么搞 罪恶值清零可以直接找到这个节点然后把值变为零,再把它的左右儿子分别并到这个节点所在的 ...
- Storm概念学习系列之storm的定时任务
不多说,直接上干货! 至于为什么,有storm的定时任务.这个很简单.但是,这个在工作中非常重要! 假设有如下的业务场景 这个spoult源源不断地发送数据,boilt呢会进行处理.然后呢,处理后的结 ...
- Ubuntu 16.04安装Kate文本编辑工具
Kate支持很多语言,比如NASM,比SBL3低那么一点,但是比Gedit好. 安装: sudo apt-get install kate 启动: 额外配置: 1.安装Kwrite sudo apt- ...
- 诊断Java中的内存泄露
每次我怀疑有内存泄漏时,我都要翻箱倒柜找这些命令.所以,这里总结一下以备后用: 首先,我用下面的命令监视进程: 1 while ( sleep 1 ) ; do ps -p $PID -o %cpu, ...
- json-server模拟REST API
介绍 可以开启一个服务前端使用 安装 npm i json-server -g 使用 $ json-server db.json -p 3000 // 在文件目录下执行上述命令,开放一个端口3000的 ...
- PHP——基本使用(一)
Apache安装与配置 install 下载地址:https://www.apachelounge.com/download/,选择2.4.33版本64位 将程序解压到一个英文目录下,以管理身份打开c ...