zoukankan      html  css  js  c++  java
  • [BZOJ1324]Exca王者之剑

    Description

    Input
    第一行给出数字N,M代表行列数.N,M均小于等于100 下面N行M列用于描述数字矩阵

    Output
    输出最多可以拿到多少块宝石

    Sample Input
    2 2
    1 2
    2 1

    Sample Output
    4


    可以发现,所有能取得宝石的点必定是不相邻的,如果本题点权都为1,我们只需要建立二分图然后求最大独立点集即可。但是这题有点权,也就是变成了求最大带权独立点集,那么我们考虑不用二分图,使用网络流,从源点向所有白点连一条流量为点权的边,所有黑点向汇点连一条流量为点权的边,所有白点向其相邻的黑点连边,答案即为总点权减去最小割

    /*program from Wolfycz*/
    #include<cmath>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define inf 0x7f7f7f7f
    #define lowbit(x) ((x)&-(x))
    using namespace std;
    typedef long long ll;
    typedef unsigned int ui;
    typedef unsigned long long ull;
    inline char gc(){
    	static char buf[1000000],*p1=buf,*p2=buf;
    	return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
    }
    inline int frd(){
    	int x=0,f=1; char ch=gc();
    	for (;ch<'0'||ch>'9';ch=gc())	if (ch=='-')	f=-1;
    	for (;ch>='0'&&ch<='9';ch=gc())	x=(x<<3)+(x<<1)+ch-'0';
    	return x*f;
    }
    inline int read(){
    	int x=0,f=1; char ch=getchar();
    	for (;ch<'0'||ch>'9';ch=getchar())	if (ch=='-')	f=-1;
    	for (;ch>='0'&&ch<='9';ch=getchar())	x=(x<<3)+(x<<1)+ch-'0';
    	return x*f;
    }
    inline void print(int x){
    	if (x<0)	putchar('-'),x=-x;
    	if (x>9)	print(x/10);
    	putchar(x%10+'0');
    }
    const int N=1e4,M=6e4;
    const int dx[4]={0,0,-1,1};
    const int dy[4]={-1,1,0,0};
    int pre[M+10],now[N+10],child[M+10],val[M+10];
    int h[N+10],deep[N+10];
    int n,m,S,T,Ans,tot=1;
    void join(int x,int y,int z){pre[++tot]=now[x],now[x]=tot,child[tot]=y,val[tot]=z;}
    void insert(int x,int y,int z){join(x,y,z),join(y,x,0);}
    bool in_map(int x,int y){return x>0&&x<=n&&y>0&&y<=m;}
    int G(int x,int y){return (x-1)*m+y;}
    bool bfs(){
    	int head=1,tail=1;
    	memset(deep,255,sizeof(deep));
    	deep[h[head]=S]=0;
    	for (;head<=tail;head++){
    		int Now=h[head];
    		for (int p=now[Now],son=child[p];p;p=pre[p],son=child[p]){
    			if (!~deep[son]&&val[p]){
    				deep[h[++tail]=son]=deep[Now]+1;
    				if (son==T)	return 1;
    			}
    		}
    	}
    	return 0;
    }
    int dfs(int x,int v){
    	if (x==T)	return v;
    	int Ans=0;
    	for (int p=now[x],son=child[p];p;p=pre[p],son=child[p]){
    		if (deep[son]>deep[x]&&val[p]){
    			int t=dfs(son,min(val[p],v));
    			val[p]-=t,v-=t;
    			val[p^1]+=t,Ans+=t;
    			if (!v)	break;
    		}
    	}
    	if (!Ans)	deep[x]=-1;
    	return Ans;
    }
    int main(){
    	n=read(),m=read(),S=n*m+1,T=S+1;
    	for (int i=1;i<=n;i++){
    		for (int j=1;j<=m;j++){
    			int v=read(); Ans+=v;
    			if ((i+j)&1){
    				insert(S,G(i,j),v);
    				for (int k=0;k<4;k++){
    					int tx=i+dx[k],ty=j+dy[k];
    					if (!in_map(tx,ty))	continue;
    					insert(G(i,j),G(tx,ty),inf);
    				}
    			}else	insert(G(i,j),T,v);
    		}
    	}
    	while (bfs())	Ans-=dfs(S,inf);
    	printf("%d
    ",Ans);
    	return 0;
    }
    
  • 相关阅读:
    Axure RP使用攻略--动态面板滑动效果(10)
    Axure RP使用攻略--带遮罩层的弹出框(9)
    Axure RP使用攻略--动态面板的用途(8)
    Axure RP使用攻略--入门级(七)之axure元件使用思路的补充
    Axure RP使用攻略--入门级(六)关于Axure rp触发事件中IF和ELSE IF的使用说明
    Axure RP使用攻略--入门级(五)系统函数与变量
    Axure RP使用攻略--入门级(四)条件生成器
    Axure RP使用攻略--入门级(三)元件的触发事件
    Altium Designer 14安装破解
    小米手环暴力拆解
  • 原文地址:https://www.cnblogs.com/Wolfycz/p/10252328.html
Copyright © 2011-2022 走看看