bzoj1179 [Apio2009]Atm——缩环最长路
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1179
tarjan 缩环,然后求到有酒吧的点的最长路即可;
但一开始想缩环后用拓扑序求答案,不由分说的秒WA了,不知道为什么...
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
typedef long long ll;
int const maxn=5e5+;
int n,m,hd[maxn],ct,deg[maxn],col[maxn],cr,head[maxn],sta[maxn],top;
int st,xt,dfn[maxn],low[maxn],tim,a[maxn],p;
ll val[maxn],ans,dis[maxn];
bool vis[maxn],bar[maxn],b[maxn];
queue<int>q;
struct N{
int to,nxt;
N(int t=,int n=):to(t),nxt(n) {}
}ed[maxn],edge[maxn];
void tarjan(int x)
{
dfn[x]=low[x]=++tim;
vis[x]=; sta[++top]=x;
for(int i=hd[x],u;i;i=ed[i].nxt)
{
if(!dfn[u=ed[i].to])tarjan(u),low[x]=min(low[x],low[u]);
else if(vis[u])low[x]=min(low[x],dfn[u]);
}
if(low[x]==dfn[x])
{
int y; cr++;
while((y=sta[top])!=x)
{
vis[y]=; col[y]=cr; top--;
val[cr]+=a[y];
if(bar[y])b[cr]=;
}
vis[x]=; col[x]=cr; top--;
val[cr]+=a[x]; if(bar[x])b[cr]=;
}
}
//void topo()
//{
// memset(vis,0,sizeof vis);
// for(int i=1;i<=cr;i++)
// if(!deg[i])q.push(i);
// while(q.size())
// {
// int x=q.front(); q.pop();
// if(b[x])vis[x]=1;
// for(int i=head[x],u;i;i=edge[i].nxt)
// {
// if(col[st]==(u=edge[i].to)&&vis[x]){ans=max(ans,val[x]); continue;}
// if(vis[x])vis[u]=1;
// val[u]+=val[x]; deg[u]--;
// if(!deg[u])q.push(u);
// }
// }
// ans+=val[col[st]];
//}
void spfa()
{
memset(vis,,sizeof vis);
q.push(col[st]); dis[col[st]]=val[col[st]]; vis[col[st]]=;
while(q.size())
{
int x=q.front(); q.pop(); vis[x]=;
for(int i=head[x],u;i;i=edge[i].nxt)
{
if(dis[u=edge[i].to]<dis[x]+val[u])
{
dis[u]=dis[x]+val[u];
if(!vis[u])vis[u]=,q.push(u);
}
}
}
for(int i=;i<=cr;i++)
if(b[i])ans=max(ans,dis[i]);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=,x,y;i<=m;i++)
{
scanf("%d%d",&x,&y);
ed[++ct]=N(y,hd[x]); hd[x]=ct;
}
for(int i=;i<=n;i++)scanf("%d",&a[i]);
scanf("%d%d",&st,&p);
for(int i=,x;i<=p;i++)scanf("%d",&x),bar[x]=;
for(int i=;i<=n;i++) if(!dfn[i])tarjan(i);
for(int i=;i<=n;i++)
for(int j=hd[i],u;j;j=ed[j].nxt)
if(col[u=ed[j].to]!=col[i])
{
edge[++xt]=N(col[u],head[col[i]]); head[col[i]]=xt;
// deg[col[i]]++;
}
// topo();
spfa();
printf("%lld\n",ans);
return ;
}
bzoj1179 [Apio2009]Atm——缩环最长路的更多相关文章
- BZOJ1179 : [Apio2009]Atm 缩点+spfa
1179: [Apio2009]Atm Time Limit: 15 Sec Memory Limit: 162 MBSubmit: 2069 Solved: 826[Submit][Status ...
- bzoj1179: [Apio2009]Atm 【缩点+spfa最长路】
题目传送门 Description Siruseri 城中的道路都是单向的.不同的道路由路口连接.按照法律的规定, 在每个路口都设立了一个 Siruser i 银行的 ATM 取款机.令人奇怪的是,S ...
- [BZOJ1179] [Apio2009]Atm(tarjan缩点 + spfa)
传送门 题意 N个点M条边的有向图 每个点有点权 从某一个结点出发 问能获得的最大点权和 一个点的点权最多被计算一次 N<=500000 M<=500000 思路 先tarjan缩点,然后 ...
- bzoj1179: [Apio2009]Atm scc缩点+dag上dp
先把强连通缩点,然后变成了dag,dp求终点是酒吧的最长路即可, /************************************************************** Pro ...
- BZOJ1179 [Apio2009]Atm 【tarjan缩点】
1179: [Apio2009]Atm Time Limit: 15 Sec Memory Limit: 162 MB Submit: 4048 Solved: 1762 [Submit][Sta ...
- 【强联通分量缩点】【最短路】【spfa】bzoj1179 [Apio2009]Atm
缩点后转化成 DAG图上的单源最长路问题.spfa/dp随便. #include<cstdio> #include<queue> #include<algorithm&g ...
- ZOJ 3795:Grouping(缩点+最长路)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5303 题意:有n个人m条边,每条边有一个u,v,代表u的年龄大于等于v,现在要 ...
- poj 3592 Instantaneous Transference 缩点+最长路
题目链接 给一个n*m的图, 从0, 0这个点开始走,只能向右和向下. 图中有的格子有值, 求能获得的最大值. 其中有些格子可以传送到另外的格子, 有些格子不可以走. 将图中的每一个格子都看成一个点, ...
- 【强连通分量+spfa】Bzoj1179 Apio2009 Atm
Description Solution 显然缩强连通分量,然后求最长路,虽然是DAG但还是有点麻烦,于是用了spfa. Code 重建图_数组写错好多次,感觉做这题也就是练了一下实现. #inclu ...
随机推荐
- gitlab 第1次提交代码到1个新仓库
1.如果是本地刚刚搭建好git环境,第一次和gitlab服务器产生连接 参照这个文 https://www.cnblogs.com/kaerxifa/p/10929098.html 2.已经和gitl ...
- 内网jenkins如何配置gitlab自动拉取代码打包
在全局工具配置中添加git安装目录的配置 http://10.2.1.92:8080/jenkins/configureTools/git1.8.3.1/usr/bin/git 打开系统设置配置git ...
- idea和eclipse互相导入方法
1.eclipse maven项目导入idea 只需要在pom.xml文件中加入如下图 为了加载项目里的一些配置文件(例如.xml和.properties文件) 2.idea maven 项目导入ec ...
- Whl自助搜索下载器
本文转载自以下链接:https://github.com/Light-City/AutoDownloadWhl 源码地址: https://github.com/Light-City/AutoDown ...
- 3.filter原理(bitset机制与caching机制)
主要知识点: 一次filter执行顺序 filter和query的特点 一.一次filter执行顺序 1.在倒排索引中查找搜索串,获取document list 以一下date数据来举 ...
- 使用 Cordova 打包 app
1.安装nodejs 2.安装 cordova npm install -g cordova 3.Cordova 打包成安卓APK需要用到ANT打包工具,首先配置好java环境: 下载安装Java J ...
- 联想小新Air 15 安装黑苹果macOS High Sierra 10.13.6过程
联想小新Air 15 安装黑苹果全过程 本文参考:https://blog.csdn.net/qq_28735663/article/details/80634300 本人是联想小新AIr 15 , ...
- copy contents of file with variable number in Matlab
input : transient.case output: transient_1.case, transient_2.case, transient_3.case ... ************ ...
- 调用ms自带的合成语音TTS
通过import of Component导入封装TTS引擎,然后选择: 最后调用: MyVoce := CoSpVoice.Create; MyVoce.Pause;//暂停 MyVoce.Stat ...
- bupt summer training for 16 #4 ——数论
https://vjudge.net/contest/173277#overview A.平方差公式后变为 n = (x + y)(x - y) 令 t = x - y ,变成 n = (t + 2x ...