【BZOJ】3538: [Usaco2014 Open]Dueling GPS(spfa)
http://www.lydsy.com/JudgeOnline/problem.php?id=3538
题意不要理解错QAQ,是说当前边(u,v)且u到n的最短距离中包含这条边,那么这条边就不警告。
那么我们反向spfa两次,然后再正向spfa就行了
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=10006, M=5*N, oo=~0u>>2;
int n, ihead[N], d[3][N], m, q[N], front, tail, vis[N], cnt, tot, P[M], Q[M], U[M], V[M];
struct ED { int to, w, next; }e[M];
void add(int u, int v) {
e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v;
} void spfa(int s, int *d) {
for1(i, 1, n) d[i]=oo;
front=tail=0;
vis[s]=1; q[tail++]=s; d[s]=0;
int v;
while(front!=tail) {
int u=q[front++]; if(front==N) front=0; vis[u]=0;
for(int i=ihead[u]; i; i=e[i].next) if(d[v=e[i].to]>d[u]+e[i].w) {
d[v]=d[u]+e[i].w;
if(!vis[v]) {
vis[v]=1;
q[tail++]=v; if(tail==N) tail=0;
}
}
}
} int main() {
read(n); read(m);
for1(i, 1, m) {
int u=getint(), v=getint();
add(v, u);
P[i]=getint(); Q[i]=getint();
U[i]=u; V[i]=v;
}
for1(i, 1, cnt) e[i].w=P[i];
spfa(n, d[0]);
for1(i, 1, cnt) e[i].w=Q[i];
spfa(n, d[1]);
CC(ihead, 0); cnt=0;
for1(i, 1, m) {
int u=U[i], v=V[i];
add(u, v);
e[i].w=2;
if(d[0][u]==d[0][v]+P[i]) --e[i].w;
if(d[1][u]==d[1][v]+Q[i]) --e[i].w;
}
spfa(1, d[2]);
if(d[2][n]==oo) d[2][n]=-1;
print(d[2][n]);
return 0;
}
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
【BZOJ】3538: [Usaco2014 Open]Dueling GPS(spfa)的更多相关文章
- 【BZOJ】2015: [Usaco2010 Feb]Chocolate Giving(spfa)
http://www.lydsy.com/JudgeOnline/problem.php?id=2015 这种水题真没啥好说的.. #include <cstdio> #include & ...
- 【BZOJ】2019: [Usaco2009 Nov]找工作(spfa)
http://www.lydsy.com/JudgeOnline/problem.php?id=2019 spfa裸题.....将飞机场的费用变成负,然后spfa找正环就行了 #include < ...
- 【BZOJ】3053: The Closest M Points(kdtree)
http://www.lydsy.com/JudgeOnline/problem.php?id=3053 本来是1a的QAQ.... 没看到有多组数据啊.....斯巴达!!!!!!!!!!!!!!!! ...
- 【BZOJ】3668: [Noi2014]起床困难综合症(暴力)
http://www.lydsy.com/JudgeOnline/problem.php?id=3668 这题很简单.............. 枚举每一位然后累计即可.. QAQ,第一次以为能1A, ...
- 【BZOJ】1097: [POI2007]旅游景点atr(spfa+状压dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=1097 首先还是我很sb....想到了分层图想不到怎么串起来,,,以为用拓扑序搞转移,,后来感到不行. ...
- 【BZOJ】3223: Tyvj 1729 文艺平衡树(splay)
http://www.lydsy.com/JudgeOnline/problem.php?id=3223 默默的.. #include <cstdio> #include <cstr ...
- 【BZOJ】1602: [Usaco2008 Oct]牧场行走(lca)
http://www.lydsy.com/JudgeOnline/problem.php?id=1602 一开始以为直接暴力最短路,但是n<=1000, q<=1000可能会tle. 显然 ...
- 【BZOJ】1601: [Usaco2008 Oct]灌水(kruskal)
http://www.lydsy.com/JudgeOnline/problem.php?id=1601 很水的题,但是一开始我看成最短路了T_T 果断错. 我们想,要求连通,对,连通!连通的价值最小 ...
- 【BZOJ】1600: [Usaco2008 Oct]建造栅栏(dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=1600 说好的今天开始刷水.. 本题一开始我以为是排列组合,但是自己弱想不出来,只想到了如果四边有一条 ...
随机推荐
- JMeter 十三:生成 report dashboard
参考:http://jmeter.apache.org/usermanual/generating-dashboard.html JMeter 3.x开始,可以生成HTML格式的report . 注意 ...
- JMeter 十一:参数化
Test Plan中定义变量 打开测试计划,在用户定义的变量中定义变量. 这里定义了一个HOST变量,值为“www.baidu.com”. 之后就可以使用 ${HOST} 来引用这个变量. User ...
- kubernetes 环境搭建
一.规划1.系统centos 7 2.ip规划及功能分配192.168.2.24 master 192.168.2.24 etcd 192.168.2.25 node1(即minion)192.168 ...
- 【转】C++ 虚函数&纯虚函数&抽象类&接口&虚基类
1. 动态多态 在面向对象语言中,接口的多种不同实现方式即为多态.多态是指,用父类的指针指向子类的实例(对象),然后通过父类的指针调用实际子类的成员函数. 多态性就是允许将子类类型的指针赋值给父类类型 ...
- iOS/iphone开发如何为苹果开发者帐号APPID续费
原文地址:iOS/iphone开发如何为苹果开发者帐号APPID续费作者:陈双超_群雄 其实相当的简单,这篇内容是给财务看的,有的地方连我自己看了都感觉有点...但如果不详细,她又要为难我,所以我就当 ...
- python将字典内容存入mysql
1.背景 项目须要,用python实现了将字典内容存入本地的mysql数据库. 比方说有个字典dic={"a":"b","c":& ...
- Loadrunner定时执行脚本
# -*- coding: utf-8 -*- import timeimport os #格式为小时,分钟,脚本名称(包括盘符,最好是放在根目录下)#需要把LoadRunner安装路径的Bin加入系 ...
- activeMq发送消息流程
1,发送消息入口 Message message = messageBean.getMessageCreator().createMessage(session); producer.send(mes ...
- Oracle中查询某字段不为空或者为空的SQL语句怎么写
比如 insert into table a (a1,b1)values("a1",''); 对于这种情况,因为表里存的是'',其实是没有内容的,要查询这个字段,不能直接使用 se ...
- 【HTML】百度地图webAPI使用
1.登录百度地图,创建WEB应用,设置白名单.获取该WEB应用的ak 2.在页面引入相应的js和ak 3.效果(CSS不提供): 4.实例化地图map,并给map添加相应的搜索和确定坐标事件 < ...