82pts蒟蒻求助
查看原帖
82pts蒟蒻求助
490694
Compound_Interest楼主2022/7/16 16:15

EK蒟蒻82ptsT了两个点

代码如下


#include<cstdio>
#include<queue>
#define int long long
using namespace std;
const int maxn=5e3+10;
queue<int>q;
struct edge{
	int nxt,to,w;
}e[maxn<<1];
struct node{
	int v,edg;
}pre[maxn];
int ans,n,m,s,t,cnt=2,head[maxn],dis[maxn];
bool vis[maxn];
void add(int u,int v,int w){
	e[cnt].to=v,e[cnt].w=w,e[cnt].nxt=head[u];
	head[u]=cnt++;
}
bool bfs(){
	for(int i=1;i<=n;i++) vis[i]=0;
	dis[s]=0x3f3f3f3f;
	while(!q.empty()) q.pop();
	q.push(s);
	while(!q.empty()){
		int u=q.front();
		q.pop();
		vis[u]=1;
		for(int i=head[u];i;i=e[i].nxt){
			int v=e[i].to;		
			if(vis[v]||e[i].w==0) continue;
			q.push(v);
			dis[v]=min(dis[u],e[i].w);
			pre[v].v=u,pre[v].edg=i;
			if(v==t) return 1;
		} 
	}
	return 0;
}
signed main(){
	scanf("%lld%lld%lld%lld",&n,&m,&s,&t);
	for(int i=1;i<=m;i++){
		int u,v,w;
		scanf("%lld%lld%lld",&u,&v,&w);
		add(u,v,w),add(v,u,0);
	}
	while(bfs()){
		for(int i=t;i!=s;i=pre[i].v){
			e[pre[i].edg].w-=dis[t];
			e[pre[i].edg^1].w+=dis[t];
		}
		ans+=dis[t];
	}
	printf("%lld",ans);
	return 0;
}

2022/7/16 16:15
加载中...