• [ZJOI2009] 假期的宿舍


    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    int t,S,T,n,a[55],b[55],cur[105],tot,h[105],dep[105],q[110];
    
    struct edge{
    	int to,nxt,z;
    }e[10000];
    void add(int x,int y,int z)
    {
    	e[++tot] = (edge){y,h[x],z},h[x] = tot;
    	e[++tot] = (edge){x,h[y],0},h[y] = tot;
    }
    int bfs()
    {
    	for (int i = 0; i <= T; i++) cur[i] = h[i];
    	memset(dep,0,sizeof dep);
    	int head = 0,tail = 0;
    	q[++tail] = S,dep[S] = 1;
    	while (head < tail)
    	{
    		int u = q[++head];
    		for (int i = h[u]; i; i = e[i].nxt)
    		{
    			int v = e[i].to;
    			if (dep[v] || e[i].z <= 0) continue;
    			dep[v] = dep[u] + 1,q[++tail] = v;
    		}
    	}
    	if (dep[T]) return 1;
    	return 0;
    }
    int dinic(int u,int mn,int fa)
    {
    	if (u == T || mn == 0) return mn;
    	int sum = 0,flow = 0;
    	for (int i = cur[u]; i; i = e[i].nxt)
    	{
    		cur[u] = i; int v = e[i].to;
    		if (v == fa || dep[v] != dep[u] + 1 || e[i].z <= 0) continue;
    		sum = dinic(v,min(mn,e[i].z),u);
    		if (sum <= 0) continue;
    		flow += sum,mn -= sum,e[i].z -= sum,e[i ^ 1].z += sum;
    		if (mn <= 0) break; 
    	}
    	return flow;
    }
    int main()
    {
    	scanf("%d",&t);
    	for (; t; t--)
    	{
    		memset(h,0,sizeof h),tot = 1;
    		scanf("%d",&n),S = n * 2 + 1,T = n * 2 + 2;
    		for (int i = 1; i <= n; i++) scanf("%d",&a[i]);
    		for (int i = 1; i <= n; i++) scanf("%d",&b[i]);
    		for (int i = 1; i <= n; i++)
    			for (int j = 1,q1; j <= n; j++)
    			{
    				scanf("%d",&q1);
    				if ((!b[i] || !a[i]) && a[j] && q1) add(i,j + n,1);
    			}
    		for (int i = 1; i <= n; i++) 
    			if (a[i] && !b[i]) add(i,i + n,1);
    		int res = 0,ans = 0;
    		for (int i = 1; i <= n; i++) 
    		{
    			if (!b[i] || !a[i]) add(S,i,1),res++;
    			if (a[i]) add(i + n,T,1);
    		}
    		while (bfs()) ans += dinic(S,2147483647,S);
    		if (ans == res) printf("^_^\n");
    		else printf("T_T\n");
    	}
    }
    
  • 相关阅读:
    uniapp
    uniapp
    uniapp
    偶然发现的一天
    了不起的麦瑟尔夫人第三季
    女性长期没有"恩爱",会出现这4个后果?提醒:频率最好能在这个数
    IntelliJ IDEA live template 方法配置
    使用MHA实现MySQL主从复制高可用
    Linux 常用命令介绍
    开源堡垒机
  • 原文地址:https://www.cnblogs.com/nibabadeboke/p/15839847.html
Copyright © 2020-2023  润新知