• BZOJ 2406: 矩阵 [上下界网络流 二分答案]


    2406: 矩阵

    题意:自己去看吧,最小化每行每列所有元素与给定矩阵差的和的绝对值中的最大值


    又带绝对值又带max不方便直接求
    显然可以二分这个最大值
    然后判定问题,给定矩阵每行每列的范围和每个元素的取值范围判断可行...和之前做过的一样了上下界可行流
    1A好开心啊

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    #define fir first
    #define sec second
    typedef long long ll;
    const int N=2005, M=1e5+5, INF=1e9;
    inline ll read(){
        char c=getchar();ll x=0,f=1;
        while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
        while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
        return x*f;
    }
    
    int n, m, l, r, a, row[N], col[N], s, t, extra[N], tot;
    
    struct edge{int v, c, f, ne;}e[M];
    int cnt=1, h[N];
    inline void ins(int u, int v, int c) { 
    	e[++cnt]=(edge){v, c, 0, h[u]}; h[u]=cnt;
    	e[++cnt]=(edge){u, 0, 0, h[v]}; h[v]=cnt;
    }
    
    int q[N], head, tail, vis[N], d[N], cur[N];
    bool bfs(int s, int t) {
    	memset(vis, 0, sizeof(vis));
    	head=tail=1;
    	q[tail++]=s; d[s]=0; vis[s]=1;
    	while(head!=tail) {
    		int u=q[head++];
    		for(int i=h[u];i;i=e[i].ne) 
    			if(!vis[e[i].v] && e[i].c>e[i].f) {
    				vis[e[i].v]=1; d[e[i].v]=d[u]+1;
    				q[tail++]=e[i].v;
    				if(e[i].v == t) return true;
    			}
    	}
    	return false;
    }
    int dfs(int u, int a, int t) { 
    	if(u==t || a==0) return a;
    	int flow=0, f;
    	for(int &i=cur[u];i;i=e[i].ne) 
    		if(d[e[i].v]==d[u]+1 && (f=dfs(e[i].v, min(a, e[i].c-e[i].f), t))>0) {
    			flow+=f;
    			e[i].f+=f;
    			e[i^1].f-=f;
    			a-=f;
    			if(a==0) break;
    		}
    	if(a) d[u]=-1;
    	return flow;
    }
    int dinic(int s, int t) {
    	int flow=0;
    	while(bfs(s, t)) {
    		for(int i=0; i<=tot; i++) cur[i]=h[i];
    		flow+=dfs(s, INF, t); 
    	}
    	return flow;
    }
    
    bool check(int mid) { //printf("
    check %d
    ",mid);
    	s=0; t=n+m+1;
    	cnt=1; memset(h,0,sizeof(h)); memset(extra,0,sizeof(extra));
    	for(int i=1; i<=n; i++)
    		for(int j=1; j<=m; j++) ins(i, n+j, r-l), extra[i]-=l, extra[n+j]+=l;
    	for(int i=1; i<=n; i++) {
    		int l = -mid+row[i], r = mid+row[i];
    		ins(s, i, r-l), extra[s]-=l, extra[i]+=l;
    	}
    	for(int j=1; j<=m; j++) {
    		int l = -mid+col[j], r = mid+col[j];
    		ins(n+j, t, r-l), extra[n+j]-=l, extra[t]+=l;
    	}
    	int ss=t+1, tt=t+2, sum=0; tot=tt;
    	for(int i=s; i<=t; i++) {
    		if(extra[i]>0) ins(ss, i, extra[i]), sum+=extra[i];
    		if(extra[i]<0) ins(i, tt, -extra[i]);
    	}
    	ins(t, s, INF);
    
    	int flow=dinic(ss, tt); //printf("flow %d %d
    ",flow,sum);
    	return flow==sum;
    }
    void solve() {
    	int l=0, r=200000, ans=-1;
    	while(l<=r) {
    		int mid=(l+r)>>1;
    		if(check(mid)) ans=mid, r=mid-1;
    		else l=mid+1;
    	}
    	printf("%d",ans);
    }
    int main() {
    	freopen("in","r",stdin);
    	n=read(); m=read();
    	for(int i=1; i<=n; i++) 
    		for(int j=1; j<=m; j++) a=read(), row[i]+=a, col[j]+=a;
    	
    	l=read(); r=read();
    	solve();
    }
    
  • 相关阅读:
    【Aizu
    【Aizu
    【OpenJ_Bailian
    leetcode-225-Implement Stack using Queues
    leetcode-219-Contains Duplicate II(使用set来判断长度为k+1的闭区间中有没有重复元素)
    leetcode-217-Contains Duplicate(使用排序来判断整个数组有没有重复元素)
    leetcode-840-Magic Squares In Grid
    leetcode-198-House Robber(动态规划)
    leetcode-191-Number of 1 Bits
    leetcode-189-Rotate Array
  • 原文地址:https://www.cnblogs.com/candy99/p/6637050.html
Copyright © 2020-2023  润新知