洛谷 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 ...
随机推荐
- 第十周 Leetcode 546. Remove Boxes (HARD) 记忆化搜索
Leetcode546 给定一个整数序列,每次删除其中连续相等的子序列,得分为序列长度的平方 求最高得分. dp方程如下: memo[l][r][k] = max(memo[l][r][k], dfs ...
- mysql数据类型和java对应表(copy)
[说明] 资料来自:http://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-type-conversions.html My ...
- xshell、xftp最新版下载方法
https://www.netsarang.com/download/main.html 登录邮箱打开第一个下载地址进行下载
- 题解报告:poj 1426 Find The Multiple(bfs、dfs)
Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose ...
- 设置VMWare虚拟机使拷贝虚拟机后固定原有的IP地址
VMWare中已经安装并设置好的虚拟机在拷贝给别人后,再次打开该虚拟机时原有自动获取的IP地址将会变化,那么原有根据该IP地址进行的设置均将失效,还需要重新设置,比较麻烦,经过百度查询原来可以将虚拟机 ...
- Oracle 参考脚本
一.创建物化视图 --新建表空间 CREATE TABLESPACE MLOG_TBS LOGGING DATAFILE 'mlog_tbs.dbf' SIZE 32M AUTOEXTEND ON N ...
- 利用 nodeJS 搭建一个简单的Web服务器(转)
下面的代码演示如何利用 nodeJS 搭建一个简单的Web服务器: 1. 文件 WebServer.js: //-------------------------------------------- ...
- 每天学点Linux命令:倒叙打印文件第二行的前100个大写字母
sed -n | rev 处理第二行 grep:提取大写字母 o: 不显示非结果 tr:删除换行 Cut:截取1-100个字符 rev:逆序 断断续续搞了好长时间. ...
- SVN服务器搭建 内网可用外网不可用的问题
检查1:内网端口映射到了外网端口,这样外网才能够访问到 映射的方式有两种: 1.通过路由器的虚拟服务器功能,网上一搜一大把. 2.将路由器的DMZ功能开启,并把DMZ主机设置为目标计算机. 检查2:S ...
- Linux系统资源监控--linux命令、nmon和spotlight
前言: 系统资源监控一般监控系统的CPU,内存,磁盘和网络.系统分为windows和Linux.本篇主要记录Linux. Linux系统资源监控常用命令及工具 一.常用命令:top.free.iost ...