Luogu P3106 [USACO14OPEN]GPS的决斗Dueling GPS's(最短路)
P3106 [USACO14OPEN]GPS的决斗Dueling GPS's
题意
题目描述
Farmer John has recently purchased a new car online, but in his haste he accidentally clicked the "Submit" button twice when selecting extra features for the car, and as a result the car ended up equipped with two GPS navigation systems! Even worse, the two systems often make conflicting decisions about the route that FJ should take.
The map of the region in which FJ lives consists of \(N\) intersections ($2 \leq N \leq 10,000$) and \(M\) directional roads ($1 \leq M \leq 50,000$). Road \(i\) connects intersections \(A_i(1 \leq A_i \leq N)\) and \(B_i(1 \leq B_i \leq N)\). Multiple roads could connect the same pair of intersections, and a bi-directional road (one permitting two-way travel) is represented by two separate directional roads in opposite orientations. FJ's house is located at intersection $1$, and his farm is located at intersection \(N\). It is possible to reach the farm from his house by traveling along a series of directional roads.
Both GPS units are using the same underlying map as described above; however, they have different notions for the travel time along each road. Road \(i\) takes \(P_i\) units of time to traverse according to the first GPS unit, and \(Q_i\) units of time to traverse according to the second unit (each travel time is an integer in the range $1 \cdots 100,000$).
FJ wants to travel from his house to the farm. However, each GPS unit complains loudly any time FJ follows a road (say, from intersection \(X\) to intersection \(Y\)) that the GPS unit believes not to be part of a shortest route from X to the farm (it is even possible that both GPS units can complain, if FJ takes a road that neither unit likes).
Please help FJ determine the minimum possible number of total complaints he can receive if he chooses his route appropriately. If both GPS units complain when FJ follows a road, this counts as \(+2\) towards the total.
给你一个$N$个点的有向图,可能有重边.
有两个$GPS$定位系统,分别认为经过边$i$的时间为$P_i$和$Q_i$.
每走一条边的时候,如果一个系统认为走的这条边不是它认为的最短路,就会受到警告一次$T$
两个系统是分开警告的,就是说当走的这条边都不在两个系统认为的最短路范围内,就会受到$2$次警告.
如果边$(u,v)$不在$u$到$n$的最短路径上,这条边就受到一次警告,求从$1$到$n$最少受到多少次警告。
输入输出格式
输入格式:
Line $1$: The integers \(N\) and \(M\).
Line \(i\) describes road \(i\) with four integers: \(A_i \ B_i \ P_i \ Q_i\).
输出格式:
Line $1$: The minimum total number of complaints FJ can receive if he routes himself from his house to the farm optimally.
输入输出样例
输入样例:
5 7
3 4 7 1
1 3 2 20
1 4 17 18
4 5 25 3
1 2 10 1
3 5 4 14
2 4 6 5
输出样例:
1
说明
There are $5$ intersections and $7$ directional roads. The first road connects from intersection $3$ to intersection $4$; the first GPS thinks this road takes $7$ units of time to traverse, and the second GPS thinks it takes $1$ unit of time, etc.
If FJ follows the path $1$ → $2$ → $4$ → $5$, then the first GPS complains on the $1$ → $2$ road (it would prefer the $1$ → $3$ road instead). However, for the rest of the route $2$ → $4$ → $5$, both GPSs are happy, since this is a shortest route from $2$ to $5$ according to each GPS.
思路
太fAKe了。 --Mercury
我们发现,无论走到哪个点,$GPS$的警告都是使用到终点$n$的最短路径来判断的,所以我们先预处理出两台$GPS$到终点$n$的最短路长度$dis1,dis2$,这可以用反向跑最短路来实现。然后对于一条边$(u,v)$,如果$dis1[u]+len1(u,v)==dis1[v]$,那么第一台$GPS$是不会警告的;同样,如果$dis2[u]+len2(u,v)==dis2[v]$,那么第二台$GPS$是不会警告的。那么我们以此来改变每条边的边权为$GPS$警告的次数,然后再跑一遍最短路,就可以得到答案了。
AC代码
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MAXN=1e4+5;
const LL MAXM=5e4+5;
LL n,m,dis[MAXN],dis1[MAXN],dis2[MAXN];
LL cnt,top[MAXN],to[MAXM],len[MAXM],nex[MAXM];
LL cnt1,top1[MAXN],to1[MAXM],len1[MAXM],nex1[MAXM];
LL cnt2,top2[MAXN],to2[MAXM],len2[MAXM],nex2[MAXM];
bool vis[MAXN];
inline LL read()
{
LL re=0;char ch=getchar();
while(!isdigit(ch)) ch=getchar();
while(isdigit(ch)) re=(re<<3)+(re<<1)+ch-'0',ch=getchar();
return re;
}
inline void add_edge(LL x,LL y,LL z){to[++cnt]=y,len[cnt]=z,nex[cnt]=top[x],top[x]=cnt;}
inline void add_edge1(LL x,LL y,LL z){to1[++cnt1]=y,len1[cnt1]=z,nex1[cnt1]=top1[x],top1[x]=cnt1;}
inline void add_edge2(LL x,LL y,LL z){to2[++cnt2]=y,len2[cnt2]=z,nex2[cnt2]=top2[x],top2[x]=cnt2;}
void SPFA()
{
memset(dis,0x3f,sizeof dis);
dis[1]=0;
queue<LL>Q;
Q.push(1);
while(!Q.empty())
{
LL now=Q.front();Q.pop();
vis[now]=false;
for(int i=top[now];i;i=nex[i])
if(dis[to[i]]>dis[now]+len[i])
{
dis[to[i]]=dis[now]+len[i];
if(!vis[to[i]])
{
vis[to[i]]=true;
Q.push(to[i]);
}
}
}
}
void SPFA1()
{
memset(dis1,0x3f,sizeof dis1);
dis1[n]=0;
queue<LL>Q;
Q.push(n);
while(!Q.empty())
{
LL now=Q.front();Q.pop();
vis[now]=false;
for(int i=top1[now];i;i=nex1[i])
if(dis1[to1[i]]>dis1[now]+len1[i])
{
dis1[to1[i]]=dis1[now]+len1[i];
if(!vis[to1[i]])
{
vis[to1[i]]=true;
Q.push(to1[i]);
}
}
}
}
void SPFA2()
{
memset(dis2,0x3f,sizeof dis2);
dis2[n]=0;
queue<LL>Q;
Q.push(n);
while(!Q.empty())
{
LL now=Q.front();Q.pop();
vis[now]=false;
for(int i=top2[now];i;i=nex2[i])
if(dis2[to2[i]]>dis2[now]+len2[i])
{
dis2[to2[i]]=dis2[now]+len2[i];
if(!vis[to2[i]])
{
vis[to2[i]]=true;
Q.push(to2[i]);
}
}
}
}
int main()
{
n=read(),m=read();
while(m--)
{
int x=read(),y=read(),z1=read(),z2=read();
add_edge1(y,x,z1);
add_edge2(y,x,z2);
}
SPFA1(),SPFA2();
for(int i=1;i<=n;i++)
for(int j=top1[i];j;j=nex1[j])
{
int l=2;
if(dis1[to1[j]]==dis1[i]+len1[j]) l--;
if(dis2[to2[j]]==dis2[i]+len2[j]) l--;
add_edge(to1[j],i,l);
}
SPFA();
printf("%lld",dis[n]);
return 0;
}
Luogu P3106 [USACO14OPEN]GPS的决斗Dueling GPS's(最短路)的更多相关文章
- BZOJ 3538 == 洛谷 P3106 [USACO14OPEN]GPS的决斗Dueling GPS's
P3106 [USACO14OPEN]GPS的决斗Dueling GPS's 题目描述 Farmer John has recently purchased a new car online, but ...
- 洛谷 3106 [USACO14OPEN]GPS的决斗Dueling GPS's 3720 [AHOI2017初中组]guide
[题解] 这两道题是完全一样的. 思路其实很简单,对于两种边权分别建反向图跑dijkstra. 如果某条边在某一种边权的图中不是最短路上的边,就把它的cnt加上1.(这样每条边的cnt是0或1或2,代 ...
- [USACO14OPEN]GPS的决斗Dueling GPS's
题目概况 题目描述 给你一个\(N\)个点的有向图,可能有重边. 有两个\(GPS\)定位系统,分别认为经过边\(i\)的时间为\(P_i\),和\(Q_i\). 每走一条边的时候,如果一个系统认为走 ...
- 2018.07.22 洛谷P3106 GPS的决斗Dueling GPS's(最短路)
传送门 图论模拟题. 这题直接写3个(可以压成一个)spfa" role="presentation" style="position: relative;&q ...
- USACO Dueling GPS's
洛谷 P3106 [USACO14OPEN]GPS的决斗Dueling GPS's 洛谷传送门 JDOJ 2424: USACO 2014 Open Silver 2.Dueling GPSs JDO ...
- BZOJ3538: [Usaco2014 Open]Dueling GPS
3538: [Usaco2014 Open]Dueling GPS Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 59 Solved: 36[Subm ...
- GPS校时器,GPS时钟装置,NTP网络时间服务器
GPS校时器,GPS时钟装置,NTP网络时间服务器 GPS校时器,GPS时钟装置,NTP网络时间服务器 GPS校时器,GPS时钟装置,NTP网络时间服务器 GPS校时器,GPS时钟装置,NTP网络时间 ...
- 部署-GPS授时系统:GPS授时系统
ylbtech-部署-GPS授时系统:GPS授时系统 GPS授时系统是针对自动化系统中的计算机.控制装置等进行校时的高科技产品,GPS授时产品它从GPS卫星上获取标准的时间信号,将这些信息通过各种接口 ...
- P3106 [USACO14OPEN]GPS的决斗(最短路)
化简:够简的了.....但是!翻译绝对有锅. 这个最短路是从n到每个点的单源最短路,也就是最短路径树. 那么,思路就很明确了.建两个图,然后跑两边SPFA,记录下最短路径. 然后,对于两点之间的边,如 ...
随机推荐
- Java A*算法搜索无向图最短路径
网上看了很多别人写的A*算法,都是针对栅格数据进行处理,每次向外扩展都是直接八方向或者四方向,这样利于理解.每次移动当前点,gCost也可以直接设置成横向10斜向14. 但是当我想处理一个连续的数据集 ...
- 面试系列13 redis都有哪些数据类型
(1)string 这是最基本的类型了,没啥可说的,就是普通的set和get,做简单的kv缓存 (2)hash 这个是类似map的一种结构,这个一般就是可以将结构化的数据,比如一个对象(前提是这个对象 ...
- 不用winio直接用c#函数实现模拟键盘
原理来自: http://blog.sina.com.cn/s/blog_71921a8e0100olaw.html /// <summary> /// 导入模拟键盘的方法 /// &l ...
- leetcode-03-二叉树的锯齿层次遍历
题目描述: 方法一: # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.va ...
- Docker这个新软件究竟是用来干嘛的???
http://dockone.io/article/378 尝试新软件 对开发者而言,每天会催生出的各式各样的新技术都需要尝试,然而开发者却不太可能为他们一一搭建好环境并进行测试.时间非常宝贵,正是得 ...
- System.Web.Mvc.RedirectToRouteResult.cs
ylbtech-System.Web.Mvc.RedirectToRouteResult.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutr ...
- Python(三)基础篇之「模块&面向对象编程」
[笔记]Python(三)基础篇之「模块&面向对象编程」 2016-12-07 ZOE 编程之魅 Python Notes: ★ 如果你是第一次阅读,推荐先浏览:[重要公告]文章更新. ...
- JSON高亮格式化页面显示
高亮CSS定义: <style type="text/css"> pre {outline: 1px solid #ccc; padding: 5px; margin: ...
- Python运算符,逻辑运算
运算符 计算机可以进行的运算有很多种,可不只加减乘除这么简单,运算按种类可分为算数运算.比较运算.逻辑运算.赋值运算.成员运算.身份运算.位运算,今天我们暂只学习算数运算.比较运算.逻辑运算.赋值运算 ...
- Chapter 3 树与二叉树
Chapter 3 树与二叉树 1- 二叉树 主要性质: 1 叶子结点数 = 度为2的结点数 + 1 2 二叉树第i层上最多有 (i≥1)个结点 3 深度为k的二叉树最多有 个结点 ...