【luogu P3627 [APIO2009]抢掠计划】 题解
题目链接:https://www.luogu.org/problemnew/show/P3627
把点权转化到边权上去。
#include <stack>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 500000 + 10;
const int inf = 0x7fffffff;
struct edge{
    int from, to, next, len;
}e1[maxn<<2], e2[maxn<<2];
int head1[maxn], head2[maxn], cnt1, cnt2;
bool vis1[maxn], vis2[maxn];
int dfn[maxn], low[maxn], tim, color[maxn], num, dis[maxn];
int n, m, val1[maxn], val2[maxn], end, S, p, x[maxn], y[maxn], ans = -1;
stack<int> s;
queue<int> q;
void add1(int u, int v)
{
    e1[++cnt1].from = u;
    e1[cnt1].to = v;
    e1[cnt1].next = head1[u];
    head1[u] = cnt1;
}
void add2(int u, int v, int w)
{
    e2[++cnt2].from = u;
    e2[cnt2].to = v;
    e2[cnt2].len = w;
    e2[cnt2].next = head2[u];
    head2[u] = cnt2;
}
void tarjan(int x)
{
    dfn[x] = low[x] = ++tim;
    s.push(x); vis1[x] = 1;
    for(int i = head1[x]; i != -1; i = e1[i].next)
    {
        int v = e1[i].to;
        if(!dfn[v])
        {
            tarjan(v);
            low[x] = min(low[x], low[v]);
        }
        else if(vis1[v])
        {
            low[x] = min(low[x], low[v]);
        }
    }
    if(dfn[x] == low[x])
    {
        color[x] = ++num;
        vis1[x] = 0;
        val2[num] += val1[x];
        while(s.top() != x)
        {
            color[s.top()] = num;
            vis1[s.top()] = 0;
            val2[num] += val1[s.top()];
            s.pop();
        }
        s.pop();
    }
}
void SPFA()
{
    for(int i = 1; i <= num; i++) dis[i] = inf;
    dis[color[S]] = -1*val2[color[S]]; vis2[color[S]] = 1; q.push(color[S]);
    while(!q.empty())
    {
        int now = q.front(); q.pop();
        vis2[now] = 0;
        for(int i = head2[now]; i != -1; i = e2[i].next)
        {
            if(dis[e2[i].to] > dis[now] + e2[i].len)
            {
                dis[e2[i].to] = dis[now] + e2[i].len;
                if(!vis2[e2[i].to])
                {
                    q.push(e2[i].to);
                    vis2[e2[i].to] = 1;
                }
            }
        }
    }
}
int main()
{
    memset(head1, -1, sizeof(head1));
    memset(head2, -1, sizeof(head2));
    scanf("%d%d",&n,&m);
    for(int i = 1; i <= m; i++)
    {
        scanf("%d%d",&x[i],&y[i]);
        add1(x[i], y[i]);
    }
    for(int i = 1; i <= n; i++)	scanf("%d",&val1[i]);
    for(int i = 1; i <= n; i++)
        if(!dfn[i]) tarjan(i);
    for(int i = 1; i <= m; i++)
    {
        if(color[x[i]] != color[y[i]])
        add2(color[x[i]], color[y[i]], val2[color[y[i]]]*(-1));
    }
    scanf("%d%d",&S,&p);
    SPFA();
    for(int i = 1; i <= p; i++)
    {
        scanf("%d",&end);
        ans = max(ans, -1*dis[color[end]]);
    }
    //for(int i = 1; i <= n; i++) cout<<dis[i]<<" ";
    printf("%d",ans);
    return 0;
}
												
											【luogu P3627 [APIO2009]抢掠计划】 题解的更多相关文章
- 洛谷 P3627 [APIO2009]抢掠计划 题解
		
Analysis 建图+强连通分量+SPFA求最长路 但要保证最后到达的点中包含酒馆 虽然思路并不难想,但要求的代码能力很高. #include<iostream> #include< ...
 - P3627 [APIO2009]抢掠计划
		
P3627 [APIO2009]抢掠计划 Tarjan缩点+最短(最长)路 显然的缩点...... 在缩点时,顺便维护每个强连通分量的总权值 缩完点按照惯例建个新图 然后跑一遍spfa最长路,枚举每个 ...
 - 【题解】洛谷P3627 [APIO2009]抢掠计划(缩点+SPFA)
		
洛谷P3627:https://www.luogu.org/problemnew/show/P3627 思路 由于有强连通分量 所以我们可以想到先把整个图缩点 缩点完之后再建一次图 把点权改为边权 并 ...
 - 洛谷  P3627 [APIO2009]抢掠计划  Tarjan缩点+Spfa求最长路
		
题目地址:https://www.luogu.com.cn/problem/P3627 第一次寒假训练的结测题,思路本身不难,但对于我这个码力蒟蒻来说实现难度不小-考试时肛了将近两个半小时才刚肛出来. ...
 - [洛谷P3627][APIO2009]抢掠计划
		
题目大意:给你一张$n(n\leqslant5\times10^5)$个点$m(m\leqslant5\times10^5)$条边的有向图,有点权,给你起点和一些可能的终点.问从起点开始,到任意一个终 ...
 - 洛谷 P3627 [APIO2009]抢掠计划
		
这题一看就是缩点,但是缩完点怎么办呢?首先我们把所有的包含酒吧的缩点找出来,打上标记,然后建立一张新图, 每个缩点上的点权就是他所包含的所有点的点权和.但是建图的时候要注意,每一对缩点之间可能有多条边 ...
 - Luogu 3627 [APIO2009]抢掠计划
		
不爽. 为什么tarjan能爆栈啊 十分显然的缩点,给缩点之后的点连上权值为后一个点集权值的有向边,然后spfa跑最长路. 注意一开始$dis_{st}$应该等于$st$这个集合的权值. 时间复杂度$ ...
 - 题解 P3627 【[APIO2009]抢掠计划】
		
咕了四个小时整整一晚上 P3627 [APIO2009] 抢掠计划(https://www.luogu.org/problemnew/show/P3627) 不难看出答案即为该有向图的最长链长度(允许 ...
 - APIO2009 抢掠计划 Tarjan DAG-DP
		
APIO2009 抢掠计划 Tarjan spfa/DAG-DP 题面 一道\(Tarjan\)缩点水题.因为可以反复经过节点,所以把一个联通快中的所有路口看做一个整体,缩点后直接跑\(spfa\)或 ...
 
随机推荐
- 昨天太晚了,今天教你用Debug模式来分析程序执行顺序
			
还是以昨天的XML文件解析来做栗子,希望通过这个好吃的栗子可以举一反三 学会用debug来看源码和找Bug 事件类型主要有五种START_DOCUMENT:xml头的事件类型 = 0END_DO ...
 - 前端测试框架 puppeteer 文档翻译
			
puppeteer puppeteer 是一个通过DevTools 协议提供高级API 来控制 chrome,chromium 的 NODE库; puppeteer默认运行在 headless 模式, ...
 - mvc中在Action里调用另一个Action
			
今天做东西时发现一个新东西.即在一个Action调用另一Action.前提是同一个控制器.(没在一个控制里的没试过) 调用方法: public ActionResult Test1(){ //to ...
 - Vue2.0以后,有哪些变化
			
最近移动端项目版本升级,Vue由之前的1.0升级到2.3,那么,Vue2.0之后,有哪些细节的变化呢,现在总结如下: 1.在每个组件模板,不再支持片段代码 组件中模板: 之前: <templat ...
 - js把数据处理成钱的格式
			
1.var Rmoney = parseFloat(money).toFixed(2);//把money处理成保存2位小数的格式
 - 服务器LIUNX之如何解决矿机问题
			
点进来的基本都是遇到liunx变矿机的小伙伴吧(cpu运载300%) 卡的连终端都很难打开 开下来之后提示 大意是, 到xxx网站给钱了事, 不过基本这个网站基本也上不去, 要么是暴力破解, 要么是通 ...
 - PAT 1051 Pop Sequence
			
#include <cstdio> #include <cstdlib> #include <vector> using namespace std; bool p ...
 - 牛客提高R5 A.同余方程
			
题意 题目链接 Sol 设\(solve(x, y)\)表示\(i \in [0, x], j \in [0, y]\)满足题目要求的方案数 首先容斥一下,\(ans = solve(r_1, r_2 ...
 - JS基础学习——作用域
			
JS基础学习--作用域 什么是作用域 变量的作用域就是变量能被访问到的代码范围,比如在下面的这个js代码中,变量a的作用域就是函数foo,因此在全局作用域内的console.log(a)语句不能访问到 ...
 - html相对定位绝对定位
			
孔子说:“温故而知新,可以为师矣.”这几天参加了一个免费的前端课,每天晚上都有直播,讲解一个独立的案例.在听前端基础的时候,发现自己有不少东西没学会,平时在学校虽说html也写了不少,但有好大一部分都 ...