3538: [Usaco2014 Open]Dueling GPS

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 59  Solved: 36
[Submit][Status]

Description

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次警告.
求一种方案,1àn,最少需要受到多少次警告.

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

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.

Sample Output

1
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.

HINT

 

Source

Silver By liyizhen2

题解:

麻烦的sb题。。。来回搞几次spfa就行了

代码:

 #include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#define inf 0x7fffffff
#define MAXN 100001
using namespace std; inline int read() {
int x = , f = ;
char ch = getchar();
while (ch < '' || ch > '') {
if (ch == '-')f = -;
ch = getchar();
}
while (ch >= '' && ch <= '') {
x = x * + ch - '';
ch = getchar();
}
return x*f;
} struct edge {
int to, next, v1, v2;
} e[MAXN], d[MAXN];
int n, m, cnt, ans, u[MAXN], v[MAXN], w1[MAXN], w2[MAXN], d1[], d2[], dis[], head[], h[]; void ins(int u, int v, int w1, int w2) {
e[++cnt] = (edge){v, head[u], w1, w2};
head[u] = cnt;
} void spfa1() {
int q[MAXN], t = , w = ;
bool inq[];
memset(inq, , sizeof (inq));
memset(d1, , sizeof (d1));
d1[n] = ;
q[] = n;
inq[n] = ;
while (t <= w) {
int now = q[t++];
for (int i = head[now]; i; i = e[i].next) {
if (d1[now] + e[i].v1 < d1[e[i].to]) {
d1[e[i].to] = d1[now] + e[i].v1;
if (!inq[e[i].to]) {
q[++w] = e[i].to;
inq[e[i].to] = ;
}
}
}
inq[now] = ;
}
} void spfa2() {
int q[MAXN], t = , w = ;
bool inq[];
memset(inq, , sizeof (inq));
memset(d2, , sizeof (d2));
d2[n] = ;
q[] = n;
inq[n] = ;
while (t <= w) {
int now = q[t++];
for (int i = head[now]; i; i = e[i].next) {
if (d2[now] + e[i].v2 < d2[e[i].to]) {
d2[e[i].to] = d2[now] + e[i].v2;
if (!inq[e[i].to]) {
q[++w] = e[i].to;
inq[e[i].to] = ;
}
}
}
inq[now] = ;
}
} void spfa3() {
int q[MAXN], t = , w = ;
bool inq[];
memset(inq, , sizeof (inq));
memset(dis, , sizeof (dis));
dis[] = ;
q[] = ;
inq[] = ;
while (t <= w) {
int now = q[t++];
for (int i = h[now]; i; i = d[i].next) {
if (dis[now] + d[i].v1 < dis[d[i].to]) {
dis[d[i].to] = dis[now] + d[i].v1;
if (!inq[d[i].to]) {
q[++w] = d[i].to;
inq[e[i].to] = ;
}
}
}
inq[now] = ;
}
} 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();
spfa2();
for (int i = ; i <= m; i++) {
d[i].to = v[i];
d[i].next = h[u[i]];
h[u[i]] = i;
if (d1[v[i]] + w1[i] > d1[u[i]])d[i].v1++;
if (d2[v[i]] + w2[i] > d2[u[i]])d[i].v1++;
}
spfa3();
printf("%d", dis[n]);
return ;
}

BZOJ3538: [Usaco2014 Open]Dueling GPS的更多相关文章

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

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

  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. USACO Dueling GPS's

    洛谷 P3106 [USACO14OPEN]GPS的决斗Dueling GPS's 洛谷传送门 JDOJ 2424: USACO 2014 Open Silver 2.Dueling GPSs JDO ...

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. Object-C @synthesize -- 笔记

  2. 使用jcrop进行头像剪切

    http://www.cnblogs.com/chenssy/archive/2013/05/18/3084985.html http://code.ciaoca.com/jquery/jcrop/ ...

  3. Java基础知识强化87:BigInteger类之BigInteger加减乘除法的使用

    1. BigInteger加减乘除法的使用 public BigInteger add(BigInteger val):加 public BigInteger subtract(BigInteger ...

  4. Android应用程序安装与Launcher启动机制

      以下资料摘录整理自老罗的Android之旅博客,是对老罗的博客关于Android底层原理的一个抽象的知识概括总结(如有错误欢迎指出)(侵删):http://blog.csdn.net/luoshe ...

  5. tomcat结合nginx使用 基础教程

    相信很多人都听过nginx,这个小巧的东西慢慢地在吞食apache和IIS的份额.那究竟它有什么作用呢?可能很多人未必了解. 说到反向代理,可能很多人都听说,但具体什么是反向代理,很多人估计就不清楚了 ...

  6. MySQL性能调优与架构设计读书笔记

    可扩展性设计之数据切分 14.2 数据的垂直切分 如何切分,切分到什么样的程度,是一个比较考验人的难题.只能在实际的应用场景中通过平衡各方面的成本和利益,才能分析出一个真正适合自己的拆分方案. 14. ...

  7. GUID的广泛使用

    GUID(Global unique identifier)全局唯一标识符,它是由网卡上的标识数字(每个网卡都有唯一的标识号)以及 CPU 时钟的唯一数字生成的的一个 16 字节的二进制值. GUID ...

  8. Asp.Net WebApi Action命名中已‘Get’开头问题

    ApiController 中的Action 命名已‘Get’开头,Post方法提交失败 场景: 1.action命名使用Get开头 /// <summary> /// 获取用户的未读消息 ...

  9. cookie有效期到了后,是由浏览器还是由系统还删除的

    Cookie可以保持登录信息到用户下次与服务器的会话,换句话说,下次访问同一网站时,用户会发现不必输入用户名和密码就已经登录了(当然,不排除用户手工删除Cookie).而还有一些Cookie在用户退出 ...

  10. Shell中逻辑判断

    [ 条件1 -a 条件2 ]   当1和2都真时才为真 [ 条件1 -o 条件2 ]   当1和2其中一个为真即为真 [ ! 条件 ]           取反   &&     与 ...