BZOJ3538: [Usaco2014 Open]Dueling GPS
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.
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
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
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
题解:
麻烦的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的更多相关文章
- 【BZOJ】3538: [Usaco2014 Open]Dueling GPS(spfa)
http://www.lydsy.com/JudgeOnline/problem.php?id=3538 题意不要理解错QAQ,是说当前边(u,v)且u到n的最短距离中包含这条边,那么这条边就不警告. ...
- 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 ...
- Luogu P3106 [USACO14OPEN]GPS的决斗Dueling GPS's(最短路)
P3106 [USACO14OPEN]GPS的决斗Dueling GPS's 题意 题目描述 Farmer John has recently purchased a new car online, ...
- [USACO14OPEN] Dueling GPS's[最短路建模]
题目描述 Farmer John has recently purchased a new car online, but in his haste he accidentally clicked t ...
- 洛谷 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\). 每走一条边的时候,如果一个系统认为走 ...
- USACO 2014 US Open Dueling GPS's /// SPFA
题目大意: 给定n个点m条边的有向图 有两个GPS 分别认为 A[i]到B[i] 的一条边的花费是P[i].Q[i] 当当前走的边不是GPS认为的最短路上的边就会被警告 即两个GPS都不认为是最短路上 ...
- 2018.07.22 洛谷P3106 GPS的决斗Dueling GPS's(最短路)
传送门 图论模拟题. 这题直接写3个(可以压成一个)spfa" role="presentation" style="position: relative;&q ...
随机推荐
- windows 编程 —— 消息与参数(定时器、初始化消息、改变大小)
目录: 定时器 WM_TIMER 初始化消息 WM_CREATE 改变大小 WM_SIZE 定时器: 接收消息:WM_TIMER 参数wParam:wParam参数等于定时器的ID值(上述情形为1). ...
- [OpenNebula]中间件訪问驱动程序
/* -------------------------------------------------------------------------- */ /* Copyright 2002-2 ...
- SVN经常使用命令说明
SVN版本号:1.5 及更新版本号 名词说明: WC:Working Copy 你的工作区 Versioned:受控的:受版本号控制的 SVN是什么? SVN是开源的版本号控制系统. 比CVS很多其它 ...
- msxml 操作xml
1.简介 在.NET平台,微软为C#或托管C++程序员提供了丰富的类库,用以支持各种需求,其中就有对XML文件操作的丰富的类.例如XMLDocument, XmlElement等.但是C++标准库中并 ...
- HDU 5139 Formula 卡内存
题目就是求这个 n达到10^7,测试数据组数为10^5 为了防止TLE,一开始把每个n对应的值先求出来,但发现竟然开不了10^7的数组(MLE),然后就意识到这是第一道卡内存的题目... 只能离线做, ...
- api接口
目录(?)[-] 接口特点汇总 PHP Token令牌 先说第一个tokenapi_token 服务端接口校验PHP实现流程如下 再说第二个tokenuser_token 接口用例如下 接口特点汇总: ...
- java03变量和基本数据类型
/** * * 失去一日甚易,欲得回已无途! * * 关键字:java中的关键字 就是指 已经被定义了的单词! * 我们就不能再拿关键字 作为我们的变量名! * 常用的关键字: * 基本数据类型: * ...
- web03--session
1.创建session1.jsp <body> <form action="session2.jsp" method="post"> & ...
- SQL 去除小数点后无效 0 的方法
select convert(float,10.0000) 就是这么简单
- nyoj 44
//nyoj 44 //和上面一题一样,求子串和,但是代码非常简洁..... 时间复杂度为n #include <iostream> using namespace std; int ma ...