RE on 7求调
查看原帖
RE on 7求调
989792
lrx___楼主2024/7/23 09:28
// #define ___coding

#ifdef ___coding
#define ___coding1 true
#else
#define ___coding1 false
#endif
#include<cstdlib>
// #include<cassert>
// #include<cctype>
// #include<climits>
// #include<cmath>
#include<cstdio>
// #include<cstring>
// #include<ctime>
#include<algorithm>
// using std::lower_bound;
// using std::max;
using std::min;
// using std::next_permutation;
// using std::reverse;
// using std::sort;
// using std::swap;
// using std::unique;
// using std::upper_bound;
// #include<bitset>
// using std::bitset;
// #include<deque>
// using std::deque;
// #include<fstream>
// using std::ifstream;
// using std::ofstream;
// #include<iomanip>
// using std::fixed;
// using std::setprecision;
// #include<iostream>
// using std::cin;
// using std::cout;
// using std::ios;
// #include<list>
// using std::list;
// #include<map>
// using std::map;
#include<queue>
using std::priority_queue;
// using std::queue;
// #include<stack>
// using std::stack;
// #include<set>
// using std::multiset;
// using std::set;
// #include<sstream>
// using std::stringstream;
// #include<string>
// using std::getline;
// using std::string;
// using std::to_string;
#include<utility>
using std::make_pair;
using std::pair;
// #include<unordered_map>
// using std::unordered_map;
// #include<unordered_set>
// using std::unordered_set;
// #include<vector>
// using std::vector;
#define UP(i,a,b) for(i=a;i<=(b);++i)
#define DN(i,a,b) for(i=a;i>=(b);--i)
#define up(i,a,b) for(i=a;i<(b);++i)
#define dn(i,a,b) for(i=a;i>(b);--i)
#define pr make_pair
typedef unsigned char unc;
typedef unsigned short uns;
typedef unsigned uni;
typedef unsigned long unl;
typedef long long ll;
typedef unsigned long long ull;
const int BUF_SIZE(1<<20);
bool ___f;
char ___c,___a[45],*___p,___ibuf[BUF_SIZE+1],*___ib(___ibuf),*___ie(___ibuf),___obuf[BUF_SIZE+1],*___ob(___obuf),*___oe(___obuf+BUF_SIZE);
char& gtch(){if(___ib==___ie)___ie=(___ib=___ibuf)+fread(___ibuf,1,BUF_SIZE,stdin);if(___ib==___ie){return *___ibuf=-1;}return *___ib++;}
void ___flush(){fwrite(___obuf,1,___ob-___obuf,stdout);___ob=___obuf;}
void ptch(char c){if(___ob==___oe)___flush();*___ob=c;++___ob;}
typedef __int128 pll;
typedef unsigned __int128 upll;
void rd(pll &r){r=0;___f=false;for(___c=gtch();___c<'0'||___c>'9';___c=gtch()){___f=(___c=='-');}for(;___c>='0'&&___c<='9';___c=gtch()){r=(r<<3)+(r<<1)+(___c^'0');}if(___f){r=~(r-1);}}
void rd(upll &r){r=0;for(___c=gtch();___c<'0'||___c>'9';___c=gtch());for(;___c>='0'&&___c<='9';___c=gtch()){r=(r<<3)+(r<<1)+(___c^'0');}}
void wt(pll r){if(!r){ptch('0');return;}if(r<0){ptch('-');r=~(r-1);}___p=___a;while(r!=0){++___p;*___p=char(r%10)^'0';r/=10;}while(___p!=___a){ptch(*___p);--___p;}}
void wt(upll r){if(!r){ptch('0');return;}___p=___a;while(r!=0){++___p;*___p=char(r%10)^'0';r/=10;}while(___p!=___a){ptch(*___p);--___p;}}
void rd(bool &r){r=false;for(___c=gtch();___c<'0'||___c>'9';___c=gtch());for(;___c>='0'&&___c<='9';___c=gtch()){r|=bool(___c^'0');}}
void rd(char &c){for(c=gtch();c==' '||c=='\r'||c=='\n';c=gtch());}
void rd(short &r){r=0;___f=false;for(___c=gtch();___c<'0'||___c>'9';___c=gtch()){___f=(___c=='-');}for(;___c>='0'&&___c<='9';___c=gtch()){r=(r<<3)+(r<<1)+(short)(___c^'0');}if(___f){r=~(r-1);}}
void rd(int &r){r=0;___f=false;for(___c=gtch();___c<'0'||___c>'9';___c=gtch()){___f=(___c=='-');}for(;___c>='0'&&___c<='9';___c=gtch()){r=(r<<3)+(r<<1)+(___c^'0');}if(___f){r=~(r-1);}}
void rd(long &r){r=0;___f=false;for(___c=gtch();___c<'0'||___c>'9';___c=gtch()){___f=(___c=='-');}for(;___c>='0'&&___c<='9';___c=gtch()){r=(r<<3)+(r<<1)+(___c^'0');}if(___f){r=~(r-1);}}
void rd(ll &r){r=0;___f=false;for(___c=gtch();___c<'0'||___c>'9';___c=gtch()){___f=(___c=='-');}for(;___c>='0'&&___c<='9';___c=gtch()){r=(r<<3)+(r<<1)+(___c^'0');}if(___f){r=~(r-1);}}
void rd(unc &c){for(c=gtch();c==' '||c=='\r'||c=='\n';c=gtch());}
void rd(uns &r){r=0;for(___c=gtch();___c<'0'||___c>'9';___c=gtch());for(;___c>='0'&&___c<='9';___c=gtch()){r=(r<<3)+(r<<1)+(uns)(___c^'0');}}
void rd(uni &r){r=0;for(___c=gtch();___c<'0'||___c>'9';___c=gtch());for(;___c>='0'&&___c<='9';___c=gtch()){r=(r<<3)+(r<<1)+(___c^'0');}}
void rd(unl &r){r=0;for(___c=gtch();___c<'0'||___c>'9';___c=gtch());for(;___c>='0'&&___c<='9';___c=gtch()){r=(r<<3)+(r<<1)+(___c^'0');}}
void rd(ull &r){r=0;for(___c=gtch();___c<'0'||___c>'9';___c=gtch());for(;___c>='0'&&___c<='9';___c=gtch()){r=(r<<3)+(r<<1)+(___c^'0');}}
void rd(char *s){for(*s=gtch();*s==' '||*s=='\r'||*s=='\n';*s=gtch());for(;~*s&&*s!=' '&&*s!='\r'&&*s!='\n';++s,*s=gtch());*s=0;}
void rd(char *s,char n){for(*s=gtch();*s==' '||*s=='\r'||*s=='\n';*s=gtch());for(;~*s&&*s!=n;++s,*s=gtch());*s=0;}
void wt(bool a){ptch('0'+a);}
void wt(char c){ptch(c);}
void wt(short r){if(!r){ptch('0');return;}if(r<0){ptch('-');r=~(r-1);}___p=___a;while(r!=0){++___p;*___p=char(r%10)^'0';r/=10;}while(___p!=___a){ptch(*___p);--___p;}}
void wt(int r){if(!r){ptch('0');return;}if(r<0){ptch('-');r=~(r-1);}___p=___a;while(r!=0){++___p;*___p=char(r%10)^'0';r/=10;}while(___p!=___a){ptch(*___p);--___p;}}
void wt(long r){if(!r){ptch('0');return;}if(r<0){ptch('-');r=~(r-1);}___p=___a;while(r!=0){++___p;*___p=char(r%10)^'0';r/=10;}while(___p!=___a){ptch(*___p);--___p;}}
void wt(ll r){if(!r){ptch('0');return;}if(r<0){ptch('-');r=~(r-1);}___p=___a;while(r!=0){++___p;*___p=char(r%10)^'0';r/=10;}while(___p!=___a){ptch(*___p);--___p;}}
void wt(unc c){ptch(c);}
void wt(uns r){if(!r){ptch('0');return;}___p=___a;while(r!=0){++___p;*___p=char(r%10)^'0';r/=10;}while(___p!=___a){ptch(*___p);--___p;}}
void wt(uni r){if(!r){ptch('0');return;}___p=___a;while(r!=0){++___p;*___p=char(r%10)^'0';r/=10;}while(___p!=___a){ptch(*___p);--___p;}}
void wt(unl r){if(!r){ptch('0');return;}___p=___a;while(r!=0){++___p;*___p=char(r%10)^'0';r/=10;}while(___p!=___a){ptch(*___p);--___p;}}
void wt(ull r){if(!r){ptch('0');return;}___p=___a;while(r!=0){++___p;*___p=char(r%10)^'0';r/=10;}while(___p!=___a){ptch(*___p);--___p;}}
void wt(char *s){for(;*s;++s){ptch(*s);}}
void wt(const char *s){for(;*s;++s){ptch(*s);}}
template<typename T,typename ...T1>void rd(T &a,T1 &...b){rd(a);rd(b...);}
template<typename T,typename ...T1>void wt(T a,T1 ...b){wt(a);wt(b...);}
template<typename T>void RD(T* a,int n=1){while(n--){rd(*a);++a;}}
ll _llabs(ll a){return a<0?-a:a;}


/*

*/
const int N(1205),M(1.2e5+5),INF(1e9);
bool vs[N];
int hd[N],ed[M<<1][3],ee,dep[N],flow[N],gap[M],n,m,s,t;
priority_queue<pair<int,int>> p;
struct queue{
	int a[N],*h,*t;
	void push(int k){*t=k;++t;}
	void pop(){++h;}
	int front(){return *h;}
	ull size(){return t-h;}
	void clear(){h=t=a;}
}q;

void add(int u,int v,int fl){
	ed[++ee][0]=hd[u];
	ed[ee][1]=v;
	ed[ee][2]=fl;
	hd[u]=ee;
}
void add_edge(int u,int v,int fl){
	add(u,v,fl);add(v,u,0);
}
void init(){
	ee=1;
	q.clear();
}
bool bfs(){
	int i,u,v;
	UP(i,1,n)dep[i]=INF;
	dep[t]=1;
	q.push(t);
	while(q.size()){
		u=q.front();q.pop();
		++gap[dep[u]];
		for(i=hd[u];i;i=ed[i][0]){
			v=ed[i][1];
			if(!ed[i][2]&&dep[v]==INF){
				dep[v]=dep[u]+1;
				q.push(v);
			}
		}
	}
	return dep[s]<INF;
}
void hlpp(){
	int i,u,v,w;
	for(i=hd[s];i;i=ed[i][0]){
		// printf("i=%d\n",i);
		v=ed[i][1];w=ed[i][2];
		if(w>0){
			ed[i][2]-=w;ed[i^1][2]+=w;
			flow[s]-=w;flow[v]+=w;
			vs[v]=true;p.push(pr(dep[v],v));
		}
	}
	while(p.size()){
		u=p.top().second;p.pop();vs[u]=false;
		for(i=hd[u];i&&flow[u];i=ed[i][0]){
			v=ed[i][1];
			if(ed[i][2]>0&&dep[v]==dep[u]-1){
				w=min(ed[i][2],flow[u]);
				ed[i][2]-=w;ed[i^1][2]+=w;
				flow[u]-=w;flow[v]+=w;
				if(v!=s&&v!=t&&!vs[v]){
					vs[v]=true;p.push(pr(dep[v],v));
				}
			}
		}
		if(flow[u]){
			if(!--gap[dep[u]]){
				UP(v,1,n){
					if(v!=s&&v!=t&&dep[v]>dep[u]){
						dep[v]=n+1;
					}
				}
			}
			dep[u]=INF;
			for(i=hd[u];i;i=ed[i][0]){
				v=ed[i][1];
				if(ed[i][2]>0){
					dep[u]=min(dep[u],dep[v]+1);
				}
			}
			++gap[dep[u]];
			vs[u]=true;p.push(pr(dep[u],u));
		}
	}
	wt(flow[t],'\n');
}
void main1(){
	int i,u,v,w;
	init();
	rd(n,m,s,t);
	up(i,0,m){
		rd(u,v,w);
		add_edge(u,v,w);
	}
	if(!bfs()){
		wt("0\n");
		return;
	}
	dep[s]=n;
	hlpp();
}
int main(){if(___coding1)freopen("in.txt","r",stdin);main1();___flush();if(___coding1){freopen("CON","r",stdin);system("pause");}return 0;}

2024/7/23 09:28
加载中...