Dinic TLE 求助
查看原帖
Dinic TLE 求助
378706
MoyunAllgorithm楼主2023/12/27 21:05
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int MAXN=1055,MAXM=25005;
const LL INF=1e17;
int N,C,B,M,S,T;
int ntot=1;
struct Edge
{
    int to,nxt,c;
}net[MAXM<<1];
int hd[MAXN],cur[MAXN],r[MAXN];
LL dis[MAXN];
int rk[MAXN][25];
void Add(int u,int v,int c)
{
   // printf("add#%d:%d %d %d\n",ntot+1,u,v,c);
    net[++ntot]={v,hd[u],c};
    hd[u]=ntot;
    net[++ntot]={u,hd[v],0};
    hd[v]=ntot;
    return;
}
bool bfs()
{
    for(int i=1;i<=N;i++) dis[i]=INF;
    queue<int>q;
    q.push(S);
    dis[S]=0;
    while(q.size())
    {
        int u=q.front();
        q.pop();
        cur[u]=hd[u];
        if(u==T) return 1;
        for(int i=hd[u];i;i=net[i].nxt)
        {
            int v=net[i].to,c=net[i].c;
            if(c>0&&dis[v]==INF)
            {
                dis[v]=dis[u]+1;
                q.push(v);
            }
        }
    }
    return 0;
}
LL dfs(int u,LL flow)
{
    if(u==T) return flow;
    LL res=0;
    for(int i=cur[u];i;i=net[i].nxt)
    {
        int v=net[i].to,c=net[i].c;
        cur[u]=i;
        if(c>0&&dis[v]==dis[u]+1)
        {
            LL mf=dfs(v,min(flow,1ll*c));
            if(mf==0) dis[v]=INF;
            net[i].c-=mf,net[i^1].c+=mf;
            flow-=mf,res+=mf;
        }
    }
    return res;
}
LL Dinic()
{
    LL res=0;
    while(bfs())
    {
        res+=dfs(S,INF);
    }
    return res;
}
bool Check(int x)
{
    for(int i=1;i+x-1<=B;i++) 
    {
        int j=i+x-1;
        ntot=1;
      //  printf({})
        for(int k=1;k<=N;k++) hd[k]=0;
        for(int k=1;k<=C;k++) Add(S,k,1);
        for(int k=1;k<=B;k++) Add(k+C,T,r[k]);
        for(int k=1;k<=C;k++)
            for(int p=i;p<=j;p++)
                Add(k,rk[k][p]+C,1);
        if(Dinic()==C) return 1;
       // puts("~~~");
    }
    return 0;
}
int main()
{
    scanf("%d %d",&C,&B);
    for(int i=1;i<=C;i++)
        for(int j=1;j<=B;j++)
            scanf("%d",&rk[i][j]);
    for(int i=1;i<=B;i++) scanf("%d",&r[i]);
    //puts("ENDOFINPUT");
    S=C+B+1,T=C+B+2;
    N=T;
    int ans,l=1,r=B;
    while(l<=r)
    {
        int mid=(l+r)>>1;
        if(Check(mid)) ans=mid,r=mid-1;
        else l=mid+1;
    }
    printf("%d\n",ans);
    return 0;
}

加了当前弧优化啊。。。为什么T了一个点

2023/12/27 21:05
加载中...