[BZOJ2229][ZJOI2011]最小割

bzoj
luogu

sol

最小割树请转一道很相似完全一模一样的题
所以跑出所有点对之间的最小割然后暴力统计答案即可。

code

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
int gi()
{
	int x=0,w=1;char ch=getchar();
	while ((ch<'0'||ch>'9')&&ch!='0') ch=getchar();
	if (ch=='-') w=0,ch=getchar();
	while (ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
	return w?x:-x;
}
const int N = 200;
const int inf = 1e9;
struct edge{int to,nxt,w;}a[N<<6];
int T,n,m,qq,head[N],cnt,dep[N],cur[N],q[N],tmp[N],cut[N][N];
queue<int>Q;
void link(int u,int v,int w)
{
	a[++cnt]=(edge){v,head[u],w};
	head[u]=cnt;
	a[++cnt]=(edge){u,head[v],w};
	head[v]=cnt;
}
bool bfs(int s,int t)
{
	memset(dep,0,sizeof(dep));
	dep[s]=1;Q.push(s);
	while (!Q.empty())
	{
		int u=Q.front();Q.pop();
		for (int e=head[u];e;e=a[e].nxt)
			if (a[e].w&&!dep[a[e].to])
				dep[a[e].to]=dep[u]+1,Q.push(a[e].to);
	}
	return dep[t];
}
int dfs(int u,int f,int t)
{
	if (u==t) return f;
	for (int &e=cur[u];e;e=a[e].nxt)
		if (a[e].w&&dep[a[e].to]==dep[u]+1)
		{
			int tmp=dfs(a[e].to,min(a[e].w,f),t);
			if (tmp) {a[e].w-=tmp;a[e^1].w+=tmp;return tmp;}
		}
	return 0;
}
int Dinic(int s,int t)
{
	int res=0;
	while (bfs(s,t))
	{
		for (int i=1;i<=n;++i) cur[i]=head[i];
		while (int tmp=dfs(s,inf,t)) res+=tmp;
	}
	return res;
}
void solve(int L,int R)
{
	if (L==R) return;
	for (int i=3;i<=cnt;i+=2)
		a[i].w=a[i^1].w=(a[i].w+a[i^1].w)>>1;
	int res=Dinic(q[L],q[R]);
	for (int i=1;i<=n;++i)
		if (dep[i])
			for (int j=1;j<=n;++j)
				if (!dep[j])
					cut[i][j]=cut[j][i]=min(cut[i][j],res);
	int l=L-1,r=R+1;
	for (int i=L;i<=R;++i)
		if (dep[q[i]]) tmp[++l]=q[i];
		else tmp[--r]=q[i];
	for (int i=L;i<=R;++i) q[i]=tmp[i];
	solve(L,l);solve(r,R);
}
int main()
{
	T=gi();
	while (T--)
	{
		n=gi();m=gi();
		memset(head,0,sizeof(head));cnt=1;
		for (int i=1;i<=m;++i)
		{
			int u=gi(),v=gi(),w=gi();
			link(u,v,w);
		}
		for (int i=1;i<=n;++i)
			for (int j=1;j<=n;++j)
				cut[i][j]=inf;
		for (int i=1;i<=n;++i) q[i]=i;
		solve(1,n);qq=gi();
		while (qq--)
		{
			int val=gi(),ans=0;
			for (int i=1;i<n;++i)
				for (int j=i+1;j<=n;++j)
					ans+=(cut[i][j]<=val);
			printf("%d\n",ans);
		}
		puts("");
	}
	return 0;
}
posted @ 2018-03-20 23:03  租酥雨  阅读(182)  评论(0编辑  收藏  举报