为什么RE7啊,各种调数组大小都过不去
查看原帖
为什么RE7啊,各种调数组大小都过不去
600654
FishAndCat楼主2022/8/25 18:26
#include <bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
int n,m,s,t;
struct edge
{
 int to,cap;
};
vector <edge>E;
vector <int>G[310];
stack <int>bucket[310];
int h[310];
int ex[310];
int gap[310];
int mx;
void init0(int n)
{
   for(int i=1;i<=n;i++)
   {
   G[i].clear();
   h[i]=0;
   }
   E.clear();
}
void init1(int n)
{
  
   for(auto v:G[s])
   {
    if(E[v].to!=s&&E[v].to!=t&&!ex[E[v].to])bucket[h[E[v].to]].push(E[v].to),mx=max(mx,h[E[v].to]);
    ex[E[v].to]+=E[v].cap;
    ex[s]-=E[v].cap;
    E[v^1].cap+=E[v].cap;
    E[v].cap=0;
    
   }
 
  
}
void addEdge(int u,int v,int cap)
{
 E.push_back({v,cap});
 E.push_back({u,0});
 m=E.size();
 G[u].push_back(m-2);
 G[v].push_back(m-1);
}
bool BFSINIT(int t)
{
 memset(h,0x3f,sizeof(h));
 h[t]=0;
 queue<int>q;
 q.push(t);
 while(!q.empty())
 {
   int u=q.front();
   q.pop();
   for(auto v:G[u])
   {
    if(E[v^1].cap&&h[E[v].to]>h[u]+1)h[E[v].to]=h[u]+1,q.push(E[v].to);
   }  
   
 }
 return h[s]!=INF;
}

int push(int u)  //推单个边/推一个点的所有边
{
   for(auto v:G[u])
   {
    if(!E[v].cap||h[u]!=h[E[v].to]+1)continue; 
   int flow=min(E[v].cap,ex[u]);
  
   if(E[v].to!=s&&E[v].to!=t&&!ex[E[v].to])bucket[h[E[v].to]].push(E[v].to),mx=max(mx,h[E[v].to]);
   ex[u]-=flow,ex[E[v].to]+=flow,E[v].cap-=flow,E[v^1].cap+=flow;
   if(!ex[u]) return 0;
   }
   return 1;
}
void relabel(int u)
{
    h[u]=INF;
    for(auto v:G[u])
     if(E[v].cap)h[u]=min(h[u],h[E[v].to]+1); 
   if(h[u]<n)    
   {
    bucket[h[u]].push(u);
    mx=max(mx,h[u]);
    ++gap[h[u]];
   }
}
int select()
{
    while(bucket[mx].size()==0&&mx>-1)mx--;
    return mx==-1?0:bucket[mx].top();
}
int HLPP()
{

   if(!BFSINIT(t)) return 0; 
 
   memset(gap,0,sizeof(gap));
    h[s]=n;
   for(int i=1;i<=n;i++)
    if(i!=INF)gap[h[i]]++;    
   init1(n);
   int u;
   while((u=select()))
   {
    bucket[mx].pop();
    if(push(u))
    {
        if(!--gap[h[u]])
            for(int i=1;i<=n;i++)
                if(i==s||i==t)continue;
                else if(h[i]>h[u]&&h[i]<n+1)
                    h[i]=n+1;
        relabel(u);
    }
   }  
  
    return ex[t];
}
int main()
{
 ios::sync_with_stdio(false);
 cin.tie(0);
 int mm;
 cin>>n>>mm;
 s=n+1;
 t=n+2;
 for(int i=1;i<=n;i++)
 {
   int cap;
   cin>>cap;
   if(cap)
   addEdge(i,t,1);
   else
   addEdge(s,i,1);
 }
 n+=2;
// init0(n);

 for(int i=1;i<=mm;i++)
 {
    int f1,f2;
    cin>>f1>>f2;
    addEdge(f1,f2,1);
    addEdge(f2,f1,1);
 }
 cout<<HLPP();
}
2022/8/25 18:26
加载中...