HDU 4971 A simple brute force problem.(最大流)

题目大意:给你一些技术和任务,你完成一个任务会获利x元,但是你得学习技术,每个技术之间又有一个先后的关系,你学习这个技术之前你得先学其他的技术。每个技术学一次就可以了啊,也就是说你这次学会了这个技术那么以后用到就不用学习了。

听说这题是最大权闭合图,后来看了看,这里有链接:http://www.cnblogs.com/wuyiqi/archive/2012/03/12/2391960.html

解题思路:把每个技术拆点,之间的容量为它的花费。源点到每个项目建边容量为项目的收益,其他边的容量都为INF,求出最大流来,总的收益减去最大流就是收益和。

A simple brute force problem.

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 337    Accepted Submission(s): 191


Problem Description
There's a company with several projects to be done. Finish a project will get you profits. However, there are some technical problems for some specific projects. To solve the problem, the manager will train his employee which may cost his budget. There may be dependencies between technical problems, for example, A requires B means you need to solve problem B before solving problem A. If A requires B and B requires A, it means that you should solve them at the same time. You can select which problems to be solved and how to solve them freely before finish your projects. Can you tell me the maximum profit?
 

Input
The first line of the input is a single integer T(<=100) which is the number of test cases. 

Each test case contains a line with two integer n(<=20) and m(<=50) which is the number of project to select to complete and the number of technical problem.

Then a line with n integers. The i-th integer(<=1000) means the profit of complete the i-th project.

Then a line with m integers. The i-th integer(<=1000) means the cost of training to solve the i-th technical problem.

Then n lines. Each line contains some integers. The first integer k is the number of technical problems, followed by k integers implying the technical problems need to solve for the i-th project.

After that, there are m lines with each line contains m integers. If the i-th row of the j-th column is 1, it means that you need to solve the i-th problem before solve the j-th problem. Otherwise the i-th row of the j-th column is 0.
 

Output
For each test case, please output a line which is "Case #X: Y ", X means the number of the test case and Y means the the maximum profit.
 

Sample Input
  
  
4 2 3 10 10 6 6 6 2 0 1 2 1 2 0 1 0 1 0 0 0 0 0 2 3 10 10 8 10 6 1 0 1 2 0 1 0 1 0 0 0 0 0 2 3 10 10 8 10 6 1 0 1 2 0 1 0 0 0 0 0 0 0 2 3 10 10 8 10 6 1 0 1 2 0 0 0 1 0 0 0 0 0
 

Sample Output
  
  
Case #1: 2 Case #2: 4 Case #3: 4 Case #4: 6
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-12
///#define M 1000100
#define LL __int64
///#define LL long long
///#define INF 0x7ffffff
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
#define zero(x) ((fabs(x)<eps)?0:x)

using namespace std;

const int maxn = 1010;

int cnt;
int n, m;
int cur[maxn], head[maxn];
int dis[maxn], gap[maxn];
int aug[maxn], pre[maxn];
int deep[maxn];

struct node
{
    int v, w;
    int next;
} f[500100];
void init()
{
    cnt = 0;
    memset(head, -1, sizeof(head));
}

void add(int u, int v, int w)
{
    f[cnt].v = v;
    f[cnt].w = w;
    f[cnt].next = head[u];
    head[u] = cnt++;

    f[cnt].v = u;
    f[cnt].w = 0;
    f[cnt].next = head[v];
    head[v] = cnt++;
}

int SAP(int s, int e, int n)
{
    int max_flow = 0, v, u = s;
    int id, mindis;
    aug[s] = INF;
    pre[s] = -1;
    memset(dis, 0, sizeof(dis));
    memset(gap, 0, sizeof(gap));
    gap[0] = n;
    for (int i = 0; i <= n; ++i)  cur[i] = head[i];/// 初始化当前弧为第一条弧
    while (dis[s] < n)
    {
        bool flag = false;
        if (u == e)
        {
            max_flow += aug[e];
            for (v = pre[e]; v != -1; v = pre[v]) /// 路径回溯更新残留网络
            {
                id = cur[v];
                f[id].w -= aug[e];
                f[id^1].w += aug[e];
                aug[v] -= aug[e]; /// 修改可增广量,以后会用到
                if (f[id].w == 0) u = v; /// 不回退到源点,仅回退到容量为0的弧的弧尾
            }
        }
        for (id = cur[u]; id != -1; id = f[id].next)/// 从当前弧开始查找允许弧
        {
            v = f[id].v;
            if (f[id].w > 0 && dis[u] == dis[v] + 1) /// 找到允许弧
            {
                flag = true;
                pre[v] = u;
                cur[u] = id;
                aug[v] = min(aug[u], f[id].w);
                u = v;
                break;
            }
        }
        if (flag == false)
        {
            if (--gap[dis[u]] == 0) break; ///gap优化,层次树出现断层则结束算法
            mindis = n;
            cur[u] = head[u];
            for (id = head[u]; id != -1; id = f[id].next)
            {
                v = f[id].v;
                if (f[id].w > 0 && dis[v] < mindis)
                {
                    mindis = dis[v];
                    cur[u] = id; /// 修改标号的同时修改当前弧
                }
            }
            dis[u] = mindis + 1;
            gap[dis[u]]++;
            if (u != s) u = pre[u]; /// 回溯继续寻找允许弧
        }
    }
    return max_flow;
}

int num1[maxn], num2[maxn];
int main()
{
    int T;
    cin >>T;
    int Case = 1;
    while(T--)
    {
        init();
        int n, m;
        int sum = 0;
        scanf("%d %d",&n , &m);
        for(int i = 1; i <= n; i++)
        {
             scanf("%d",&num1[i]);
             sum += num1[i];
        }
        for(int i = 1; i <= m; i++) scanf("%d",&num2[i]);
        int k;
        int x;
        for(int i = 1; i <= n; i++)
        {
            scanf("%d",&k);
            for(int j = 1; j <= k; j++)
            {
                scanf("%d", &x);
                add(2*m+i, x+1, INF);
            }
            add(0, 2*m+i, num1[i]);
        }
        for(int i = 1; i <= m; i++)
        {
            for(int j = 1; j <= m; j++)
            {
                scanf("%d",&x);
                if(x) add(i, j, INF);
            }
        }
        for(int i = 1; i <= m; i++)
        {
            add(i, i+m, num2[i]);
            add(i+m, 2*m+n+1, INF);
        }
        int S = 0, T = 2*m+n+1, en = T+2;
        int ans = SAP(S, T, en);
        printf("Case #%d: ", Case++);
        cout<<sum-ans<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值