明明优化了spfa还是好慢……

因为只能取一次值,所以先tarjan缩点,把一个scc的点权和加起来作为新点的点权,然后建立新图。在新图上跑spfa最长路,最后把酒吧点的dis取个max就是答案。

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int N=500005,inf=1e9;
int n,m,h[N],cnt,dis[N],st,p,a[N],b[N],ans,dfn[N],low[N],tot,bl[N],col,s[N],top,x[N],y[N],va[N];
bool v[N];
struct qwe
{
int ne,to;
}e[N];
int read()
{
int r=0,f=1;
char p=getchar();
while(p<'0'||p>'9')
{
if(p=='-')
f=-1;
p=getchar();
}
while(p>='0'&&p<='9')
{
r=r*10+p-48;
p=getchar();
}
return r*f;
}
void add(int u,int v)
{
cnt++;
e[cnt].ne=h[u];
e[cnt].to=v;
h[u]=cnt;
}
void tarjan(int u)
{
low[u]=dfn[u]=++tot;
v[s[++top]=u]=1;
for(int i=h[u];i;i=e[i].ne)
{
if(!dfn[e[i].to])
{
tarjan(e[i].to);
low[u]=min(low[u],low[e[i].to]);
}
else if(v[e[i].to])
low[u]=min(low[u],dfn[e[i].to]);
}
if(low[u]==dfn[u])
{
col++;
while(s[top]!=u)
{
bl[s[top]]=col;
va[col]+=a[s[top]];
v[s[top--]]=0;
}
bl[s[top]]=col;
va[col]+=a[s[top]];
v[s[top--]]=0;
}
}
int main()
{
n=read(),m=read();
for(int i=1;i<=m;i++)
{
x[i]=read(),y[i]=read();
add(x[i],y[i]);
}
for(int i=1;i<=n;i++)
a[i]=read();
st=read(),p=read();
for(int i=1;i<=p;i++)
b[i]=read();
for(int i=1;i<=n;i++)
if(!dfn[i])
tarjan(i);
memset(v,0,sizeof(v));
memset(h,0,sizeof(h));
cnt=0;
for(int i=1;i<=m;i++)
if(bl[x[i]]!=bl[y[i]])
add(bl[x[i]],bl[y[i]]);
for(int i=1;i<=n;i++)
dis[i]=-inf;
deque<int>q;
q.push_back(bl[st]);
v[bl[st]]=1;
dis[bl[st]]=va[bl[st]];
while(!q.empty())
{
int u=q.front();
q.pop_front();
v[u]=0;
for(int i=h[u];i;i=e[i].ne)
if(dis[e[i].to]<dis[u]+va[e[i].to])
{
dis[e[i].to]=dis[u]+va[e[i].to];
if(!v[e[i].to])
{
v[e[i].to]=1;
if(q.empty()||dis[q.front()]<dis[e[i].to])
q.push_back(e[i].to);
else
q.push_front(e[i].to);
}
}
}
for(int i=1;i<=p;i++)
if(dis[bl[b[i]]]>ans)
ans=dis[bl[b[i]]];
printf("%d\n",ans);
return 0;
}

bzoj 1179: [Apio2009]Atm【tarjan+spfa】的更多相关文章

  1. 【BZOJ】1179: [Apio2009]Atm(tarjan+spfa)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1179 缩点建图... #include <cstdio> #include <cs ...

  2. BZOJ 1179: [Apio2009]Atm( tarjan + 最短路 )

    对于一个强连通分量, 一定是整个走或者不走, 所以tarjan缩点然后跑dijkstra. ------------------------------------------------------ ...

  3. BZOJ1179 [Apio2009]Atm 【tarjan缩点】

    1179: [Apio2009]Atm Time Limit: 15 Sec  Memory Limit: 162 MB Submit: 4048  Solved: 1762 [Submit][Sta ...

  4. bzoj 1179[Apio2009]Atm (tarjan+spfa)

    题目 输入 第一行包含两个整数N.M.N表示路口的个数,M表示道路条数.接下来M行,每行两个整数,这两个整数都在1到N之间,第i+1行的两个整数表示第i条道路的起点和终点的路口编号.接下来N行,每行一 ...

  5. 【tarjan+SPFA】BZOJ1179-[Apio2009]Atm

    [题目大意] 给出一张有点权的有向图,已知起点和可以作为终点的一些点,问由起点出发,每条边和每个点可以经过任意多次,经过点的权值总和最大为多少. [思路] 由于可以走任意多次,显然强连通分量可以缩点. ...

  6. BZOJ 1179 [Apio2009]Atm(强连通分量)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1179 [题目大意] 给出一张有向带环点权图,给出一些终点,在路径中同一个点的点权只能累 ...

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

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

  8. bzoj 1179 [Apio2009]Atm——SCC缩点+spfa

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1179 显然SCC缩点. 然后准备倒着拓扑序推到st,结果WA. 听TJ说dj求最长路会发生不 ...

  9. bzoj 1179: [Apio2009]Atm

    Description Input 第 一行包含两个整数N.M.N表示路口的个数,M表示道路条数.接下来M行,每行两个整数,这两个整数都在1到N之间,第i+1行的两个整数表示第i条道路 的起点和终点的 ...

随机推荐

  1. struts2中的session使用

    1.1. 如何获取Session 1.1.1. 获取Session的方式 Struts2中获取Session的方式有3种,大家掌握其中任何一种都可以. 通过ActionContext.getConte ...

  2. CoolCTO - 创业者的技术合伙人

    CoolCTO - 创业者的技术合伙人

  3. c++之虚基类初始化

    C++虚基类构造函数下面文章详细介绍C++虚基,所谓C++虚基类:是由最派生类的构造函数通过调用虚基类的构造函数进行初始化的,但前提是要深入理解到底什么是C++虚基类,及他是怎么运行的. 前面讲过,为 ...

  4. redis connetced refused remote

    239down vote I've been stuck with the same issue, and the preceding answer did not help me (albeit w ...

  5. Java 8 中的 java.util.Optional

    Java 8 中的 java.util.Optional 学习了:https://blog.csdn.net/sun_promise/article/details/51362838 package ...

  6. [TypeScript] Transform Existing Types Using Mapped Types in TypeScript

    Mapped types are a powerful and unique feature of TypeScript's type system. They allow you to create ...

  7. ExpandableListView的使用以及信息的高亮显示

    ExpandableListView是ListView控件的延伸,它能够对数据进行分组显示和隐藏,并统计总数量.可进行滚动,对某一内容高亮显示. <1>编写xml布局文件,用于获取Expa ...

  8. leetCode 67.Add Binary (二进制加法) 解题思路和方法

    Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...

  9. SQLite Expert表分离和解决SQLite Expert删除表后大小不变的问题

    最后要使用到号码归属地的查询,在网上找到一个数据库文件.大小有12M多,压缩成zip也有1.9M,这样对于一个apk的大小非常不利,后来看了一下数据库的内容,发现有非常多冗余.特别是中文字符占用非常大 ...

  10. 网络基础笔记——OSI七层模型

    OSI七层模型 由于整个网络连接的过程相当复杂,包含硬件.软件数据封包与应用程序的互相链接等等.假设想要写一支将联网所有功能都串连在一块的程序.那么当某个小环节出现故障时,整仅仅程序都须要改写.所以我 ...