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. ps-基础知识

    一.常用名词及几个常见控制面板. 二.工具栏中各工具的名称及使用方法. 三.新建文件的流程和注意事项. 四.光与色的基础知识

  2. C#文件操作常用相关类(Directory类、File类、Path类)

    1.文件操作常用相关类 1)File //操作文件,静态类,对文件整体操作.拷贝.删除.剪切等 2)Directory //操作目录(文件夹),静态类 3)DirectoryInfo //文件夹的一个 ...

  3. hdu4121 poj4001 Xiangqi(模拟)

    模拟题考验coding能力,一定要思路清晰,按照模块化思想,有哪些情况,需要哪些功能都要事先分析好了.高手的模拟题代码往往结构很清晰,功能模块写成函数,没有过多重复代码,让人一看便明. 方法选择的好坏 ...

  4. Unity 头发随动效果

    目标 实现角色的衣袖.头发.裙摆.披风.尾巴等,在角色运动时,可以产生随动的效果.类似王者荣耀角色展示界面. 准备 源码出出处:https://github.com/unity3d-jp/unityc ...

  5. 【转】Cron表达式简介

    Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或7个域,每一个域代表一个含义,Cron有如下两种语法格式: Seconds Minutes Hours DayofMonth Month ...

  6. type命令

    用途说明 type命令用来显示指定命令的类型.一个命令的类型可以是如下几种: alias 别名 keyword 关键字,Shell保留字 function 函数,Shell函数 builtin 内建命 ...

  7. canvas绘制简单图形

    canvas绘图篇: canvas绘制矩形: <!DOCTYPE html> <html> <head lang="en"> <meta ...

  8. Java-API-Package:org.springframework.web.bind.annotation

    ylbtech-Java-API-Package:org.springframework.web.bind.annotation 1.返回顶部 1. @NonNullApi @NonNullField ...

  9. [转]在 Windows 操作系统中的已知安全标识符(Sid security identifiers)

    安全标识符 (SID) 是用于标识安全主体或安全组在 Windows 操作系统中的可变长度的唯一值.常用 Sid 的 Sid 标识普通用户的一组或通用组.跨所有操作系统,它们的值保持不变. 此信息可用 ...

  10. 升级 AngularJS 至 Angular

    Victor Savkin 大神撰写了一系列文章详细介绍如何升级 AngularJS 应用: NgUpgrade in Depth Upgrade Shell Two Approaches to Up ...