求助板题
查看原帖
求助板题
100325
peterwuyihong楼主2022/3/15 12:45

这份代码 Re on #10

#include<bits/stdc++.h>
using namespace std;
/* --------------- fast io --------------- */ // begin
namespace Fread{
const int SIZE= 1 << 16;
	char buf[SIZE],*S,*T;
	inline char getchar(){if(S==T){T=(S=buf)+
	fread(buf,1,SIZE,stdin);if(S==T)return'\n';}return *S++;}
} // namespace Fread
namespace Fwrite {
const int SIZE= 1 << 16;
	char buf[SIZE],*S=buf,*T=buf+SIZE;
	inline void flush(){fwrite(buf,1,S-buf,stdout);S=buf;}
	inline void putchar(char c){*S++=c;if(S==T)flush();}
	struct NTR{~NTR(){flush();}}ztr;
} // namespace Fwrite
#ifdef ONLINE_JUDGE
	#define getchar Fread::getchar
	#define putchar Fwrite::putchar
#endif
namespace Fastio{
struct Reader{
	template<typename T>Reader&operator>>(T&x){
		char c=getchar();short f=1;
		while(c<'0'||c>'9'){if(c=='-')f*=-1;c=getchar();}
		x=0;while(c>='0'&&c<='9'){
			x=(x<<1)+(x<<3)+(c^48);
			c=getchar();
		}x*=f;return *this;
	}
	Reader&operator>>(double&x){
		char c=getchar();short f=1,s=0;x=0;double t=0;
		while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}
		while(c>='0'&&c<='9'&&c!='.')x=x*10+(c^48),c=getchar();
		if(c=='.')c=getchar();else return x*=f,*this;
		while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();
		while(s--)t/=10.0;x=(x+t)*f;return*this;
	}
	Reader&operator>>(long double&x){
		char c=getchar();short f=1,s=0;x=0;long double t=0;
		while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}
		while(c>='0'&&c<='9'&&c!='.')x=x*10+(c^48),c=getchar();
		if(c=='.')c=getchar();else return x*=f,*this;
		while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();
		while(s--)t/=10.0;x=(x+t)*f;return*this;
	}
	Reader&operator>>(__float128&x){
		char c=getchar();short f=1,s=0;x=0;__float128 t=0;
		while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}
		while(c>='0'&&c<='9'&&c!='.')x=x*10+(c^48),c=getchar();
		if(c=='.')c=getchar();else return x*=f,*this;
		while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();
		while(s--)t/=10.0;x=(x+t)*f;return*this;
	}
	Reader&operator>>(char&c){
		c=getchar();while(c=='\n'||c==' '||c=='\r')c=getchar();
		return *this;
	}
	Reader&operator>>(char*str){
		int len=0;char c=getchar();
		while(c=='\n'||c==' '||c=='\r')c=getchar();
		while(c!='\n'&&c!=' '&&c!='\r')str[len++]=c,c=getchar();
		str[len]='\0';return*this;
	}
	Reader&operator>>(string&str){
		char c=getchar();str.clear();
		while(c=='\n'||c==' '||c=='\r')c=getchar();
		while(c!='\n'&&c!=' '&&c!='\r')str.push_back(c),c=getchar();
		return*this;
	}
	template<class _Tp>Reader&operator>>(vector<_Tp>&vec){for(unsigned i=0;i<vec.size();i++)cin>>vec[i];return*this;}
	template<class _Tp,class _tp>Reader&operator>>(pair<_Tp,_tp>&a){cin>>a.first>>a.second;return*this;}
	Reader(){}
}cin;
const char endl='\n';
struct Writer{
const int Setprecision=6;
typedef int mxdouble;
	template<typename T>Writer&operator<<(T x){
		if(x==0)return putchar('0'),*this;
		if(x<0)putchar('-'),x=-x;
		static int sta[45];int top=0;
		while(x)sta[++top]=x%10,x/=10;
		while(top)putchar(sta[top]+'0'),--top;
		return*this;
	}
	Writer&operator<<(double x){
		if(x<0)putchar('-'),x=-x;
		mxdouble _=x;x-=(double)_;static int sta[45];int top=0;
		while(_)sta[++top]=_%10,_/=10;if(!top)putchar('0');
		while(top)putchar(sta[top]+'0'),--top;putchar('.');
		for(int i=0;i<Setprecision;i++)x*=10;
		_=x;while(_)sta[++top]=_%10,_/=10;
		for(int i=0;i<Setprecision-top;i++)putchar('0');
		while(top)putchar(sta[top]+'0'),--top;
		return*this;
	}
	Writer&operator<<(long double x){
		if(x<0)putchar('-'),x=-x;
		mxdouble _=x;x-=(long double)_;static int sta[45];int top=0;
		while(_)sta[++top]=_%10,_/=10;if(!top)putchar('0');
		while(top)putchar(sta[top]+'0'),--top;putchar('.');
		for(int i=0;i<Setprecision;i++)x*=10;
		_=x;while(_)sta[++top]=_%10,_/=10;
		for(int i=0;i<Setprecision-top;i++)putchar('0');
		while(top)putchar(sta[top]+'0'),--top;
		return*this;
	}
	Writer&operator<<(__float128 x){
		if(x<0)putchar('-'),x=-x;
		mxdouble _=x;x-=(__float128)_;static int sta[45];int top=0;
		while(_)sta[++top]=_%10,_/=10;if(!top)putchar('0');
		while(top)putchar(sta[top]+'0'),--top;putchar('.');
		for(int i=0;i<Setprecision;i++)x*=10;
		_=x;while(_)sta[++top]=_%10,_/=10;
		for(int i=0;i<Setprecision-top;i++)putchar('0');
		while(top)putchar(sta[top]+'0'),--top;
		return*this;
	}
	Writer&operator<<(char c){putchar(c);return*this;}
	Writer& operator<<(char*str){
		int cur=0;while(str[cur])putchar(str[cur++]);
		return *this;
	}
	Writer&operator<<(const char*str){
		int cur=0;while(str[cur])putchar(str[cur++]);
		return*this;
	}
	Writer&operator<<(string str){
		int st=0,ed=str.size();
		while(st<ed)putchar(str[st++]);
		return*this;
	}
	template<class _Tp>Writer&operator<<(vector<_Tp>vec){for(unsigned i=0;i<vec.size()-1;i++)cout<<vec[i]<<" ";cout<<vec[vec.size()-1];return*this;}
	template<class _Tp,class _tp>Writer&operator<<(pair<_Tp,_tp>a){cout<<a.first<<" "<<a.second;return*this;}
	Writer(){}
}cout;
} // namespace Fastio
#define cin Fastio :: cin
#define cout Fastio :: cout
#define endl Fastio :: endl
/* --------------- fast io --------------- */ // end
#define rep(i,j,k) for(int i=j;i<=(k);i++)
#define per(i,j,k) for(int i=j;i>=(k);i--)
#define all(x) x.begin(),x.end()


using _Tp = long long;
vector<vector<pair<int,_Tp>>>g;
void add(int x,int y,_Tp z){g[x].push_back(make_pair(y,z));}

auto liu(vector<vector<pair<int,_Tp>>>g,int S,int T){
  const _Tp INF = numeric_limits<_Tp>::max();
  int n=g.size();
  vector<vector<pair<pair<int,int>,_Tp>>>adj(n);
  queue<int>q;
  vector<int>H;
  vector<_Tp>ex(n);
  vector<int>arc(n);
  vector<int>nxt(n),lst;
  vector<int>gN(2*n),gP(2*n);
  auto add=[&](int x,int y,_Tp z){
    adj[x].push_back({{y,int(adj[y].size())},z});
    adj[y].push_back({{x,int(adj[x].size())-1},0});
  };
  for(int i=0;i<(int)g.size();i++)
  for(auto j:g[i])
    add(i,j.first,j.second);
  auto pushlst=[&](int h,int v){
    nxt[v]=lst[h],lst[h]=v;
  };
  int hG,hi,work;
  auto upd=[&](int v,int nh){
    if(H[v]!=n){
      gN[gP[v]]=gN[v];
      gP[gN[v]]=gP[v];
    }
    H[v]=nh;
    if(nh==n)return;hG=max(hG,nh);
    if(ex[v]>0)hi=max(hi,nh),pushlst(nh,v);nh+=n;
    gN[v]=gN[nh];gP[v]=nh;
    gN[nh]=v;gP[gN[v]]=v;
  };
  auto GR=[&](){
    work=0;
    H.assign(n,n);
    lst.assign(n,-1);
    iota(gN.begin(),gN.end(),0);
    iota(gP.begin(),gP.end(),0);
    H[T]=0,q.push(T);
    while(!q.empty()){
      int v=q.front();q.pop();
      for(auto&e:adj[v]){
        if(H[e.first.first]==n&&adj[e.first.first][e.first.second].second>0)
          q.push(e.first.first),upd(e.first.first,H[v]+1);
      }hi=hG=H[v];
    }
  };
  auto push=[&](int v,auto&e){
    _Tp df=min(ex[v],e.second);
    if(df>0){
      if(ex[e.first.first]==0)pushlst(H[e.first.first],e.first.first);
      e.second-=df,adj[e.first.first][e.first.second].second+=df;
      ex[v]-=df,ex[e.first.first]+=df;
    }
  };
  auto discharge=[&](int v){
    int nh=n;
    for(int i=arc[v];i<(int)adj[v].size();i++){
      auto&e=adj[v][i];
      if(e.second>0){
        if(H[v]==H[e.first.first]+1){
          push(v,e);if(ex[v]<=0){arc[v]=i;return;}
        }else nh=min(nh,H[e.first.first]+1);
      }
    }
    for(int i=0;i<(int)arc[v];i++){
      auto&e=adj[v][i];
      if(e.second>0){
        if(H[v]==H[e.first.first]+1){
          push(v,e);
          if(ex[v]<=0){
            arc[v]=i;
            return;
          }
        }else nh=min(nh,H[e.first.first]+1);
      }
    }
    work++;
    if(gN[gN[H[v]+n]]!=H[v]+n)upd(v,nh);
    else{
      int oldH=H[v];
      for(int h=oldH;h<=hG;h++){
        for(int i=gN[h+n];i<n;i=gN[i])H[i]=n;
        gN[h+n]=gP[h+n]=h+n;
      }
      hG=oldH-1;
    }
  };
  for(auto&vec:adj){
    sort(vec.begin(),vec.end());
    for(int i=0;i<(int)vec.size();i++){
      auto&e=vec[i];
      adj[e.first.first][e.first.second].first.second=i;
    }
  }
  ex[S]=INF,ex[T]=-INF;
  GR();
  for(auto&e:adj[S])push(S,e);
  while(hi>=0){
    while(~lst[hi]){
      int v=lst[hi];
      lst[hi]=nxt[v];
      if(H[v]==hi){
        discharge(v);
        if(work>4*n)GR();
      }
    }
    hi--;
  }
  return ex[T]+INF;
}
signed main(){
  int n,m;
  cin>>n>>m;
  g.resize(m);
  while(n--){
    int x,y,z;
    cin>>x>>y>>z;
    add(x-1,y-1,z);
  }
  cout<<liu(g,0,m-1)<<endl;
}

然后我把预留推进的板题 std 跑了一下又 Wa on #10,这是怎么一回事呢?

2022/3/15 12:45
加载中...