题目大意:给你一张$n(n\leqslant5\times10^5)$个点$m(m\leqslant5\times10^5)$条边的有向图,有点权,给你起点和一些可能的终点。问从起点开始,到任意一个终点经过的点权和的最大值是多少。

题解:先把有向图缩点,然后从起点跑最长路,对每个终点取个最大值即可

卡点:求最长路写了$dijkstra$,然后死活调不出。

C++ Code:

#include <algorithm>
#include <cstdio>
#include <cctype>
#include <queue>
namespace __IO {
int ch;
inline int read() {
static int x;
while (isspace(ch = getchar())) ;
for (x = ch & 15; isdigit(ch = getchar()); ) x = x * 10 + (ch & 15);
return x;
}
}
using __IO::read; const int maxn = 500010, maxm = 500010;
int n, m;
int w[maxn], W[maxn]; namespace Graph {
int head[maxn], cnt;
struct Edge {
int from, to, nxt;
} e[maxm];
inline void addedge(int a, int b) {
e[++cnt] = (Edge) { a, b, head[a] }; head[a] = cnt;
} int DFN[maxn], low[maxn], idx;
int S[maxn], top, bel[maxn], scc;
bool inS[maxn];
void tarjan(int u) {
DFN[u] = low[u] = ++idx;
inS[S[++top] = u] = true;
int v;
for (int i = head[u]; i; i = e[i].nxt) {
v = e[i].to;
if (!DFN[v]) {
tarjan(v);
low[u] = std::min(low[u], low[v]);
} else if (inS[v]) low[u] = std::min(low[u], DFN[v]);
}
if (DFN[u] == low[u]) {
++scc;
do {
inS[v = S[top--]] = false;
W[bel[v] = scc] += w[v];
} while (v != u);
}
}
} int head[maxn], cnt;
struct Edge {
int to, nxt;
} e[maxm];
inline void addedge(int a, int b) {
e[++cnt] = (Edge) { b, head[a] }; head[a] = cnt;
} using Graph::scc;
using Graph::bel;
std::queue<int> q;
int dis[maxn];
bool inq[maxn];
void SPFA(int S) {
dis[S] = W[S];
q.push(S);
while (!q.empty()) {
int u = q.front(); q.pop();
inq[u] = false;
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if (dis[v] < dis[u] + W[v]) {
dis[v] = dis[u] + W[v];
q.push(v);
inq[v] = true;
}
}
}
} int S, ans;
int main() {
n = read(), m = read();
for (int i = 0, a, b; i < m; ++i) {
a = read(), b = read();
Graph::addedge(a, b);
}
for (int i = 1; i <= n; ++i) w[i] = read();
S = read();
Graph::tarjan(S);
for (int i = 1, u, v; i <= m; ++i) {
u = bel[Graph::e[i].from], v = bel[Graph::e[i].to];
if (u != v) addedge(u, v);
}
SPFA(bel[S]);
for (int i = read(), x; i; --i) ans = std::max(ans, dis[bel[x = read()]]);
printf("%d\n", ans);
return 0;
}

[洛谷P3627][APIO2009]抢掠计划的更多相关文章

  1. 【题解】洛谷P3627 [APIO2009]抢掠计划(缩点+SPFA)

    洛谷P3627:https://www.luogu.org/problemnew/show/P3627 思路 由于有强连通分量 所以我们可以想到先把整个图缩点 缩点完之后再建一次图 把点权改为边权 并 ...

  2. 洛谷 P3627 [APIO2009]抢掠计划 Tarjan缩点+Spfa求最长路

    题目地址:https://www.luogu.com.cn/problem/P3627 第一次寒假训练的结测题,思路本身不难,但对于我这个码力蒟蒻来说实现难度不小-考试时肛了将近两个半小时才刚肛出来. ...

  3. 洛谷 P3627 [APIO2009]抢掠计划

    这题一看就是缩点,但是缩完点怎么办呢?首先我们把所有的包含酒吧的缩点找出来,打上标记,然后建立一张新图, 每个缩点上的点权就是他所包含的所有点的点权和.但是建图的时候要注意,每一对缩点之间可能有多条边 ...

  4. 洛谷 P3627 [APIO2009]抢掠计划 题解

    Analysis 建图+强连通分量+SPFA求最长路 但要保证最后到达的点中包含酒馆 虽然思路并不难想,但要求的代码能力很高. #include<iostream> #include< ...

  5. 洛谷 P3627 【抢掠计划】

    题库:洛谷 题号:3627 题目:抢掠计划 link:https://www.luogu.org/problem/P3627 思路 : 这道题是一道Tarjan + 最长路的题.首先,我们用Tarja ...

  6. BZOJ1179或洛谷3672 [APIO2009]抢掠计划

    BZOJ原题链接 洛谷原题链接 在一个强连通分量里的\(ATM\)机显然都可被抢,所以先用\(tarjan\)找强连通分量并缩点,在缩点的后的\(DAG\)上跑最长路,然后扫一遍酒吧记录答案即可. # ...

  7. 洛谷3627 [APIO2009]抢掠计划

    题目描述 输入格式: 第一行包含两个整数 N.M.N 表示路口的个数,M 表示道路条数.接下来 M 行,每行两个整数,这两个整数都在 1 到 N 之间,第 i+1 行的两个整数表示第 i 条道路的起点 ...

  8. P3627 [APIO2009]抢掠计划

    P3627 [APIO2009]抢掠计划 Tarjan缩点+最短(最长)路 显然的缩点...... 在缩点时,顺便维护每个强连通分量的总权值 缩完点按照惯例建个新图 然后跑一遍spfa最长路,枚举每个 ...

  9. 洛谷 P3627 [APIO2009](抢掠计划 缩点+spfa)

    题目描述 Siruseri 城中的道路都是单向的.不同的道路由路口连接.按照法律的规定, 在每个路口都设立了一个 Siruseri 银行的 ATM 取款机.令人奇怪的是,Siruseri 的酒吧也都设 ...

随机推荐

  1. sphinx生成cakephp文档

    cakephp的文档是用一个叫sphinx程序生成的 这个程序是python写的,所以我们要用sphinx本机必须先装python. 编译过程在Ubuntu下进行,默认Ubuntu已经安装了pytho ...

  2. steam更新出错 应用运行中

    游戏程序没有完全关闭,仍在后台运行. 打开任务处理器,选择进程,下面找到TslGame,关闭之.

  3. redis 问题记录

    摘抄来自:https://zhuoroger.github.io/ 1.slowlog和排队延时 slowlog是排查性能问题关键监控指标.它是记录Redis queries运行时间超时特定阀值的系统 ...

  4. nested class 例子

    #include<iostream> using namespace std; /* start of Enclosing class declaration */ class Enclo ...

  5. Python入门编程中的变量、字符串以及数据类型

    //2018.10.10 字符串与变量 1. 在输出语句中如果需要出现单引号或者双引号,可以使用转义符号\,它可以将其中的歧义错误解释化解,使得输出正常: 2. 对于python的任何变量都需要进行赋 ...

  6. 为什么说session依赖cookie,以及cookie的常用知识

    session的用法 session在Flask中通常用做设置某些页面的权限,比如某些页面必须要登录才可以看到,登录的信息或标志就放到session中.它的使用过程如下: 在整个flask工程的启动文 ...

  7. leetcode-帕斯卡三角形

    帕斯卡三角形 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 示例: 输入: 5 输出: [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4 ...

  8. lintcode: Missing String

    Missing String  描述: Given two strings, you have to find the missing string. Have you met this questi ...

  9. JAVA基础学习之路(四)定义简单java类

    简单java类开发一般原则: 类名称必须有意义,再怎么说,要让人家看的明白吧 类之中所有属性必须使用private封装,并提供setter,getter方法 类之中可以有多个构造方法,但是必须保留有一 ...

  10. [C++] OOP - Base and Derived Classes

    There is a base class at the root of the hierarchy, from which the other class inherit, directly or ...