洛谷 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.他们有 ...
随机推荐
- javascript匿名方法
首先,看一段很有意思的代码: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> < ...
- HTML中a标签自动识别电话、邮箱
HTML中a标签自动识别电话.邮箱 联系电话:<a href="tel:010-88888888">010-88888888</a><br> 联 ...
- Android偏好设置(1)概述和Preferences简介
1.Overview Instead of using View objects to build the user interface, settings are built using vario ...
- C#模版学习研究
原文链接1 原文链接2 using System; using System.Collections.Generic; using System.Text; using T = System.By ...
- java数组实现买彩票(二个一维数组的比较思想)
/** 设计一个程序,模拟从彩球池里随机抽取5个彩球(彩球池里一共有11个彩球,编号为1~11), 要求在控制台打印出这5个被取出来的彩球的编号(注意编号不能重复). 思路: 1.创建一个int类型的 ...
- 定时清除 /var/log/massage 下的信息脚本文件
定时清除 /var/log/massage 下的信息脚本 #!/bin/sh #Date: 0:07 #Author: Xiaodong #Mail: 990974238@qq.com #Puncti ...
- Oracle中的序列
序列是什么? 序列是用来生成唯一.连续的整数的数据库对象.序列通常用来自动生成主键或唯一键的值.序列可以按升序排列,也可以按照降序排列. 其实Oracle中的序列和MySQL中的自增长差不多一个意思. ...
- Caused by: javax.el.PropertyNotFoundException: Property 'product' not found on type java.lang.String
今天在JSP利用EL表达式取值报了 "javax.el.PropertyNotFoundException”,经过debug和打印将问题定位到这段代码: HTML应该是没啥问题,看提示在ja ...
- VUE学习,is 特性,转载来源:https://segmentfault.com/q/1010000007205176/
- schtasks /create 计划任务 中文路径 名字都要加“” 子命令 /tn /tr 前面要空格 否则会出错
echo off echo. 清空连接 net use * /del /y echo. 连接 net use \\192.168.1.2\人力资源部\考勤\考勤数据-小莫提供 "密码&quo ...