您的位置:首页 > 其它

bzoj1179: [Apio2009]Atm

2017-04-02 15:41 225 查看
传送门

对原图进行tarjan缩点后暴力dp一发水过。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdlib>
#define ll long long
#define inf 0x7fffffff
#define N 500005
using namespace std;
struct edge{int to,next;}e
,ed
;
int head
,he
,f
,be
,v
,c
,dfn
,low
,q
,inq
;
int n,m,s,p,tim,scc,top,cnt,ans;
inline ll read(){
ll k=0,f=1;
char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1;
for(;ch>='0'&&ch<='9';ch=getchar()) k=k*10+ch-'0';
return k*f;
}
void add(int u,int v){
e[++cnt].to=v;
e[cnt].next=head[u];
head[u]=cnt;
}
void ins(int u,int v){
ed[++cnt].to=v;
ed[cnt].next=he[u];
he[u]=cnt;
}
void tar(int x){
int now=0;
dfn[x]=low[x]=++tim;
q[++top]=x;
inq[x]=1;
for (int i=head[x];i;i=e[i].next)
if (!dfn[e[i].to]){
tar(e[i].to);
low[x]=min(low[x],low[e[i].to]);
}
else if (inq[e[i].to]) low[x]=min(low[x],dfn[e[i].to]);
if (low[x]==dfn[x]){
scc++;
while (now!=x){
now=q[top];
top--;
be[now]=scc;
v[scc]+=c[now];
inq[now]=0;
}
}
}
void rebuild(){
cnt=0;
for (int i=1;i<=n;i++)
for (int j=head[i];j;j=e[j].next)
if (be[i]!=be[e[j].to]) ins(be[i],be[e[j].to]);
}
void spfa(){
int h=0,t=1;
q[1]=be[s];
inq[q[1]]=1;
f[q[1]]=v[q[1]];
while (h!=t){
h++;
if (h>500000) h=1;
int x=q[h];
inq[x]=0;
for (int i=he[x];i;i=ed[i].next)
if (f[x]+v[ed[i].to]>f[ed[i].to]){
f[ed[i].to]=f[x]+v[ed[i].to];
if (!inq[ed[i].to]){
inq[ed[i].to]=1;
t++;
if (t>500000) t=1;
q[t]=ed[i].to;
}
}
}
}
int main(){
n=read();
m=read();
for (int i=1;i<=m;i++){
int x,y;
x=read();
y=read();
add(x,y);
}
for (int i=1;i<=n;i++) c[i]=read();
for (int i=1;i<=n;i++) if (!dfn[i]) tar(i);
s=read();
p=read();
rebuild();
spfa();
for (int i=1;i<=p;i++){
int x=read();
if (f[be[x]]>ans) ans=f[be[x]];
}
printf("%d",ans);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: