题目链接: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]抢掠计划】 题解的更多相关文章

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

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

  2. P3627 [APIO2009]抢掠计划

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

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

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

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

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

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

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

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

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

  7. Luogu 3627 [APIO2009]抢掠计划

    不爽. 为什么tarjan能爆栈啊 十分显然的缩点,给缩点之后的点连上权值为后一个点集权值的有向边,然后spfa跑最长路. 注意一开始$dis_{st}$应该等于$st$这个集合的权值. 时间复杂度$ ...

  8. 题解 P3627 【[APIO2009]抢掠计划】

    咕了四个小时整整一晚上 P3627 [APIO2009] 抢掠计划(https://www.luogu.org/problemnew/show/P3627) 不难看出答案即为该有向图的最长链长度(允许 ...

  9. APIO2009 抢掠计划 Tarjan DAG-DP

    APIO2009 抢掠计划 Tarjan spfa/DAG-DP 题面 一道\(Tarjan\)缩点水题.因为可以反复经过节点,所以把一个联通快中的所有路口看做一个整体,缩点后直接跑\(spfa\)或 ...

随机推荐

  1. CentOS初试

    由于实在是对ubuntu不太感冒,加上买的鸟哥又是拿CentOS做的例子,所以我就把ubuntu换成了CentOS6.5.依旧win7,CentOS 双系统,具体过程参照http://www.cnbl ...

  2. 【ExtJS】关于自定义组件

    一.命名规范 在你编码过程中对类,名字空间以及文件名使用统一的命名规则对你代码的组织,结构化以及可读性有很大的好处. 1.类命名规范: 类名最好只包含字母,在多数情况下,数字是不鼓励使用的,除非非要用 ...

  3. TOJ 1258 Very Simple Counting

    Description Let f(n) be the number of factors of integer n. Your task is to count the number of i(1 ...

  4. React.js 小书 Lesson16 - 实战分析:评论功能(三)

    作者:胡子大哈 原文链接:http://huziketang.com/books/react/lesson16 转载请注明出处,保留原文链接和作者信息. 接下来的代码比较顺理成章了.修改 Commen ...

  5. [转]Oracle ROWNUM用法和分页查询总结

    本文转自:http://blog.csdn.net/fw0124/article/details/42737671 ****************************************** ...

  6. ssm(spring、springmvc、mybatis)框架整合

    第一次接触这3大框架,打算一个一个慢慢学,参照网上资料搭建了一个ssm项目,作为新手吃亏在jar包的导入上,比如jdbc DataSource配置的时候由于导入的jar包不兼容或者缺包导致项目无法正常 ...

  7. My first python application

    ''' Authon:WSE_Gordon This application is use for the costomer to login the application. The Costome ...

  8. Csharp: Send Email

    /// <summary> /// 發送郵件 /// 塗聚文 /// 20130816 /// </summary> /// <param name="to&q ...

  9. 00字体图标iconfont的制作与使用--阿里矢量图库

    一.iconfont的使用范围 在工作当中,经常会用到嵌在元素里的小图标 在这种情况下,如果使用<img>标签或者用作背景图片,也能实现这种效果.但是如果这么做的话,就必须把图片一个个切下 ...

  10. 在 CentOS 上安装 node.js

    进入到 /usr/local/ 目录中: cd /usr/local/ 创建 nodejs 文件夹: mkdir -p nodejs 进入到 nodejs 目录中: cd nodejs 下载 node ...