其他分享
首页 > 其他分享> > 【JZOJ6346】ZYB和售货机

【JZOJ6346】ZYB和售货机

作者:互联网

description


analysis


code

#pragma GCC optimize("O3")
#pragma G++ optimize("O3")
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define MAXN 100005
#define MAXM MAXN*2
#define ha 19260817
#define ll long long
#define reg register ll
#define fo(i,a,b) for (reg i=a;i<=b;++i)
#define fd(i,a,b) for (reg i=a;i>=b;--i)
#define rep(i,a) for (reg i=last[a];i;i=next[i])

using namespace std;

ll last[MAXM],next[MAXM],tov[MAXM];
ll fa[MAXN],a[MAXN],c[MAXN],d[MAXN];
ll dfn[MAXN],num[MAXN],val[MAXN],mx[MAXN],smx[MAXN];
ll bz[MAXN],flag[MAXN];
ll n,mn=ha,tot,ans,where;

inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while (ch<'0' || '9'<ch){if (ch=='-')f=-1;ch=getchar();}
    while ('0'<=ch && ch<='9')x=x*10+ch-'0',ch=getchar();
    return x*f;
}
inline ll max(ll x,ll y){return x>y?x:y;}
inline ll min(ll x,ll y){return x<y?x:y;}
inline void link(ll x,ll y){next[++tot]=last[x],last[x]=tot,tov[tot]=y;}
/*
inline void findcircle(ll x)
{
    if (dfn[x]==tot)
    {
        fo(i,1,n)if (dfn[x]==tot)bz[i]=1;
        return;
    }
    if (dfn[x])return;
    dfn[x]=tot;if (x!=fa[x])findcircle(fa[x]);
}
inline void dfs(ll x)
{
    ans+=val[mx[x]]*a[x];
    rep(i,x)dfs(tov[i]);
}*/

inline void dfs1(ll x)
{
    if (flag[x]==tot){ans-=mn;return;}
    if (flag[x])return;
    flag[x]=tot,mn=min(mn,val[mx[x]]-val[smx[x]]);
    if (mx[x]!=x)dfs1(mx[x]);
}

int main()
{
    //freopen("T2.in","r",stdin);
    freopen("goods.in","r",stdin);
    freopen("goods.out","w",stdout);
    n=read();
    fo(i,1,n)fa[i]=read(),c[i]=read(),d[i]=read(),a[i]=read(),link(fa[i],i);
    fo(i,1,n)
    {
        val[i]=d[fa[i]]-c[i];
        if (val[i]<0)continue;
        if (val[i]>val[mx[fa[i]]])smx[fa[i]]=mx[fa[i]],mx[fa[i]]=i;
        else if (val[i]>val[smx[fa[i]]])smx[fa[i]]=i;
    }
    tot=0;
    //fo(i,1,n)if (!dfn[i])++tot,findcircle(i);
    fo(i,1,n)ans+=val[mx[i]]*a[i];
    tot=0;
    fo(i,1,n)if (!flag[i])++tot,mn=ha,dfs1(i);
    printf("%lld\n",ans);
    return 0;
}

标签:JZOJ6346,ll,tot,fa,MAXN,ZYB,mx,售货机,define
来源: https://www.cnblogs.com/horizonwd/p/11498461.html