洛谷 P3106 [USACO14OPEN]GPS的决斗Dueling GPS's

洛谷传送门

JDOJ 2424: USACO 2014 Open Silver 2.Dueling GPSs

JDOJ传送门

Description

Problem 2: Dueling GPS's [Brian Dean, 2014]

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.

Input

* Line 1: The integers N and M.

Line i describes road i with four integers: A_i B_i P_i Q_i.

Output

* Line 1: The minimum total number of complaints FJ can receive if he

routes himself from his house to the farm optimally.

Sample Input

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

Sample Output

1

HINT

INPUT DETAILS:

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.

OUTPUT DETAILS:

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.

题目大意:

给你一个N个点的有向图,可能有重边.

有两个GPS定位系统,分别认为经过边i的时间为Pi,和Qi.

每走一条边的时候,如果一个系统认为走的这条边不是它认为的最短路,就会受到警告一次T T

两个系统是分开警告的,就是说当走的这条边都不在两个系统认为的最短路范围内,就会受到2次警告.

如果边(u,v)不在u到n的最短路径上,这条边就受到一次警告,求从1到n最少受到多少次警告。

题解:

3遍SPFA 打到我吐血。

我不想讲了。

#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
#define inf 0x3f3f3f
using namespace std;
struct edge{
int w,to,next;
}e1[201100];
edge e2[201100];
edge e3[201100];
int head1[101100],head2[101100],head3[101100];
int d1[101100],d2[101100],d3[101100];
int f1[101100],f2[101100];
int n,en1,en2,en3,m;
void add1(int v,int u,int w){
en1++;
e1[en1].to=v;
e1[en1].w=w;
e1[en1].next=head1[u];
head1[u]=en1;
}
void add2(int v,int u,int w){
en2++;
e2[en2].to=v;
e2[en2].w=w;
e2[en2].next=head2[u];
head2[u]=en2;
}
void add3(int u,int v,int w){
en3++;
e3[en3].to=v;
e3[en3].w=w;
e3[en3].next=head3[u];
head3[u]=en3;
}
void sp1(int s){
queue<int> q;
int inq[20010];
memset(inq,0,sizeof(inq));
for(int i=1;i<=n;i++)d1[i]=inf;
q.push(s);inq[s]=1;d1[s]=0;
while(!q.empty()){
int u=q.front();q.pop();inq[u]=0;
for(int i=head1[u];i;i=e1[i].next){
int v=e1[i].to,w=e1[i].w;
if(d1[v]>d1[u]+w)
{
d1[v]=d1[u]+w;
f1[v]=i;
if(!inq[v]){
inq[v]=1;
q.push(v);
}
}
}
}
}
void sp2(int s){
queue<int> q;
int inq[20010];
memset(inq,0,sizeof(inq));
for(int i=1;i<=n;i++)d2[i]=inf;
q.push(s);inq[s]=1;d2[s]=0;
while(!q.empty()){
int u=q.front();q.pop();inq[u]=0;
for(int i=head2[u];i;i=e2[i].next){
int v=e2[i].to,w=e2[i].w;
if(d2[v]>d2[u]+w)
{
d2[v]=d2[u]+w;
f2[v]=i;
if(!inq[v]){
inq[v]=1;
q.push(v);
}
}
}
}
}
void sp3(int s){
queue<int> q;
int inq[20010];
memset(inq,0,sizeof(inq));
for(int i=1;i<=n;i++)d3[i]=inf;
q.push(s);inq[s]=1;d3[s]=0;
while(!q.empty()){
int u=q.front();q.pop();
for(int i=head3[u];i;i=e3[i].next){
int v=e3[i].to,w=e3[i].w;
if(i==f1[u])w--;
if(i==f2[u])w--;
if(d3[v]>d3[u]+w)
{
d3[v]=d3[u]+w;
if(!inq[v]){
inq[v]=1;
q.push(v);
}
}
}inq[u]=0;
}
}
int main(){
cin>>n>>m;
for(int i=1;i<=m;i++){
int a,b,c,d;
cin>>a>>b>>c>>d;
add1(a,b,c);
add2(a,b,d);
add3(a,b,2);
}
sp1(n);
sp2(n);
sp3(1);
cout<<d3[n];
}

USACO Dueling GPS's的更多相关文章

  1. BZOJ3538: [Usaco2014 Open]Dueling GPS

    3538: [Usaco2014 Open]Dueling GPS Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 59  Solved: 36[Subm ...

  2. BZOJ 3538 == 洛谷 P3106 [USACO14OPEN]GPS的决斗Dueling GPS's

    P3106 [USACO14OPEN]GPS的决斗Dueling GPS's 题目描述 Farmer John has recently purchased a new car online, but ...

  3. Luogu P3106 [USACO14OPEN]GPS的决斗Dueling GPS's(最短路)

    P3106 [USACO14OPEN]GPS的决斗Dueling GPS's 题意 题目描述 Farmer John has recently purchased a new car online, ...

  4. USACO 2014 US Open Dueling GPS's /// SPFA

    题目大意: 给定n个点m条边的有向图 有两个GPS 分别认为 A[i]到B[i] 的一条边的花费是P[i].Q[i] 当当前走的边不是GPS认为的最短路上的边就会被警告 即两个GPS都不认为是最短路上 ...

  5. [USACO14OPEN] Dueling GPS's[最短路建模]

    题目描述 Farmer John has recently purchased a new car online, but in his haste he accidentally clicked t ...

  6. 【BZOJ】3538: [Usaco2014 Open]Dueling GPS(spfa)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3538 题意不要理解错QAQ,是说当前边(u,v)且u到n的最短距离中包含这条边,那么这条边就不警告. ...

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

    [题解] 这两道题是完全一样的. 思路其实很简单,对于两种边权分别建反向图跑dijkstra. 如果某条边在某一种边权的图中不是最短路上的边,就把它的cnt加上1.(这样每条边的cnt是0或1或2,代 ...

  8. [USACO14OPEN]GPS的决斗Dueling GPS's

    题目概况 题目描述 给你一个\(N\)个点的有向图,可能有重边. 有两个\(GPS\)定位系统,分别认为经过边\(i\)的时间为\(P_i\),和\(Q_i\). 每走一条边的时候,如果一个系统认为走 ...

  9. 2018.07.22 洛谷P3106 GPS的决斗Dueling GPS's(最短路)

    传送门 图论模拟题. 这题直接写3个(可以压成一个)spfa" role="presentation" style="position: relative;&q ...

随机推荐

  1. Fink| API| Time与Window

    1. Flink 批处理Api 1.1 Source Flink+kafka是如何实现exactly-once语义的 Flink通过checkpoint来保存数据是否处理完成的状态: 有JobMana ...

  2. 近似计算一个对象在js占用内存

    内存 在很久之前,我就想查看一个对象在JS里占用多少内存了,直到最近由于线上使用了需要计算从服务端传输数据的大小,让这个需求尤为强烈. 预备知识 我们现在使用的js是高级语言,它在内存细节之上建立一个 ...

  3. 第04组 Alpha事后诸葛亮

    一.组长博客:地址 二.Postmortem模板 设想和目标 1.我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 我们要解决的问题是让大学生可以通过福鱼网站将暂时无 ...

  4. How does Chrome Extension crx Downloader work? ——— From crxdown.com

    How does Chrome Extension crx Downloader work? Home >> blog >> How does Chrome Extension ...

  5. 小玩意儿之Gitlab 代码提交日志同步到禅道项目管理系统

    以前都是使用禅道官方推荐的服务器本地扫描的方式,但其实不太方便,需要跟着项目的变化,不断的在配置文件维护项目相应仓库的配置. 然后现在Web Hooks越来越普遍的情况下,想尝试一种新的方式.看了禅道 ...

  6. 物联网架构成长之路(34)-物联网数据可视化grafana展示

    一.前言 前面介绍了利用后台业务服务器监听EMQ的Topic,作为EMQ的一个客户端方式来保存数据.然后将数据保存到时序数据库InfluxDB中.本小节就简单介绍一下如何安装和使用,及如何利用Graf ...

  7. SpringBoot整合log4j2导入新的依赖出现jar冲突解决

    1.问题复现: 之前在SpringBoot中配置整合了log4j2,今天在pom文件中,导入新的依赖(依赖如下)之后, <dependency> <groupId>com.gi ...

  8. LeetCode 200:岛屿数量 Number of Islands

    题目: 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. Given ...

  9. HTML+CSS基础 权重的计算 权重计算规则

    权重的计算 将选择器上面的选择器进行叠加,叠加后的总和就是该选择器的权重. 权重计算规则

  10. python asyncio 协程调用task步骤

    import asyncio async def compute(x, y): print("Compute %s + %s ..." % (x, y)) await asynci ...