刚学最大流0.1919ms wa #38910求助大佬
查看原帖
刚学最大流0.1919ms wa #38910求助大佬
590571
_weishiqi66_楼主2023/12/19 16:55
#include<bits/stdc++.h>
#define int long long 
using namespace std;
const int N=1e4;
const int inf=2005020600;
int n,m,s,t,ans=0;
int head[N],cnt;
int v[N],incf[N],pre[N];
bool pd[203][203];
struct code{
	int to,next,w;
}e[N];
void add(int x,int y,int z){
	e[++cnt].to=y;
	e[cnt].next=head[x];
	e[cnt].w=z;
	head[x]=cnt;
	
	e[++cnt].to=x;
	e[cnt].next=head[y];
	e[cnt].w=0;
	head[y]=cnt;
}
bool bfs(){
	memset(v,0,sizeof(v));
	queue<int> q;
	q.push(s);
	v[s]=1;
	incf[s]=inf;
	while(!q.empty()){
		int x=q.front();q.pop();
		for(int i=head[x];i;i=e[i].next){
			if(e[i].w!=0){
				int y=e[i].to;
				if(v[y]) continue;
				incf[y]=min(incf[x],e[i].w);
				pre[y]=i;
				q.push(y),v[y]=1;
				if(y==t){
					return 1;
				} 
			}
		}
	}
	return 0;
}
void update(){
	int x=t;
	int jsq=0;
//	if(x==s) cout<<'y'<<endl;
//	else cout<<x<<" "<<s<<endl;
	while(x!=s){
		++jsq;
		if(jsq>=50) break;
		int i=pre[x];
	//	cout<<i<<" "<<(i^1)<<endl;
		e[i].w-=incf[t];
		int tmp=i-1;
		if(i&1) tmp=i+1;
		e[tmp].w+=incf[t];
		x=e[tmp].to;
///		cout<<x<<endl;
	}
	ans+=incf[t];
}
signed main(){
	//freopen("P3376_7.in","r",stdin);
	cin>>n>>m>>s>>t;
	for(int i=1;i<=m;i++){
		int x,y,z;
		cin>>x>>y>>z;
		if(pd[x][y]==0){
			add(x,y,z);
			pd[x][y]=cnt;
		}
		else e[pd[x][y]-1].w+=z;
		
		
	} 
	while(bfs()){
	update();	
	} 
	cout<<ans;
	return 0;        
}
2023/12/19 16:55
加载中...