0分求助!QAQ
查看原帖
0分求助!QAQ
393674
jixiang楼主2022/3/24 16:11
#include<bits/stdc++.h>
#define rep(i,j,k) for(int i=j;i<=k;i++)
using namespace std;
const int N = 5e4+9;
const int M = 4e5+9;
const int INF= 1e9+9;
int h[N], e[M], ne[M], idx;
int cur[N],d[N],f[M];
int A[N],l[M];
int n,m,S,T;
int s,t;

void add(int a, int b, int c,int d)
{

    l[idx] = c; e[idx] = b, f[idx] = d-c, ne[idx] = h[a], h[a] = idx ++ ;
    e[idx] = a, f[idx] = 0, ne[idx] = h[b], h[b] = idx ++ ;
}

template <typename T> void inline read(T &x)
{
    x= 0 ; bool f= 0; char ch  = getchar();
    while (ch < '0' || ch > '9') {if(ch == '-')f= 1; ch = getchar();}
    while (ch <='9' && ch >= '0') {x= (x<<1)+ (x<<3) + ch - '0'; ch = getchar();}
    if(f)x*=-1;
}

inline void wri(int x)
{
    if(x<0)putchar('-'), x= -x;
    if(x>9)wri(x/10);
    putchar(x%10 +'0');
}

bool bfs()
{
    queue<int>q;
    memset(d,-1,sizeof d);
    d[S]= 0; q.push(S); cur[S]=h[S];
    while (q.size())
    {
        int top = q.front();
        q.pop();
        for(int i=h[top];~i;i=ne[i])
        {
            int v=e[i];
            if(d[v]==-1&&f[i])
            {
                d[v]=d[top]+1;
                cur[v]=h[v];
                if(v==T) return 1;
                q.push(v);
            }
        }
    }
    return 0;
}

int find (int u,int lim)
{
    if(u==T)return lim;
    int flow  = 0;
    for(int i=cur[u];~i&&flow<lim;i=ne[i])
    {
        cur[u]=i;
        int v=e[i];
        if(d[v]==d[u]+1&&f[i])
        {
            int tx = find(v,min(f[i],lim-flow));
            if(!tx) d[v]=-1;
            f[i]-=tx; f[i^1]+=tx; flow+=tx;
        }
    }
    return flow;
}

int Dinic()
{
    int res =0;
    int flow;
    while(bfs())while(flow=find(S,INF))res+=flow;
    return res;
}

void work()
{
    idx = 0;
    memset(ne,0,sizeof ne);
    memset(e,0,sizeof e);
    memset(f,0,sizeof f);
    memset(A,0,sizeof A);
    memset(l,0,sizeof l);
    memset(h, -1, sizeof h);

    /// 1-n 表示天数,n+1 到 n+m+1 表示少女
    /// 天数从s连边,规定上界
    /// 少女和t连边,规定下界

    s = 0;
    t = n+m+1;
    S = n+m+5;
    T = n+m+8;


    /// 少女
    rep(i,1,m)
    {
        int low,pos;
        pos=i+n;
        read(low);
        A[pos]-=low;
        A[t]+=low;
        add(pos,t,low,INF);
    }

    /// 天数
    rep(i,1,n)
    {
        int c,d;
        read(c); read(d);
        add(s,i,0,d);

        rep(j,1,c)
        {
            int num,a,b;
            read(num);
            num+=n+1;

            read(a); read(b);
            A[i]-=a;
            A[num]+=a;

            add(i,num,a,b);
        }
    }

    add(t,s,0,INF);

    int tot = 0;

    rep(i,1,n+m)
    {
        if(A[i]>0) add(S,i,0,A[i]),tot+=A[i];
        else if(A[i]<0) add(i,T,0,-A[i]);
    }

    if(Dinic()==tot)
    {

        S= s; T = t;
        f[idx-1]=f[idx-2]= 0;
        wri(Dinic());
    }

    else puts("-1");
    puts("");
    puts("");
}

int main()
{
    while(cin>>n>>m) work();
    return 0;
}



2022/3/24 16:11
加载中...