EK算法40pts,6个WA求助(样例1没有过)
查看原帖
EK算法40pts,6个WA求助(样例1没有过)
382274
暗影之梦楼主2022/2/10 21:08
#include<iostream>
#include<cstring>
#include<queue>
#define int long long
using namespace std;
int n,m,sta,en,ee;
const int maxn=500001,maxm=500001;
int head[maxn],cnt;
int q[maxn],d[maxn],pre[maxn];
bool st[maxn];
struct node
{
	int nxt,to,w;
}e[maxm];
inline void addedge(int a,int b,int c)
{
	e[cnt].to=b;
	e[cnt].w=c;
	e[cnt].nxt=head[a];
	head[a]=cnt++;
	e[cnt].to=a;
	e[cnt].w=0;
	e[cnt].nxt=head[b];
	head[b]=cnt++;
}
inline bool bfs()
{
	queue<int> q;
	memset(st,0,sizeof(st));
	q.push(sta);
	st[sta]=1,d[sta]=1e16;
	while(!q.empty())
	{
		int tp=q.front();
		q.pop();
		for(int i=head[tp];i;i=e[i].nxt)
		{
			int v=e[i].to;
			if(!st[v]&&e[i].w)
			{
				st[v]=1;
				d[v]=min(d[tp],e[i].w);
				pre[v]=i;
				if(v==en) return 1;
				q.push(v);
			}
		}
	}
	return 0;
}
int EK()
{
	int r=0;
	while(bfs())
	{
		r+=d[en];
		for(int i=en;i!=sta;i=e[pre[i]^1].to)
		{
			e[pre[i]].w-=d[en],e[pre[i]^1].w+=d[en]; 
		} 
	}
	return r;
}
signed main()
{
	cin>>n>>m>>ee;
	sta=1;
	en=n+m+2;
	memset(head,-1,sizeof(head));
	for(int i=1;i<=n;i++)
	{
		addedge(1,i+1,1);
	}
	for(int i=1;i<=ee;i++)
	{
		int cina,cinb;
		cin>>cina>>cinb;
		addedge(cina+1,cinb+n+1,1);
	}
	for(int i=1;i<=m;i++)
	{
		addedge(i+n+1,n+m+2,1);
	}
	cout<<EK();
	return 0;
}
2022/2/10 21:08
加载中...