Luogu P3627 抢掠计划
题目传送门
\(Tarjan\)缩点+SPFA最长路
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct zzz{
int f,t,nex;
}e[500010]; int head[500010],tot;
void add(int x,int y){
e[++tot].f=x;
e[tot].t=y;
e[tot].nex=head[x];
head[x]=tot;
}
int dfn[500010],low[500010],deth,s[500010],top,col,belong[500010];
bool vis[500010];
void tarjan(int f){
dfn[f]=low[f]=++deth;
s[++top]=f; vis[f]=1;
for(int i=head[f];i;i=e[i].nex){
int to=e[i].t;
if(!dfn[to]){
tarjan(to); low[f]=min(low[f],low[to]);
}
else
if(vis[to]) low[f]=min(low[f],dfn[to]);
}
if(dfn[f]==low[f]){
col++;
int k=0;
do{
k=s[top--];
vis[k]=0;
belong[k]=col;
}while(k!=f);
}
}
int read(){
int k=0; char c=getchar();
for(;c<'0'||c>'9';) c=getchar();
for(;c>='0'&&c<='9';c=getchar())
k=(k<<3)+(k<<1)+c-48;
return k;
}
int atm[500010]; bool cof[500010];
int q[500010<<2],dis[500010]; int h=1,tail;
int main(){
int n=read(),m=read();
for(int i=1;i<=m;i++){
int x=read(),y=read();
add(x,y);
}
for(int i=1;i<=n;i++)
if(!dfn[i]) tarjan(i);
for(int i=1;i<=n;i++)
atm[belong[i]]+=read();
int s=read(),cnt=read();
for(int i=1;i<=cnt;i++)
cof[belong[read()]]=1;
memset(head,0,sizeof(head)); tot=0;
for(int i=1;i<=m;i++){
int x=belong[e[i].f],y=belong[e[i].t];
if(x!=y) add(x,y);
}
memset(vis,0,sizeof(vis));
q[++tail]=belong[s]; dis[belong[s]]=atm[belong[s]];
vis[belong[s]]=1;
while(h<=tail){
int k=q[h]; h++; vis[k]=0;
for(int i=head[k];i;i=e[i].nex){
int to=e[i].t;
if(dis[k]+atm[to]>dis[to]){
dis[to]=atm[to]+dis[k];
if(!vis[to])
q[++tail]=to, vis[to]=1;
}
}
}
int ans=0;
for(int i=1;i<=col;i++)
if(cof[i]) ans=max(ans,dis[i]);
cout<<ans;
return 0;
}
Luogu P3627 抢掠计划的更多相关文章
- 【Luogu】P3627抢掠计划(缩点最短路)
题目链接在此 有环当然一定尽量走环,这是搞缩点的人都知道的常识. 建了新图之后搞点权SPFA跑最长路.枚举每个酒吧选择最大值. 发现我的博客写的越来越水了 #include<cstdio> ...
- 洛谷 P3627 【抢掠计划】
题库:洛谷 题号:3627 题目:抢掠计划 link:https://www.luogu.org/problem/P3627 思路 : 这道题是一道Tarjan + 最长路的题.首先,我们用Tarja ...
- 题解 P3627 【[APIO2009]抢掠计划】
咕了四个小时整整一晚上 P3627 [APIO2009] 抢掠计划(https://www.luogu.org/problemnew/show/P3627) 不难看出答案即为该有向图的最长链长度(允许 ...
- P3627 [APIO2009]抢掠计划
P3627 [APIO2009]抢掠计划 Tarjan缩点+最短(最长)路 显然的缩点...... 在缩点时,顺便维护每个强连通分量的总权值 缩完点按照惯例建个新图 然后跑一遍spfa最长路,枚举每个 ...
- 【洛谷P3627】[APIO2009]抢掠计划
抢掠计划 题目链接 比较水的缩点模板题,Tarjan缩点,重新建图,记录联通块的钱数.是否有酒吧 DAG上记忆化搜索即可 #include<iostream> #include<cs ...
- APIO2009 抢掠计划 Tarjan DAG-DP
APIO2009 抢掠计划 Tarjan spfa/DAG-DP 题面 一道\(Tarjan\)缩点水题.因为可以反复经过节点,所以把一个联通快中的所有路口看做一个整体,缩点后直接跑\(spfa\)或 ...
- [APIO2009]抢掠计划(Tarjan,SPFA)
[APIO2009]抢掠计划 题目描述 Siruseri 城中的道路都是单向的.不同的道路由路口连接.按照法律的规定, 在每个路口都设立了一个 Siruseri 银行的 ATM 取款机.令人奇怪的是, ...
- 【luogu P3627 [APIO2009]抢掠计划】 题解
题目链接:https://www.luogu.org/problemnew/show/P3627 把点权转化到边权上去. #include <stack> #include <que ...
- 洛谷 P3627 [APIO2009]抢掠计划 Tarjan缩点+Spfa求最长路
题目地址:https://www.luogu.com.cn/problem/P3627 第一次寒假训练的结测题,思路本身不难,但对于我这个码力蒟蒻来说实现难度不小-考试时肛了将近两个半小时才刚肛出来. ...
随机推荐
- vue key值的重复键问题报错
1.问题描述:在vue2.0+ 中做一个公用的评论组件,:key使用的时创建评论的时间,当加载更多的时候,会报错: Duplicate keys detected: '2019-01-24T07:15 ...
- HDU 3729【二分匹配】
题意: 给出n个同学的排名,代表每个排名在哪个区间,要求保证最多人说的是实话,并在此前提下求一个说真话人最大字典序. 思路: 最后感觉就是点去填区间,点和区间建个边,然后跑个二分图,然后sort一发. ...
- SQL 语句(原生)
//查 //查询表里的所有数据 select * from 表名 //根据id等字段查询数据 select * from 表名 where 字段 = 值 or 字段 = 值 (例):select * ...
- MySQL 派生表(Derived Table) Merge Optimization
本文将通过演示告诉你:MySQL中派生表(Derived Table)是什么?以及MySQL对它的优化. Background 有如下一张表: mysql> desc city; +------ ...
- Elasticsearch学习记录(分布式的特性)
Elasticsearch学习记录(分布式的特性) 分布式的特性 我们提到Elasticsearch可以扩展到上百(甚至上千)的服务器来处理PB级的数据.然而我们的例子只给出了一些使用Elastics ...
- [软件工程基础]2017.11.01 第五次 Scrum 会议
具体事项 燃尽图 每人工作内容 成员 已完成的工作 计划完成的工作 工作中遇到的困难 游心 #8 掌握 Laravel 框架 #10 搭建可用的开发测试环境:#9 阅读分析 PhyLab 后端代码与文 ...
- BZOJ1415(期望dp)
解法: 首先bfs预处理go数组:可可在j点时聪聪在点i是怎样贪心走的,这是为了之后O(1)获取转移线路. 然后dfs记忆化一下f[i][j],代表从i到j的期望,对于每层:将所有情况的期望值相加.边 ...
- Technocup 2017 - Elimination Round 1 (Unofficially Open for Everyone, Rated for Div. 2) A
Vasily has a number a, which he wants to turn into a number b. For this purpose, he can do two types ...
- GDB 格式化结构体输出
转载:http://blog.csdn.net/unix21/article/details/9991925 set print addressset print address on打开地址输出,当 ...
- Unity里面两种单例模式的实现
using System; public class Singleton<T> where T : class, new() { private static T m_instance; ...