洛谷数据好强啊,普通spfa开o2都过不了,要加双端队列优化 因为是双向边,所以dis(u,v)=dis(v,u),所以分别以pa1和pa2为起点spfa一遍,表示pb-->pa1-->pa2和pb-->pa2-->pa1,取个min即可 #include<iostream> #include<cstdio> #include<queue> using namespace std; const int N=200005,inf=2e9+7; in…
跑两遍最短路就好了.. 话说这翻译2333 ---------------------------------------------------------------------- #include<cstdio> #include<queue> #include<algorithm> #include<cstring> #include<iostream>   #define rep( i , n ) for( int i = 0 ; i…
http://www.lydsy.com/JudgeOnline/problem.php?id=2100 这题我要吐血啊 我交了不下10次tle.. 噗 果然是写挫了. 一开始没加spfa优化果断t 然后看了题解加了(加错了T_T)还是tle..我就怀疑数据了... 噗 原来我有个地方打错了.. 这个spfa的队列优化真神.. #include <cstdio> #include <cstring> using namespace std; #define rep(i, n) fo…
由于是无向图,所以可以枚举两个终点,跑两次最短路来更新答案. #include <queue> #include <cstdio> #include <cstring> #include <algorithm> #define N 100006 #define M 200007 #define setIO(s) freopen(s".in","r",stdin) using namespace std; deque&l…
因为是双向边,所以相当于两条到1的最短路和,先跑spfa然后直接处理询问即可 #include<iostream> #include<cstdio> #include<queue> using namespace std; const int N=50005,inf=1e9; int n,m,b,h[N],cnt,dis[N]; bool v[N]; struct qwe { int ne,to,va; }e[N<<2]; int read() { int…
题目描述 Bessie has two crisp red apples to deliver to two of her friends in the herd. Of course, she travels the C (1 <= C <= 200,000) cowpaths which are arranged as the usual graph which connects P (1 <= P <= 100,000) pastures conveniently numbe…
Description Bessie has two crisp red apples to deliver to two of her friends in the herd. Of course, she travels the C (1 <= C <= 200,000) cowpaths which are arranged as the usual graph which connects P (1 <= P <= 100,000) pastures convenientl…
题目描述 一张P个点的无向图,C条正权路.CLJ要从Pb点(家)出发,既要去Pa1点NOI赛场拿金牌,也要去Pa2点CMO赛场拿金牌.(途中不必回家)可以先去NOI,也可以先去CMO.当然神犇CLJ肯定会使总路程最小,输出最小值. 题解:做两遍spfa,找出从起点开始先去pa1或者先去pa2的最小值 需要用一下spfa的优化,每次进行入队的时候都与队头进行比较,如果比队头小就放在队头,否则放队尾. #include<bits/stdc++.h> using namespace std; #de…
tag是假的,用了及其诡异的方法判负环 正权无向边和负权有向边的图 #include<iostream> #include<cstdio> #include<cstring> using namespace std; const int N=505,inf=210000000; int n,m,w,h[N],cnt,d[N]; struct qwe { int ne,to,va; }e[N*N]; int read() { int r=0,f=1; char p=get…
明明是个最短路却有网络流一样的神建图= A = 首先要是暴力建图的话最坏有O(nm)条边.所以优化建图. 考虑分块思想,设bs=sqrt(n),对于p大于bs的,直接连边即可,最多有sqrt(n)条,注意边权不全是1了,因为要从b走过去:对于p小于等于bs,先把每栋楼建sqrt个辅助点,然后这些辅助点向原点连边,其中i点的辅助点j表示i栋楼可以跳p步.这些辅助点同层之间连双向边.然后对于一只狗,直接连接b点与b点的第p辅助点即可. spfa即可 然而事实证明bs取sqrt甚至会re,直接取100…