[USACO14OPEN] 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 <= N <= 10,000) and M directional roads (1 <= M <= 50,000). Road i connects intersections A_i (1 <= A_i <= N) and B_i (1 <= B_i <= 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..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的时间为Pi,和Qi.
每走一条边的时候,如果一个系统认为走的这条边不是它认为的最短路,就会受到警告一次T 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.
感觉好神
先求两次最短路,然后新建图只要不在一条最短路上就边w++,再求最短路
注意GPS认为的最短路是到达n,所以前两次反向建图
PS:沙茶的数组M写成N
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int N=1e4+,M=5e4+,INF=1e9+;
typedef long long ll;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int n,m,u[M],v[M],w1[M],w2[M];
struct edge{
int u,v,ne,w1,w2;
}e[M];
struct edge2{
int v,ne,w;
}en[M];
int h[N],cnt=;
inline void ins(int u,int v,int w1,int w2){
cnt++;
e[cnt].u=h[u];
e[cnt].v=v;e[cnt].ne=h[u];h[u]=cnt;e[cnt].w1=w1;e[cnt].w2=w2;
}
inline void add(int u,int v,int w){
cnt++;
en[cnt].v=v;en[cnt].w=w;en[cnt].ne=h[u];h[u]=cnt;
}
int q[N],head=,tail=,inq[N],d1[N],d2[N],d3[N];
inline void lop(int &x){if(x==N-) x=;}
void spfa1(int d[]){
for(int i=;i<=n;i++) d[i]=INF;
memset(inq,,sizeof(inq));
head=tail=;
q[tail++]=n;inq[n]=;d[n]=;
while(head!=tail){
int u=q[head++];inq[u]=;lop(head);
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v,w=e[i].w1;
if(d[v]>d[u]+w){
d[v]=d[u]+w;
if(!inq[v]){q[tail++]=v;inq[v]=;lop(tail);}
}
}
}
}
void spfa2(int d[]){
for(int i=;i<=n;i++) d[i]=INF;
memset(inq,,sizeof(inq));
head=tail=;
q[tail++]=n;inq[n]=;d[n]=;
while(head!=tail){
int u=q[head++];inq[u]=;lop(head);
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v,w=e[i].w2;
if(d[v]>d[u]+w){
d[v]=d[u]+w;
if(!inq[v]){q[tail++]=v;inq[v]=;lop(tail);}
}
}
}
} void buildGraph(){
cnt=;
memset(h,,sizeof(h));
for(int i=;i<=m;i++){
int w=;
if(d1[u[i]]<d1[v[i]]+w1[i]) w++;
if(d2[u[i]]<d2[v[i]]+w2[i]) w++;
add(u[i],v[i],w);//printf("add %d %d %d\n",u,v,w);
}
}
void spfa3(int d[]){
for(int i=;i<=n;i++) d[i]=INF;
memset(inq,,sizeof(inq));
head=tail=;
q[tail++]=;inq[]=;d[]=;
while(head!=tail){
int u=q[head++];inq[u]=;lop(head);//printf("spfa3 %d\n",u);
for(int i=h[u];i;i=en[i].ne){
int v=en[i].v,w=en[i].w;
if(d[v]>d[u]+w){
d[v]=d[u]+w;
if(!inq[v]){q[tail++]=v;inq[v]=;lop(tail);}
}
}
}
}
int main(){
n=read();m=read();
for(int i=;i<=m;i++){
u[i]=read();v[i]=read();w1[i]=read();w2[i]=read();
ins(v[i],u[i],w1[i],w2[i]);
}
spfa1(d1);
spfa2(d2);
buildGraph();
spfa3(d3);
printf("%d",d3[n]);
}
[USACO14OPEN] Dueling GPS's[最短路建模]的更多相关文章
- Luogu P3106 [USACO14OPEN]GPS的决斗Dueling GPS's(最短路)
P3106 [USACO14OPEN]GPS的决斗Dueling GPS's 题意 题目描述 Farmer John has recently purchased a new car online, ...
- BZOJ 3538 == 洛谷 P3106 [USACO14OPEN]GPS的决斗Dueling GPS's
P3106 [USACO14OPEN]GPS的决斗Dueling GPS's 题目描述 Farmer John has recently purchased a new car online, but ...
- 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 ...
- [USACO14OPEN]GPS的决斗Dueling GPS's
题目概况 题目描述 给你一个\(N\)个点的有向图,可能有重边. 有两个\(GPS\)定位系统,分别认为经过边\(i\)的时间为\(P_i\),和\(Q_i\). 每走一条边的时候,如果一个系统认为走 ...
- 洛谷 3106 [USACO14OPEN]GPS的决斗Dueling GPS's 3720 [AHOI2017初中组]guide
[题解] 这两道题是完全一样的. 思路其实很简单,对于两种边权分别建反向图跑dijkstra. 如果某条边在某一种边权的图中不是最短路上的边,就把它的cnt加上1.(这样每条边的cnt是0或1或2,代 ...
- 2018.07.22 洛谷P3106 GPS的决斗Dueling GPS's(最短路)
传送门 图论模拟题. 这题直接写3个(可以压成一个)spfa" role="presentation" style="position: relative;&q ...
- POJ 1062 昂贵的聘礼 【带限制的最短路/建模】
年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请求酋长降低要求.酋长说:" ...
- 【BZOJ】3538: [Usaco2014 Open]Dueling GPS(spfa)
http://www.lydsy.com/JudgeOnline/problem.php?id=3538 题意不要理解错QAQ,是说当前边(u,v)且u到n的最短距离中包含这条边,那么这条边就不警告. ...
随机推荐
- 尝试加载 Oracle 客户端库时引发 BadImageFormatException
尝试加载 Oracle 客户端库时引发 BadImageFormatException 工程师给计算机诊断,就像医生给病人诊断一样,很多同样的症状,可能是由多种截然不同的原因导致的. 最近进行C# ...
- 【Java每日一题】20161216
package Dec2016; import java.util.ArrayList; import java.util.List; public class Ques1216 { public s ...
- JS与Jquery区别
很多人对JS和JQuery很容易搞混淆,今天我们就相比学习下: 加载区别: var myfunction(){}; JS:1.window.onload=function(){} 2.<body ...
- 关于Java语言和面向对象记录
本科时常用的c语言是面向过程的语言,而Java是面向对象的语言 Java语言的11个关键术语 简单性.可移植性.面向对象.分布式.高性能.解释型.健壮性.多线程.安全性.动态性.体系结构中立 面向对象 ...
- proxool+hsqldb数据库
使用hsqldb数据库,开发环境eclipse,jdk8.0 Dsqldb数据库 1.下载包:hsqldb.jar 2.新建项目,复制到项目WebContent -> WEB -> INF ...
- Matlab 之 数据元素访问
Matlab的含义是矩阵实验室,其特征之一就是数据的向量化操作,借此提升软件运行效率.那么,必然会涉及数据元素的访问.Matlab主要支持下面一些形式的访问: (1)array-inde: A(i) ...
- 高性能 Socket 组件 HP-Socket v3.1.3 正式发布
HP-Socket 是一套通用的高性能 Windows Socket 组件,提供服务端组件(IOCP 模型)和客户端组件(Event Select 模型),广泛适用于 Windows 平台的 TCP/ ...
- Hibernate(十)__缓存机制
为什么需要缓存? 缓存的作用主要用来提高性能,可以简单的理解成一个Map: 使 用缓存涉及到三个操作:把数据放入缓存.从缓存中获取数据. 删除缓存中的无效数据. 从上图看出: 当我们去查询对象的时候, ...
- 事件流之事件冒泡与事件捕获<JavaScript高级程序设计>学习笔记
1.事件流 浏览器开发团队遇到一个很有意思问题:页面的那一部分会拥有特定的事件? 对于理解这个问题您可以想象画在一张纸上的一组同心圆,如果你把手指放在圆心上,那么你的手指指向的其实不是一个圆,而是纸上 ...
- 前后端分离之前端项目构建(grunt+require+angular)
前言 前段时间做了一个项目,前端开发页面,然后把代码给到后端同学,后端同学通过vm再来渲染页面.后来才发现,这种方式简直是太low了,因为前端代码在服务端同学那里,每次前端需要更改的时候都需要去到服务 ...