首页 > 搜索 > BFS搜索 > HDU 4307-Matrix-图-[解题报告]HOJ
2015
05-23

HDU 4307-Matrix-图-[解题报告]HOJ

Matrix

问题描述 :

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).

输入:

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.

输出:

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.

样例输入:

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

样例输出:

2
Hint
For sample, A=[1, 1, 0] or A=[1, 1, 1] would get the maximum D.

从本质上讲,之所以能够用最大流解决这个问题,关键在于最大流可以求解下面这个函数的最小值:

Matrix

接下来就分析一下如何用最大流求解上面这个函数的极值。

首先xi一共只有两种选择,那么最终可以按xi的取值将xi划分成两个集合,那么如果xi在值为1的集合里,xj在值为0的集合里,那么就会产生一个代价cij。同时如果xi选择0就会产生一个bi的代价,如果xi选择1就会产生一个ai的代价。

于是构造一个源点S,汇点T做最小割,不妨假设做完最小割之后值为1的xi的集合是和S相连的部分,值为0的xi的集合是和T相连的部分。

由于表达式中有三项,我们用三种割边来分别描述这三项的值。一种是xi选择了1,这样就不能选择0,需要把xi-T这条边割掉,由于xi选择1会产生ai的代价,那么就把这条边的容量设为ai。另一种是xi选择了0,这样就不能选择1,需要把S-xi这条边割掉,由于xi选择0会产生bi的代价,那么就把这条边的容量设为bi。最后一种是xi选择了1,xj选择了0,这样xi和xj不能在同一个集合中,需要把xi-xj这条边割掉,由于xi选择1,xj选择0产生cij的代价,那么就把这条边的容量设为cij。

这样对建好的图做最小割就可以得到上面哪个函数的最小值。

接着我们分析这个题目如何转化成上面这种模型。

首先我们将D的表达式赤裸裸地写出来:

Matrix

这种形式必然不能看出来和上面那个表达式有什么关系,于是我们继续将其化简:

Matrix

如果令f等于最后一行括号里的内容,那么发生了什么?如果ai选择0会产生sum{bij}(1<=j<=N)的代价,如果ai选择1会产生ci的代价,如果ai选择1且aj选择0就会产生bij的代价。这样就完全转化成了上面的模型,具体的做法就不再重复说明了。


#include<cstdio>
#include<cstring>
#include<algorithm>
#define MAXD 1010
#define MAXM 2004010
#include<iostream>
#define INF 0x7fffffff
using namespace std;

int head[MAXD], e;
int v[MAXM], next[MAXM], flow[MAXM];
int lev[MAXD], q[MAXD], out[MAXD];

void init()
{
    e=0;
    memset(head,-1,sizeof(head));
}

void add(int x, int y, int z)
{
    v[e]=y;
    flow[e]=z;
    next[e]=head[x];
    head[x]=e++;
    v[e]=x;
    flow[e]=0;
    next[e]=head[y];
    head[y]=e++;
}

int bfs(int S,int T)
{
    int  rear = 0;
    memset(lev, -1, sizeof(lev));
    lev[S] = 0, q[rear ++] = S;
    for(int i = 0; i < rear; i ++)
        for(int j = head[q[i]]; j != -1; j = next[j])
            if(flow[j] && lev[v[j]] == -1)
            {
                lev[v[j]] = lev[q[i]] + 1, q[rear ++] = v[j];
                if(v[j] == T) return 1;
            }
    return 0;
}

int dfs(int cur, int a,  int T)
{
    if(cur == T)
        return a;
    for(int &i = out[cur]; i != -1; i = next[i])
        if(flow[i] && lev[v[i]] == lev[cur] + 1)
        {
            int t = dfs(v[i], min(a, flow[i]),T);
            if(t)
            {
                flow[i] -= t, flow[i ^ 1] += t;
                return t;
            }
        }
    return 0;
}

long long dinic(int S,int T)
{
    long long ans = 0;
    while(bfs(S,T))
    {
        memcpy(out, head, sizeof(head));
        while(int t = dfs(S, INF,T))
            ans += t;
    }
    return ans;
}
int x, a;
int N;
int S = 0, T = N + 1;
long long SUM;
void input()
{
    scanf("%d", &N);
    init();
    S=0;
    T=N+1;
    SUM = 0;
    for(int i = 1; i <= N; i ++)
    {
        a = 0;
        for(int j = 1; j <= N; j ++)
        {
            scanf("%d", &x), a += x;
            add(i, j, x);
        }
        SUM += a;
        add(S, i, a);
    }
    for(int i = 1; i <= N; i ++)
    {
        scanf("%d", &x);
        add(i, T, x);
    }
    cout<<SUM-dinic(S,T)<<endl;
}

int main()
{
    int t;
    scanf("%d", &t);
    while(t --)
    {
        input();
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/weiguang_123/article/details/8077385


, ,