洛谷 3106 [USACO14OPEN]GPS的决斗Dueling GPS's 3720 [AHOI2017初中组]guide

【题解】
这两道题是完全一样的。
思路其实很简单,对于两种边权分别建反向图跑dijkstra。
如果某条边在某一种边权的图中不是最短路上的边,就把它的cnt加上1。(这样每条边的cnt是0或1或2,代表经过这条边GPS报警的次数)
最后用每条边的cnt作为边权建图,跑dijkstra即可。
判断某条边是不是最短路上的边:建反向图,以n为起点跑dijkstra,如果某条边(u,v)满足dis[v]=dis[u]+w,那么这条边是u到n的最短路上的边。
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
#define rg register
#define N 200010
#define M 500010
using namespace std;
int n,m,tot,fa,son,last[N],dis[N],pos[N];
struct edge{int to,pre,dis;}e[M];
struct rec{int u,v,d1,d2,d3;}r[M];
struct heap{int p,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].p],pos[h[x].p]),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].p],pos[h[son].p]),x=son;
else return;
}
}
inline void dijkstra(int x){
for(rg int i=;i<=n;i++) dis[i]=1e9;
h[tot=pos[x]=]=(heap){x,dis[x]=};
while(tot){
int now=h[].p; pos[h[tot].p]=; 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].dis){
dis[to]=dis[now]+e[i].dis;
if(!pos[to]) h[pos[to]=++tot]=(heap){to,dis[to]};
else h[pos[to]].d=dis[to];
up(pos[to]);
}
}
}
inline void Pre(){
memset(last,,sizeof(last));
memset(pos,,sizeof(pos));
tot=;
}
inline void work(){
for(rg int i=,u,v,d;i<=m;i++){
u=r[i].u,v=r[i].v,d=r[i].d1;
e[++tot]=(edge){u,last[v],d}; last[v]=tot;
}
dijkstra(n);
for(rg int i=;i<=m;i++)
if(dis[r[i].u]!=dis[r[i].v]+r[i].d1) r[i].d3++;
Pre();
for(rg int i=,u,v,d;i<=m;i++){
u=r[i].u,v=r[i].v,d=r[i].d2;
e[++tot]=(edge){u,last[v],d}; last[v]=tot;
}
dijkstra(n);
for(rg int i=;i<=m;i++)
if(dis[r[i].u]!=dis[r[i].v]+r[i].d2) r[i].d3++;
Pre();
for(rg int i=,u,v;i<=m;i++){
u=r[i].u,v=r[i].v;
e[++tot]=(edge){u,last[v],r[i].d3}; last[v]=tot;
}
dijkstra(n);
}
int main(){
n=read(); m=read();
for(rg int i=;i<=m;i++)
r[i].u=read(),r[i].v=read(),r[i].d1=read(),r[i].d2=read();
work();
printf("%d\n",dis[]);
return ;
}
洛谷 3106 [USACO14OPEN]GPS的决斗Dueling GPS's 3720 [AHOI2017初中组]guide的更多相关文章
- BZOJ 3538 == 洛谷 P3106 [USACO14OPEN]GPS的决斗Dueling GPS's
P3106 [USACO14OPEN]GPS的决斗Dueling GPS's 题目描述 Farmer John has recently purchased a new car online, but ...
- Luogu P3106 [USACO14OPEN]GPS的决斗Dueling GPS's(最短路)
P3106 [USACO14OPEN]GPS的决斗Dueling GPS's 题意 题目描述 Farmer John has recently purchased a new car online, ...
- 2018.07.22 洛谷P3106 GPS的决斗Dueling GPS's(最短路)
传送门 图论模拟题. 这题直接写3个(可以压成一个)spfa" role="presentation" style="position: relative;&q ...
- [USACO14OPEN]GPS的决斗Dueling GPS's
题目概况 题目描述 给你一个\(N\)个点的有向图,可能有重边. 有两个\(GPS\)定位系统,分别认为经过边\(i\)的时间为\(P_i\),和\(Q_i\). 每走一条边的时候,如果一个系统认为走 ...
- 洛谷 U45568 赌神:决斗
题目描述 \mathcal{tomoo}tomoo决定与\mathcal{CYJian}CYJian进行决斗! 已知\mathcal{tomoo}tomoo有\mathcal{N}N张扑克牌,每张扑克 ...
- USACO Dueling GPS's
洛谷 P3106 [USACO14OPEN]GPS的决斗Dueling GPS's 洛谷传送门 JDOJ 2424: USACO 2014 Open Silver 2.Dueling GPSs JDO ...
- 洛谷 P2299 Mzc和体委的争夺战
洛谷 P2299 Mzc和体委的争夺战 题目背景 mzc与djn第四弹. 题目描述 mzc家很有钱(开玩笑),他家有n个男家丁(做过前三弹的都知道).但如此之多的男家丁吸引来了我们的体委(矮胖小伙), ...
- 洛谷1640 bzoj1854游戏 匈牙利就是又短又快
bzoj炸了,靠离线版题目做了两道(过过样例什么的还是轻松的)但是交不了,正巧洛谷有个"大牛分站",就转回洛谷做题了 水题先行,一道傻逼匈牙利 其实本来的思路是搜索然后发现写出来类 ...
- 洛谷P1352 codevs1380 没有上司的舞会——S.B.S.
没有上司的舞会 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description Ural大学有N个职员,编号为1~N.他们有 ...
随机推荐
- 【底层原理】高级开发必须懂的"字节对齐"
认识字节对齐之前,假定int(4Byte),char(1Byte),short(2Byte) 认识字节对齐 先看段代码: struct Data1 { char a; int b; short c; ...
- 【原创】CentOS 6.5 中安装 Mysql 5.6,并远程连接Mysql
ι 版权声明:本文为博主原创文章,未经博主允许不得转载. 1.在安装CentOS时,若选择的是Basic Server(可支持J2EE开发),则新安装好的CentOS系统中默认是已经安装了一个mysq ...
- centos mysql数据库忘记密码修改
1.vim /etc/my.cnf 2.在[mysqld]中添加 skip-grant-tables 例如: [mysqld]skip-grant-tablesdatadir=/var/lib/mys ...
- ACM_送气球(规律题)
送气球 Time Limit: 2000/1000ms (Java/Others) Problem Description: 为了奖励近段时间辛苦刷题的ACMer,会长决定给正在机房刷题的他们送气球. ...
- Java中的流(3)字符流-Reader和Writer
java中提供了处理以16位的Unicode码表示的字符流的类,即以Reader和Writer 为基类派生出的一系列类. 1.Reader和Writer 这两个类是抽象类,只是提供了一系列用于字符 ...
- 438 Find All Anagrams in a String 找出字符串中所有的变位词
详见:https://leetcode.com/problems/find-all-anagrams-in-a-string/description/ C++: class Solution { pu ...
- 增大PHP允许上传的文件大小;解决POST Content-Length exceeds the limit
在php.ini中: upload_max_filesize = 1000M ;1GB post_max_size = 1000M 然后重启apache 参考链接
- Oracle中的表空间
表空间是什么? Oracle数据库包含逻辑结构和物理结构. 数据库的物理结构是指构成数据库的一组操作系统文件. 数据库的逻辑结构是指描述数据组织方式的一组逻辑概念及它们之间的关系. 表空间是数据库数据 ...
- php debug/phpstorm调试
apache+phpstorm调试php代码,修改php.ini配置文件开启调试,没有以下代码加上即可, [XDebug]zend_extension="C:\php\php-7.0.12- ...
- 纯css实现的三级水平导航菜单
vscode练习使用开发纯css的三级水平导航菜单.先上图: 1.html5布局 <html> <head> <meta charset="UTF-8" ...