• bzoj 5120: [2017国家集训队测试]无限之环【最小费用最大流】


    玄妙的建图……
    这种平衡度数的题按套路是先黑白染色然后分别连ST点,相邻格子连黑向白连费用1流量0的边,然后考虑费用怎么表示
    把一个点拆成五个,上下左右中,中间点黑白染色连ST,
    对于连S的点,中点连它不转时候的四边点(有哪个连哪个)流量1费用0的边。
    然后考虑转的费用,因为这里已经拆点了,所以在里面互相连就能表示费用
    对于一个插头的,插头原来那个位置向另外三个连费用1(和原位置相邻)/2(正对着原位置)流量1的边,这样从中间点出发可以经过原点然后走一条有费用的边到其他点出去
    对于有两个相邻插头(能转)的,两个有插头的位置分别向它们正对的位置连流量1费用1的边
    对于有三个插头的,三个有插头的分别连向那个没插头的,流量1,费用为正对2相邻1
    连向T的同理,就是上述所有边反过来即可

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<queue>
    #include<algorithm>
    #define u(x) x+lz*n*m
    #define r(x) x+((lz+1)&3)*n*m
    #define d(x) x+((lz+2)&3)*n*m
    #define l(x) x+((lz+3)&3)*n*m
    #define m(x) x+((n*m)<<2)
    using namespace std;
    const int N=100005,d[]={0,0,1,0,2,0,1,1,3,3,0,0,2,3,2,0};
    int n,m,h[N],cnt=1,s,t,tot,lz,dis[N],fr[N],b[20],sm,val,sum,smm;
    bool v[N];
    struct qwe
    {
    	int ne,no,to,va,c;
    }e[N*50];
    int read()
    {
    	int r=0,f=1;
    	char p=getchar();
    	while(p>'9'||p<'0')
    	{
    		if(p=='-')
    			f=-1;
    		p=getchar();
    	}
    	while(p>='0'&&p<='9')
    	{
    		r=r*10+p-48;
    		p=getchar();
    	}
    	return r*f;
    }
    void add(int u,int v,int w,int c)
    {
    	cnt++;
    	e[cnt].ne=h[u];
    	e[cnt].no=u;
    	e[cnt].to=v;
    	e[cnt].va=w;
    	e[cnt].c=c;
    	h[u]=cnt;
    }
    void ins(int u,int v,int w,int c,int t)
    {
    	if(t)
    		swap(u,v);//cerr<<u<<" "<<v<<" "<<w<<" "<<c<<endl;
    	add(u,v,w,c);
    	add(v,u,0,-c);
    }
    bool spfa()
    {
    	for(int i=s;i<=t;i++)
    		dis[i]=1e9;
    	queue<int>q;
    	dis[s]=0;
    	v[s]=1;
    	q.push(s);
    	while(!q.empty())
    	{
    		int u=q.front();
    		q.pop();
    		v[u]=0;
    		for(int i=h[u];i;i=e[i].ne)
    			if(e[i].va>0&&dis[e[i].to]>dis[u]+e[i].c)
    			{
    				dis[e[i].to]=dis[u]+e[i].c;
    				fr[e[i].to]=i;
    				if(!v[e[i].to])
    				{
    					v[e[i].to]=1;
    					q.push(e[i].to);
    				}
    			}
    	}
    	return dis[t]<1e9;
    }
    void mcf()
    {
    	int x=1e9;
    	for(int i=fr[t];i;i=fr[e[i].no])
    		x=min(x,e[i].va);
    	sum+=x;
    	for(int i=fr[t];i;i=fr[e[i].no])
    	{
    		val+=e[i].c*x;
    		e[i].va-=x;
    		e[i^1].va+=x;
    	}
    }
    int main()
    {
    	for(int i=1;i<16;i++)
    		b[i]=b[i-(i&(-i))]+1;
    	n=read(),m=read();
    	s=0,t=n*m*5+1;
    	for(int i=1;i<=n;i++)
    		for(int j=1;j<=m;j++)
    		{
    			tot++,lz=0;
    			int o=(i+j)&1,x=read();
    			if(o)
    				ins(s,m(tot),1e9,0,0),sm+=b[x];
    			else
    				ins(m(tot),t,1e9,0,0),smm+=b[x];
    			if(i>1)
    				ins(d(tot-m),u(tot),1,0,o);
    			if(j>1)
    				ins(r(tot-1),l(tot),1,0,o);
    			if(x&1)
    				ins(u(tot),m(tot),1,0,o);
    			if(x&2)
    				ins(r(tot),m(tot),1,0,o);
    			if(x&4)
    				ins(d(tot),m(tot),1,0,o);
    			if(x&8)
    				ins(l(tot),m(tot),1,0,o);
    			lz=d[x];
    			if(b[x]==1)
    			{
    				ins(r(tot),u(tot),1,1,o);
    				ins(d(tot),u(tot),1,2,o);
    				ins(l(tot),u(tot),1,1,o);
    			}
    			if(b[x]==2&&x!=10&&x!=5)
    			{
    				ins(d(tot),u(tot),1,1,o);
    				ins(l(tot),r(tot),1,1,o);
    			}
    			if(b[x]==3)
    			{
    				ins(d(tot),l(tot),1,1,o);
    				ins(d(tot),u(tot),1,2,o);
    				ins(d(tot),r(tot),1,1,o);
    			}
    		}
    	while(spfa())
    		mcf();
    	// cerr<<n*m<<" "<<val<<endl;
    	printf("%d
    ",(sm!=smm||sm!=sum)?-1:val);
    	return 0;
    }
    
  • 相关阅读:
    [Docker]如何实现使用scp从任意路径中向Docker镜像中复制文件
    puma web server如何监听所有IP地址
    Consider increasing the configuration parameter "max_wal_size
    ABFramework中使用select查询表时,想要排除某些字段不显示的方法
    FireDAC内存表过虑的错误
    有了ABMeeting,远程控制再也不用什么向X葵了
    ABFramework中设置关联表下拉选择的方法
    太牛逼了
    python3 利用scapy抓取网卡数据包并保存pcap
    json数组根据某属性去重
  • 原文地址:https://www.cnblogs.com/lokiii/p/10756637.html
Copyright © 2020-2023  润新知