【BZOJ1179】[Apio2009]Atm (tarjan+SPFA)
显而易见的tarjan+spfa...不解释了
const maxn=;
type
edgetype=record
toward,next:longint;
end; var
edge1,edge2:array[..maxn] of edgetype;
first1,first2,scc,stack,dfn,low,val,sum,q,d:Array[..maxn] of longint;
pd:array[..maxn] of boolean;
num,tot,n,m,cnt,top:longint; function min(x,y:longint):longint; begin if x<y then exit(x) else exit(y); end; procedure addedge(i,j:longint);
begin
inc(tot);
edge1[tot].toward:=j;
edge1[tot].next:=first1[i];
first1[i]:=tot;
end; procedure add(i,j:longint);
begin
inc(tot);
edge2[tot].toward:=j;
edge2[tot].next:=first2[i];
first2[i]:=tot;
end; procedure tarjan(v:longint);
var i,tmp,u:longint;
begin
inc(cnt); dfn[v]:=cnt; low[v]:=cnt;
inc(top); stack[top]:=v;
pd[v]:=true; i:=first1[v];
while i<> do
begin
tmp:=edge1[i].toward;
if dfn[tmp]= then
begin
tarjan(tmp);
low[v]:=min(low[v],low[tmp]);
end
else if pd[tmp] then low[v]:=min(low[v],dfn[tmp]);
i:=edge1[i].next;
end;
if low[v]=dfn[v] then
begin
inc(num);
repeat
u:=stack[top]; dec(top);
pd[u]:=false; scc[u]:=num;
inc(sum[num],val[u]);
until u=v;
end;
end; procedure spfa(s:longint);
var head,tail,i,j,tmp:longint;
begin
fillchar(d,sizeof(d),);
fillchar(pd,sizeof(pd),false);
head:=;
tail:=;
pd[scc[s]]:=true;
q[]:=scc[s];
d[scc[s]]:=sum[scc[s]];
while head<>tail do
begin
inc(head);
if head>maxn then head:=;
j:=q[head];
i:=first2[j];
while i<> do
begin
tmp:=edge2[i].toward;
if d[j]+sum[tmp]>d[tmp] then
begin
d[tmp]:=d[j]+sum[tmp];
if not pd[tmp] then
begin
inc(tail); if tail>maxn then tail:=;
q[tail]:=tmp;
pd[tmp]:=true;
end;
end;
i:=edge2[i].next;
end;
pd[j]:=false;
end;
end; procedure init;
var i,j,a,b:longint;
begin
read(n,m);
for i:= to m do
begin
readln(a,b);
addedge(a,b);
end;
for i:= to n do read(val[i]);
for i:= to n do if dfn[i]= then tarjan(i);
end; procedure solve;
var p,x,ans,s,i,j:longint;
begin
read(s,p);
tot:=;
for j:= to n do
begin
i:=first1[j];
while i<> do
begin
x:=edge1[i].toward;
if scc[x]<>scc[j] then add(scc[j],scc[x]);
i:=edge1[i].next;
end;
end;
spfa(s);
ans:=;
for i:= to p do
begin
read(x);
if d[scc[x]]>ans then ans:=d[scc[x]];
end;
writeln(ans);
end; Begin
init;
solve;
End.
【BZOJ1179】[Apio2009]Atm (tarjan+SPFA)的更多相关文章
- 【BZOJ1179】 [Apio2009]Atm tarjan缩点+SPFA
Description Input 第一行包含两个整数N.M.N表示路口的个数,M表示道路条数.接下来M行,每行两个整数,这两个整数都在1到N之间,第i+1行的两个整数表示第i条道路的起点和终点的路口 ...
- 【bzoj1179】[Apio2009]Atm Tarjan缩点+Spfa最长路
题目描述 输入 第一行包含两个整数N.M.N表示路口的个数,M表示道路条数.接下来M行,每行两个整数,这两个整数都在1到N之间,第i+1行的两个整数表示第i条道路的起点和终点的路口编号.接下来N行,每 ...
- 【bzoj1179】 Apio2009—Atm
www.lydsy.com/JudgeOnline/problem.php?id=1179 (题目链接) 题意 给出一张有向图,每个节点有点权.标记一些点,找出一条路径,可以重复经过一条边,使得总点权 ...
- 【bzoj1179】[Apio2009]Atm
*题目描述: *输入: 第一行包含两个整数N.M.N表示路口的个数,M表示道路条数.接下来M行,每行两个整数,这两个整数都在1到N之间,第i+1行的两个整数表示第i条道路的起点和终点的路口编号.接下来 ...
- bzoj 1179[Apio2009]Atm (tarjan+spfa)
题目 输入 第一行包含两个整数N.M.N表示路口的个数,M表示道路条数.接下来M行,每行两个整数,这两个整数都在1到N之间,第i+1行的两个整数表示第i条道路的起点和终点的路口编号.接下来N行,每行一 ...
- 【BZOJ4651】【NOI2016】网格(Tarjan,哈希)
[BZOJ4651][NOI2016]网格(Tarjan,哈希) 题面 BZOJ 洛谷 题解 首先把题目稍微变得好说一些,给定一个网格,已经删去了若干个格子 问最少删去多少个格子使得图不连通. 这题的 ...
- 【BZOJ-1179】Atm Tarjan + SPFA
1179: [Apio2009]Atm Time Limit: 15 Sec Memory Limit: 162 MBSubmit: 2407 Solved: 993[Submit][Status ...
- 【Tarjan】+【SPFA】APIO2009 Atm
一.算法介绍 tarjan——求解有向图强连通分量.这个算法在本人的一篇blog中有介绍,这里就不赘述了.贴上介绍tarjan的的blog链接:http://www.cnblogs.com/Maki- ...
- 【BZOJ 1179】[Apio2009]Atm
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] tarjan强连通缩点一下. 然后把缩点之后,每个点的钱的数累加起来. 然后从S出发 开始一边做bfs一遍做dp. 最后输出有酒吧的 ...
随机推荐
- java后台输入数据的2种方式
java后台输入数据的2种方式 (1) import java.io.BufferedReader; import java.io.InputStreamReader; public class 输入 ...
- LArea 微信端 地址选择
最近做到一个项目,微信端的商城需要地址选择功能 在百度上看了,采用LArea.js....下载实例,在移动端模拟器上运行是比较好的, 在微信上模拟后出现很多问题, 1,出现undefined 都定义正 ...
- Docker自学纪实(五) 使用Dockerfile构建php网站环境镜像
一般呢,docker构建镜像容器的方式有两种:一种是pull dockerhub仓库里面的镜像,一种是使用Dockerfile自定义构建镜像. 很多时候,公司要求的镜像并不一定符合dockerhub仓 ...
- 使用CSS隐藏HTML元素的四种常用方法
CSS隐藏HTML元素的四种常用方法 1.opacity:设置opacity: 0可以使一个元素变得完全透明. 设置的透明度会被子元素继承,而且无法取消. 通常可以使用opacity属性来制作元素的淡 ...
- LAMP架构的搭建
什么是LAMP架构? L : Linux,2.6.18-308.el5(redhat5.8) A :Apache,httpd 2.4.4 M : mysql-5.5.28 P : php-5.4. ...
- POJ:2139-Six Degrees of Cowvin Bacon
传送门:http://poj.org/problem?id=2139 Six Degrees of Cowvin Bacon Time Limit: 1000MS Memory Limit: 6553 ...
- aspx页面 按钮不响应回车键
aspx页面在IE浏览器中,页面上的按钮默认都响应回车键,但有的时候我们的文本框可能需要响应回车键,这时我们就不想让按钮再响应回车键,这时我们只需要设置按钮的属性即可. 按钮分为两种,一种是<b ...
- springboot 入门2 开发环境与生产环境采用不同配置问题
目开发中我们通常有两套配置信息 分别配置了我们的数据源信息等? 那么我们要如何不通过修改配置文件大量配置来实现简单的修改与配置来实现相关配置加载功能 首先springboot 有一个核心的配置文件a ...
- 剑指Offer - 九度1510 - 替换空格
剑指Offer - 九度1510 - 替换空格2013-11-29 20:53 题目描述: 请实现一个函数,将一个字符串中的空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之 ...
- 《数据结构》C++代码 栈与队列
线性表中,先进先出的叫队列,先进后出的叫栈.队列常用于BFS,而在函数递归层数过高时,需要手动实现递归过程,这时候便需要写一个“手动栈”. 有时候,我们会有大量数据频繁出入队列,但同时存在其内的元素却 ...