zoukankan      html  css  js  c++  java
  • HDU4307 Matrix(最小割)

    题目

    Source

    http://acm.hdu.edu.cn/showproblem.php?pid=4307

    Description

    Let A be a 1*N matrix, and each element of A is either 0 or 1. You are to find such A that maximize D=(A*B-C)*AT, where B is a given N*N matrix whose elements are non-negative, C is a given 1*N matrix whose elements are also non-negative, and AT is the transposition of A (i.e. a N*1 matrix).

    Input

    The first line contains the number of test cases T, followed by T test cases.
    For each case, the first line contains an integer N (1<=N<=1000).
    The next N lines, each of which contains N integers, illustrating the matrix B. The jth integer on the ith line is B[i][j].
    Then one line followed, containing N integers, describing the matrix C, the ith one for C[i].
    You may assume that sum{B[i][j]} < 2^31, and sum{C[i]} < 2^31.

    Output

    For each case, output the the maximum D you may get.

    Sample Input

    1
    3
    1 2 1
    3 1 0
    1 2 3
    2 3 7

    Sample Output

    2

    分析

    化一下那个矩阵,可以知道,目标是最大化这个:

    $$D = sum_{i=1}^N sum_{j=1}^N A_iA_jB_{ij} - sum_{i=1}^NC_i$$

    这样就是100多W个点的最大权闭合子图了= =。。

    其实式子还可以再化。。 http://blog.csdn.net/weiguang_123/article/details/8077385

    然后就化成了最小割的经典二者选一的模型了,POJ3469

    这我一点都化不来= =。。

    代码

    #include<cstdio>
    #include<cstring>
    #include<queue>
    #include<algorithm>
    using namespace std;
    #define INF (1<<30)
    #define MAXN 1111
    #define MAXM 2222*1111
     
    struct Edge{
        int v,cap,flow,next;
    }edge[MAXM];
    int vs,vt,NE,NV;
    int head[MAXN];
     
    void addEdge(int u,int v,int cap){
        edge[NE].v=v; edge[NE].cap=cap; edge[NE].flow=0;
        edge[NE].next=head[u]; head[u]=NE++;
        edge[NE].v=u; edge[NE].cap=0; edge[NE].flow=0;
        edge[NE].next=head[v]; head[v]=NE++;
    }
     
    int level[MAXN];
    int gap[MAXN];
    void bfs(){
        memset(level,-1,sizeof(level));
        memset(gap,0,sizeof(gap));
        level[vt]=0;
        gap[level[vt]]++;
        queue<int> que;
        que.push(vt);
        while(!que.empty()){
            int u=que.front(); que.pop();
            for(int i=head[u]; i!=-1; i=edge[i].next){
                int v=edge[i].v;
                if(level[v]!=-1) continue;
                level[v]=level[u]+1;
                gap[level[v]]++;
                que.push(v);
            }
        }
    }
     
    int pre[MAXN];
    int cur[MAXN];
    int ISAP(){
        bfs();
        memset(pre,-1,sizeof(pre));
        memcpy(cur,head,sizeof(head));
        int u=pre[vs]=vs,flow=0,aug=INF;
        gap[0]=NV;
        while(level[vs]<NV){
            bool flag=false;
            for(int &i=cur[u]; i!=-1; i=edge[i].next){
                int v=edge[i].v;
                if(edge[i].cap!=edge[i].flow && level[u]==level[v]+1){
                    flag=true;
                    pre[v]=u;
                    u=v;
                    //aug=(aug==-1?edge[i].cap:min(aug,edge[i].cap));
                    aug=min(aug,edge[i].cap-edge[i].flow);
                    if(v==vt){
                        flow+=aug;
                        for(u=pre[v]; v!=vs; v=u,u=pre[u]){
                            edge[cur[u]].flow+=aug;
                            edge[cur[u]^1].flow-=aug;
                        }
                        //aug=-1;
                        aug=INF;
                    }
                    break;
                }
            }
            if(flag) continue;
            int minlevel=NV;
            for(int i=head[u]; i!=-1; i=edge[i].next){
                int v=edge[i].v;
                if(edge[i].cap!=edge[i].flow && level[v]<minlevel){
                    minlevel=level[v];
                    cur[u]=i;
                }
            }
            if(--gap[level[u]]==0) break;
            level[u]=minlevel+1;
            gap[level[u]]++;
            u=pre[u];
        }
        return flow;
    }
    
    int B[1111][1111],C[1111];
    
    int main(){
    	int t,n;
    	scanf("%d",&t);
    	while(t--){
    		scanf("%d",&n);
    		int sum=0;
    		for(int i=1; i<=n; ++i){
    			for(int j=1; j<=n; ++j){
    				scanf("%d",&B[i][j]);
    				sum+=B[i][j];
    			}
    		}
    		for(int i=1; i<=n; ++i){
    			scanf("%d",&C[i]);
    		}
    		vs=0; vt=n+1; NV=vt+1; NE=0;
    		memset(head,-1,sizeof(head));
    		for(int i=1; i<=n; ++i){
    			addEdge(vs,i,C[i]);
    			int tmp=0;
    			for(int j=1; j<=n; ++j) tmp+=B[i][j];
    			addEdge(i,vt,tmp);
    			for(int j=1; j<=n; ++j){
    				if(i!=j) addEdge(j,i,B[i][j]);
    			}
    		}
    		printf("%d
    ",sum-ISAP());
    	}
    	return 0;
    }
    
  • 相关阅读:
    无法添加数据库未能加载文件或程序集“Microsoft.SqlServer.Management.Sdk.Sfc, Version=10.0.0.0, Culture=neutral,PublicKeyToken=89845dcd8080c
    转载:自己制作Visual Studio项目模板(以原有项目为模版) VS—项目模板丢失的解决方案
    设计一个高效的缓存管理服务 C#
    Visual Studio 30个快捷键2009年05月22日
    Everything 中文绿色版
    Visual studio 打包
    远程桌面连接超出最大连接数的3种解决办法
    [Cache 学习] Cache.Insert 与 Cache.Add 区别
    三层架构之我见 —— 不同于您见过的三层架构。
    基于IIS发布你的WCF Service。
  • 原文地址:https://www.cnblogs.com/WABoss/p/5802323.html
Copyright © 2011-2022 走看看