【Luogu】P3627抢掠计划(缩点最短路)
题目链接在此
有环当然一定尽量走环,这是搞缩点的人都知道的常识。
建了新图之后搞点权SPFA跑最长路。枚举每个酒吧选择最大值。
发现我的博客写的越来越水了
#include<cstdio>
#include<cctype>
#include<cstring>
inline long long read(){
long long num=,f=;
char ch=getchar();
while(!isdigit(ch)){
if(ch=='-') f=-;
ch=getchar();
}
while(isdigit(ch)){
num=num*+ch-'';
ch=getchar();
}
return num*f;
} inline long long max(long long a,long long b){ return a>b?a:b; }
inline long long min(long long a,long long b){ return a<b?a:b; } struct Edge{
int next,to;
}; struct Pic{
Edge edge[];
int head[],num;
inline void add(int from,int to){
edge[++num]=(Edge){head[from],to};
head[from]=num;
}
}Old,New; int que[];
int fsh[];
int f[],h,t=;
int stack[],top;
int dfn[],low[],ID;
int col[],cnt;
int val[];
bool vis[];
int dis[];
void tarjan(int x){
vis[x]=;
dfn[x]=low[x]=++ID;
stack[++top]=x;
for(int i=Old.head[x];i;i=Old.edge[i].next){
int to=Old.edge[i].to;
if(!dfn[to]){
tarjan(to);
low[x]=min(low[x],low[to]);
}
else if(vis[to]) low[x]=min(low[x],dfn[to]);
}
if(dfn[x]==low[x]){
vis[x]=;
col[x]=++cnt;
val[cnt]+=que[x];
while(stack[top]!=x){
vis[stack[top]]=;
val[cnt]+=que[stack[top]];
col[stack[top--]]=cnt;
}
top--;
}
}
int ans;
int main(){
int n=read(),m=read();
for(int i=;i<=m;++i){
int from=read(),to=read();
Old.add(from,to);
}
for(int i=;i<=n;++i) que[i]=read();
int s=read(),p=read();
for(int i=;i<=p;++i) fsh[i]=read();
for(int i=;i<=n;++i)
if(!dfn[i]) tarjan(i);
for(int i=;i<=n;++i)
for(int j=Old.head[i];j;j=Old.edge[j].next){
int to=Old.edge[j].to;
if(col[i]!=col[to]) New.add(col[i],col[to]);
}
f[]=col[s];
memset(vis,,sizeof(vis));
dis[col[s]]=val[col[s]];
while(h<t){
h++;
int from=f[h];
vis[from]=;
for(int i=New.head[from];i;i=New.edge[i].next){
int to=New.edge[i].to;
if(dis[to]<dis[from]+val[to]){
dis[to]=dis[from]+val[to];
if(!vis[to]){
vis[to]=;
f[++t]=to;
}
}
}
}
for(int i=;i<=p;++i) ans=max(ans,dis[col[fsh[i]]]);
printf("%d",ans);
return ;
}
【Luogu】P3627抢掠计划(缩点最短路)的更多相关文章
- Luogu P3627 抢掠计划
题目传送门 \(Tarjan\)缩点+SPFA最长路 #include<iostream> #include<cstdio> #include<cstring> # ...
- 洛谷 P3627 [APIO2009](抢掠计划 缩点+spfa)
题目描述 Siruseri 城中的道路都是单向的.不同的道路由路口连接.按照法律的规定, 在每个路口都设立了一个 Siruseri 银行的 ATM 取款机.令人奇怪的是,Siruseri 的酒吧也都设 ...
- 洛谷 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 ...
- 洛谷 题解 P3627 【[APIO2009]抢掠计划】
图论 tarjan缩点+最短路 的一道题 tarjan求强连通分量(为以后缩点打下良好的基础) (如果不会tarjan的请点击这儿) 你需要的东西: (1).dfn[],表示这个点在dfs时是第几个被 ...
- bzoj 1179 [Apio2009]Atm 缩点+最短路
[Apio2009]Atm Time Limit: 15 Sec Memory Limit: 162 MBSubmit: 4290 Solved: 1893[Submit][Status][Dis ...
- APIO2009 抢掠计划 Tarjan DAG-DP
APIO2009 抢掠计划 Tarjan spfa/DAG-DP 题面 一道\(Tarjan\)缩点水题.因为可以反复经过节点,所以把一个联通快中的所有路口看做一个整体,缩点后直接跑\(spfa\)或 ...
随机推荐
- matlab均方根误差
Matlab均方根误差的计算 http://blog.sina.com.cn/s/blog_6210f654010308kv.html
- ycsb安装和使用介绍
nosql性能测试工具ycsb0.1的使用 使用文档参考地址:https://www.cnblogs.com/SailorXiao/p/5808828.html ycsb地址:https://gith ...
- 初习mysql procedure
1.存储过程简介 我们常用的操作数据库语言SQL语句在执行的时候需要要先编译,然后执行,而存储过程(Stored Procedure)是一组为了完成特定功能的SQL语句集,经编译后存储在数据库中,用户 ...
- Mybatis-Generator逆向生成Po,Mapper,XMLMAPPER(idea)
前文有一篇手工生成的说明,地址: http://www.cnblogs.com/xiaolive/p/4874605.html, 现在这个补充一下在idea里面的自动版本的数据库逆向生成工具: 一.g ...
- Codeforces Round #317 (Div. 2) D Minimization (贪心+dp)
D. Minimization time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- GUI进化--数据与界面分离
http://blog.csdn.net/doon/article/details/5946862 1.何谓数据和界面分离? GUI,即Graphic User Interface,人机交换界面.连接 ...
- 【转】SpringBoot 2.0.0新版和SpringBoot1.5.2版本中Tomcat配置的差别
https://blog.csdn.net/wd2014610/article/details/79587161 2018年春SpringBoot 2.0.0 新版本有了很多新的改变,其中Tomcat ...
- myeclipse 导入项目时no projects are found to import解决办法
myeclipse 识别一个工程需要.classpath与.project文件,一般无需提交SVN所以项目切下来的时候是没有这两个文件的. 方法1: 1) 在myeclipse中新建一个和你要导入的项 ...
- Kernel Stack Overflow(转)
0x00 漏洞代码 stack_smashing.c #include <linux/init.h> #include <linux/module.h> #include &l ...
- (49)zabbix事件是什么?事件来源有哪些分类
什么是zabbix 事件 在trigger的文章内,我们已经有用到事件,这个事件要讲概念真心不知道怎么说,就拿trigger事件来说,如果trigger从当前值ok转变为problem,那么我们称之为 ...