BZOJ2200

听说加上slf优化的spfa的卡过,真的不想写这些东西。

考虑使用堆优化的dij算法。

先加上所有双向边,然后dfs一下搜出所有由双向边构成的联通块,然后加上所有的单向边,一边对所有联通块拓扑排序一边在联通块内部处理最短路,因为所有的双向边都是不带负权的,而单向边都是有负权的,所以这样规避dij贪心的错误之处。

注意到一个$inf$可能被另一个$inf$加上一个负权边拓展得到,所以最后的答案可能会小于$inf$,检验的时候注意取的极大值要小于一开始赋的$inf$。

时间复杂度$O(nlogn)$。

Code:

#include <cstdio>
#include <cstring>
#include <queue>
#include <iostream>
#include <vector>
using namespace std;
typedef pair <int, int> pin; const int N = ;
const int M = 2e5 + ;
const int inf = 1e8; int n, m1, m2, st, tot = , head[N], dis[N];
int l = , r = , q[N], deg[N], sccCnt = , bel[N];
bool vis[N];
vector <int> scc[N]; struct Edge {
int to, nxt, val;
} e[M]; inline void add(int from, int to, int val) {
e[++tot].to = to;
e[tot].val = val;
e[tot].nxt = head[from];
head[from] = tot;
} template <typename T>
inline void read(T &X) {
X = ; char ch = ; T op = ;
for(; ch > ''|| ch < ''; ch = getchar())
if(ch == '-') op = -;
for(; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} void dfs(int x) {
bel[x] = sccCnt, scc[sccCnt].push_back(x);
for(int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if(!bel[y]) dfs(y);
}
} priority_queue <pin> Q;
void dij(int c) {
for(unsigned int i = ; i < scc[c].size(); i++) Q.push(pin(-dis[scc[c][i]], scc[c][i]));
for(; !Q.empty(); ) {
int x = Q.top().second; Q.pop();
if(vis[x]) continue;
vis[x] = ;
for(int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if(bel[y] == c) {
if(dis[y] > dis[x] + e[i].val) {
dis[y] = dis[x] + e[i].val;
Q.push(pin(-dis[y], y));
}
} else {
if(dis[y] > dis[x] + e[i].val) dis[y] = dis[x] + e[i].val;
deg[bel[y]]--;
if(!deg[bel[y]]) q[++r] = bel[y];
}
}
}
} int main() {
read(n), read(m1), read(m2), read(st);
for(int x, y, v, i = ; i <= m1; i++) {
read(x), read(y), read(v);
add(x, y, v), add(y, x, v);
} for(int i = ; i <= n; i++)
if(!bel[i]) ++sccCnt, dfs(i); for(int x, y, v, i = ; i <= m2; i++) {
read(x), read(y), read(v);
add(x, y, v);
deg[bel[y]]++;
} for(int i = ; i <= sccCnt; i++)
if(!deg[i]) q[++r] = i; memset(dis, 0x3f, sizeof(dis)); dis[st] = ;
for(; l <= r; ++l) dij(q[l]); for(int i = ; i <= n; i++) {
if(dis[i] > inf) puts("NO PATH");
else printf("%d\n", dis[i]);
} return ;
}

Luogu 3008 [USACO11JAN]道路和飞机Roads and Planes的更多相关文章

  1. P3008 [USACO11JAN]道路和飞机Roads and Planes

    P3008 [USACO11JAN]道路和飞机Roads and Planes Dijkstra+Tarjan 因为题目有特殊限制所以不用担心负权的问题 但是朴素的Dijkstra就算用堆优化,也显然 ...

  2. bzoj1626 / P2872 [USACO07DEC]道路建设Building Roads

    P2872 [USACO07DEC]道路建设Building Roads kruskal求最小生成树. #include<iostream> #include<cstdio> ...

  3. luogu 2296 寻找道路 (搜索)

    luogu 2296 寻找道路 题目链接:https://www.luogu.org/problemnew/show/P2296 从终点bfs或者dfs,找出所有终点能到达的点. 然后再从1到n看一下 ...

  4. 洛谷——P2872 [USACO07DEC]道路建设Building Roads

    P2872 [USACO07DEC]道路建设Building Roads 题目描述 Farmer John had just acquired several new farms! He wants ...

  5. 洛谷 P2872 [USACO07DEC]道路建设Building Roads 题解

    P2872 [USACO07DEC]道路建设Building Roads 题目描述 Farmer John had just acquired several new farms! He wants ...

  6. Luogu 2296 寻找道路

    https://www.luogu.org/problemnew/show/2296 题目描述 在有向图G 中,每条边的长度均为1 ,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足以 ...

  7. USACO 07DEC 道路建设(Building Roads)

    Farmer John had just acquired several new farms! He wants to connect the farms with roads so that he ...

  8. Luogu P3007 [USACO11JAN]大陆议会The Continental Cowngress

    P3007 [USACO11JAN]大陆议会The Continental Cowngress 题意 题意翻译 简述:给出\(n\)个法案,\(m\)头牛的意见,每头牛有两个表决格式为"支持 ...

  9. 【luogu P2296 寻找道路】 题解

    题目链接:https://www.luogu.org/problemnew/show/P2296 题意:给定起点终点,找一条从起点到终点的最短路径使路上的每个点都能有路径到达终点. 我们先反着建一遍图 ...

随机推荐

  1. (转)libcurl库使用方法,好长,好详细。

    一.ibcurl作为是一个多协议的便于客户端使用的URL传输库,基于C语言,提供C语言的API接口,支持DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP ...

  2. Leetcode 974. Subarray Sums Divisible by K

    前缀和(prefix sum/cumulative sum)的应用. 还用了一个知识点: a≡b(mod d) 则 a-b被d整除. 即:a与b对d同余,则a-b被d整除. class Solutio ...

  3. PHP学习之数组Array操作和键值对操作函数(一)

    PHP 中的数组实际上是一个有序映射.映射是一种把 values关联到 keys 的类型.此类型在很多方面做了优化,因此可以把它当成真正的数组,或列表(向量),散列表(是映射的一种实现),字典,集合, ...

  4. java 守护线程整理

    java中finally语句不走的可能存在system.exit(0)与守护线程 线程sleep采用TimeUnit类 设定线程的名字thread.getcurrentThread().setName ...

  5. 学习动态性能表(7)--v$process

    学习动态性能表 第七篇--V$PROCESS  2007.5.30 本视图包含当前系统oracle运行的所有进程信息.常被用于将oracle或服务进程的操作系统进程ID与数据库session之间建立联 ...

  6. 解决docker 下来镜像出现 error pulling image configuration: Get https://dseasb33srnrn.cloudfront.net的问题

    http://f2d6cb40.m.daocloud.io [root@node2 ~]# docker --version                                       ...

  7. Installing Redis more properly

    Installing Redis more properly Running Redis from the command line is fine just to hack a bit with i ...

  8. java中i/o练习

    总结: FileInputStream fis; int length; while((length=fis.read(b,0,b.length))!=-1){ output.write(b,0,le ...

  9. ThreadPoolTaskExecutor的配置解释

    ThreadPoolTaskExecutor的配置在网上找了很多解释没找到,看了下ThreadPoolExecutor的配置,名字差不多,应该含义也差不多.只不过ThreadPoolTaskExecu ...

  10. L3-001. 凑零钱(dfs或者01背包)

    L3-001. 凑零钱 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 韩梅梅喜欢满宇宙到处逛街.现在她逛到了一家火星店里,发现 ...