题目链接在此

有环当然一定尽量走环,这是搞缩点的人都知道的常识。

建了新图之后搞点权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抢掠计划(缩点最短路)的更多相关文章

  1. Luogu P3627 抢掠计划

    题目传送门 \(Tarjan\)缩点+SPFA最长路 #include<iostream> #include<cstdio> #include<cstring> # ...

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

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

  3. 洛谷 P3627 【抢掠计划】

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

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

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

  5. P3627 [APIO2009]抢掠计划

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

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

    抢掠计划 题目链接 比较水的缩点模板题,Tarjan缩点,重新建图,记录联通块的钱数.是否有酒吧 DAG上记忆化搜索即可 #include<iostream> #include<cs ...

  7. 洛谷 题解 P3627 【[APIO2009]抢掠计划】

    图论 tarjan缩点+最短路 的一道题 tarjan求强连通分量(为以后缩点打下良好的基础) (如果不会tarjan的请点击这儿) 你需要的东西: (1).dfn[],表示这个点在dfs时是第几个被 ...

  8. bzoj 1179 [Apio2009]Atm 缩点+最短路

    [Apio2009]Atm Time Limit: 15 Sec  Memory Limit: 162 MBSubmit: 4290  Solved: 1893[Submit][Status][Dis ...

  9. APIO2009 抢掠计划 Tarjan DAG-DP

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

随机推荐

  1. 在页面实现qq跳转链接

    http://shang.qq.com/v3/widget/consult.html

  2. Android setVisibility(View.GONE)无效的问题及原因分析

    解决方案:可以在setVisibility()之前调用clearAnimation()方法清除掉动画,或setFillAfter(false)(时间上该函数内部也调用了clearAnimation() ...

  3. 如何修改tomcat的启动方式为 run

    tomcat根目录\bin\startup.bat,记事本打开,找到: call "%EXECUTABLE%" start %CMD_LINE_ARGS% :end 把start ...

  4. Servlet Context

    Servlet Context Container Provider 负责提供ServletContext的实现. A ServletContext is rooted at a known path ...

  5. Luogu P5351 Ruri Loves Maschera

    先ORZ\(Owen\)一发.感觉是个很套路的题,这里给一个蒟蒻的需要特判数据的伪\(n\log^2 n\)算法,真正的两只\(\log\)的还是去看标算吧(但这个好想好写跑不满啊) 首先这种树上路径 ...

  6. kubernetes概念

    kubernetes blog cluster cluster是计算.存储.和网络资源的集合,kubernetes利用这些资源运行各种基于容器的应用. master master是cluster的大脑 ...

  7. springboot上传linux文件无法浏览,提示404错误

    1.配置文件地址置换 @Componentclass WebConfigurer implements WebMvcConfigurer { @Autowired ConfigUtil bootdoC ...

  8. 学习C语言库函数

    使用C语言功能强大的函数,我们需要包含头文件 #include<math.h>. 1)取两个数的较大值或较小值函数: double a = 9.9; double b = 6.6; pri ...

  9. 674. Longest Continuous Increasing Subsequence@python

    Given an unsorted array of integers, find the length of longest continuous increasing subsequence (s ...

  10. centOS下jenkins

    转:centos7搭建jenkins小记 转自:https://segmentfault.com/a/1190000007086764 安装java环境 1.查看服务器版本 centos7,继续. c ...