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. python-Django初体验

    1.搭建Django开发环境 2.创建工程与应用 CentOS6.5环境下 Python 2.6 ipython 1.2.1 Django 1.6.5 pip install -y django == ...

  2. Codeforces Round #260(div2)C(递推)

    有明显的递推关系: f[i]表示i为数列中最大值时所求结果.num[i]表示数i在数列中出现了几次. 对于数i,要么删i,要么删i-1,只有这两种情况,且子问题还是一样的思路.那么很显然递推一下就行了 ...

  3. uva1636 - Headshot(条件概率)

    简单的条件概率题,直接再来一枪没子弹的概率是所有子串”00“的数目除以‘0’的数目,随机转一下再打没子弹的概率是‘0’的数目除以总数目. #include<iostream> #inclu ...

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

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

  5. myEclipse下Maven配置操作

    一.Maven 安装与配置 1.点击计算机属性里的高级系统设置,点开环境变量进行配置 2.检验配置是否成功 二.手动创建一个Maven项目,并编译运行成功 1.创建一个文件夹作为项目的根目录 2. 在 ...

  6. SQL夯实基础(三):聚合函数详解

    一.GROUP BY  Having 聊聚合函数,首先肯定要弄清楚group by 和having 的用法. SELECT id, COUNT(course) as numcourse, AVG(sc ...

  7. bzoj 4059:Non-boring sequences 分治

    题目: 我们害怕把这道题题面搞得太无聊了,所以我们决定让这题超短.一个序列被称为是不无聊的,仅当它的每个连续子序列存在一个独一无二的数字,即每个子序列里至少存在一个数字只出现一次.给定一个整数序列,请 ...

  8. ASP.NET MVC5中View显示Html

    @Html.Raw(Model.Name) @(new HtmlString(Model.Name));

  9. hihoCoder#1068(RMQ-ST算法)

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho在美国旅行了相当长的一段时间之后,终于准备要回国啦!而在回国之前,他们准备去超市采购一些当地特产——比如汉堡 ...

  10. delphi 10.2.2.2004 Tokyo 安装步骤

    delphi 各版本的安装,其XX工具都附有详细说明.遵守其安装步骤,很容易成功. 本教就以 delphi 10.2.2.2004 为例,演示一下整个安装过程.其它各版本就自行尝试. 附: delph ...