bzoj1179: [Apio2009]Atm

传送门
对原图进行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[N],ed[N];
int head[N],he[N],f[N],be[N],v[N],c[N],dfn[N],low[N],q[N],inq[N];
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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值