bzoj1179: [Apio2009]Atm 【缩点+spfa最长路】
Description

Sol
感觉这题思路还是很明了的...先进行一遍tarjan求出图中所有的强连通分量,将他们缩点,然后在这个DAG中跑一遍spfa求最长路。
坑点:好像不能用dijkstra+heap求最长路&&主程序中调用tarjan不能仅一次。
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<cstring>
#define maxn 500090 using namespace std;
typedef long long ll; int n,m,tot,sp,P,dfs_clock,scc_cnt;
int head[maxn],Head[maxn],val[maxn],dfn[maxn],low[maxn],scc[maxn],pub[maxn];
bool vis[maxn];
ll ans,scc_val[maxn],dis[maxn];
struct node{
int to,next;
}edge[maxn],Edge[maxn];
stack<int>st; void add(int x,int y)
{
edge[++tot].to=y;
edge[tot].next=head[x];
head[x]=tot;
} void ADD(int x,int y)
{
Edge[++tot].to=y;
Edge[tot].next=Head[x];
Head[x]=tot;
} void tarjan(int u)
{
dfn[u]=low[u]=++dfs_clock;
st.push(u);
for(int i=head[u];i;i=edge[i].next)
{
int v=edge[i].to;
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(!scc[v]) low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u])
{
scc_cnt++;
while()
{
int x=st.top();st.pop();
scc[x]=scc_cnt;
scc_val[scc_cnt]+=val[x];
if(x==u) break;
}
}
} void dijkstra(int s)
{
priority_queue<pair<ll,int> >q;
for(int i=;i<=scc_cnt;i++) dis[i]=-;
q.push(make_pair(,s));dis[s]=scc_val[s];
while(!q.empty())
{
int u=q.top().second;q.pop();
if(vis[u]) continue;
vis[u]=;
for(int i=Head[u];i;i=Edge[i].next)
{
int v=Edge[i].to;
if(dis[v]<dis[u]+scc_val[v])
{
dis[v]=dis[u]+scc_val[v];
q.push(make_pair(dis[v],v));
}
}
}
} int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
int x=,y=;
scanf("%d%d",&x,&y);
add(x,y);
}
for(int i=;i<=n;i++) scanf("%d",&val[i]);
for(int i=;i<=n;i++)
if(!dfn[i]) tarjan(i);
tot=;
for(int x=;x<=n;x++)
for(int i=head[x];i;i=edge[i].next)
{
int y=edge[i].to;
if(scc[x]!=scc[y])
ADD(scc[x],scc[y]);
}
scanf("%d%d",&sp,&P);
for(int i=;i<=P;i++) scanf("%d",&pub[i]);
dijkstra(scc[sp]);
for(int i=;i<=P;i++)
ans=max(ans,dis[scc[pub[i]]]);
printf("%lld\n",ans);
return ;
}
bzoj1179: [Apio2009]Atm 【缩点+spfa最长路】的更多相关文章
- BZOJ1179 : [Apio2009]Atm 缩点+spfa
1179: [Apio2009]Atm Time Limit: 15 Sec Memory Limit: 162 MBSubmit: 2069 Solved: 826[Submit][Status ...
- 【bzoj1179】[Apio2009]Atm Tarjan缩点+Spfa最长路
题目描述 输入 第一行包含两个整数N.M.N表示路口的个数,M表示道路条数.接下来M行,每行两个整数,这两个整数都在1到N之间,第i+1行的两个整数表示第i条道路的起点和终点的路口编号.接下来N行,每 ...
- Tarjan缩点+Spfa最长路【p3627】[APIO2009] 抢掠计划
Description Siruseri 城中的道路都是单向的.不同的道路由路口连接.按照法律的规定, 在每个路口都设立了一个 Siruseri 银行的 ATM 取款机.令人奇怪的是,Siruseri ...
- 缩点+spfa最长路【bzoj】 1179: [Apio2009]Atm
[bzoj] 1179: [Apio2009]Atm Description Siruseri 城中的道路都是单向的.不同的道路由路口连接.按照法律的规定, 在每个路口都设立了一个 Siruseri ...
- bzoj1179 [Apio2009]Atm——缩环最长路
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1179 tarjan 缩环,然后求到有酒吧的点的最长路即可: 但一开始想缩环后用拓扑序求答案, ...
- [luogu3627 APIO2009] 抢掠计划 (tarjan缩点+spfa最长路)
传送门 Description Input 第一行包含两个整数 N.M.N 表示路口的个数,M 表示道路条数.接下来 M 行,每行两个整数,这两个整数都在 1 到 N 之间,第 i+1 行的两个整数表 ...
- [BZOJ1179][APIO2009][强连通分量Tarjan+spfa]ATM
[BZOJ1179][APIO2009]ATM Input 第一行包含两个整数N.M.N表示路口的个数,M表示道路条数.接下来M行,每行两个整数,这两个整数都在1到N之间,第i+1行的两个整数表示第i ...
- bzoj 1179: [Apio2009]Atm【tarjan+spfa】
明明优化了spfa还是好慢-- 因为只能取一次值,所以先tarjan缩点,把一个scc的点权和加起来作为新点的点权,然后建立新图.在新图上跑spfa最长路,最后把酒吧点的dis取个max就是答案. # ...
- UVA11324 The Largest Clique (强连通缩点+DP最长路)
<题目链接> 题目大意: 给你一张有向图 G,求一个结点数最大的结点集,使得该结点集中的任意两个结点 u 和 v 满足:要么 u 可以达 v,要么 v 可以达 u(u,v相互可达也行). ...
随机推荐
- Intel MIC
http://en.wikipedia.org/wiki/Intel_MIC Intel MIC From Wikipedia, the free encyclopedia Intel Man ...
- Intel Developer Forum
http://en.wikipedia.org/wiki/Intel_Developer_Forum Intel Developer Forum From Wikipedia, the free en ...
- 小心APP应用让你成为“透明人”
随着智能手机和平板电脑的迅猛发展,各式各样的APP在涌入这些移动终端的同一时候.吸费.窃取隐私等恶意程序也随之盛行. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5u ...
- CodeChef - COUNTARI FTT+分块
Arithmetic Progressions Given N integers A1, A2, …. AN, Dexter wants to know how many ways he can ch ...
- 网页 H5“线条” 特效实现方式(canvas-nest)
先上图 (看博客空白处也可以呦): 前一阵浏览网站的时候,发现了这个好玩的东西,一直想找找怎么实现的,今天忙里偷闲,上网搜了一下,发现实现起来特别简单. 只需要在网页body里引入一个<scri ...
- Android:在子线程中更新UI的三种方式
①使用Activity中的runOnUiThread(Runnable) ②使用Handler中的post(Runnable) 在创建Handler对象时,必须先通过Context的getMainLo ...
- 通俗易懂EJB
摘自:http://blog.csdn.net/jojo52013145/article/details/5783677 1. 我们不禁要问,什么是"服务集群"?什么是" ...
- C# 事件处理与自定义事件
http://blog.csdn.net/cyp403/article/details/1514023 图一 ...
- JSP JDBC 读取SQL Server 数据2
<%-- Created by IntelliJ IDEA. User: hellohongfu Date: 2017/12/21 Time: 0:16 To change this templ ...
- 搭建java运行环境
安装IDE 集成开发环境(IDE,Integrated Development Environment ) 1.安装jdk,jre(jdk自带jre),记住他们的安装位置. 2.配置环境变量.(JAV ...