2014 Multi-University Training Contest 10 1001 最大权闭合图

来源:互联网 发布:数据特征分析 编辑:程序博客网 时间:2024/05/29 11:40

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
42 310 106 6 62 0 12 1 20 1 01 0 00 0 02 310 108 10 61 01 20 1 01 0 00 0 02 310 108 10 61 01 20 1 00 0 00 0 02 310 108 10 61 01 20 0 01 0 00 0 0
 

Sample Output
Case #1: 2Case #2: 4Case #3: 4Case #4: 6
 

Source
2014 Multi-University Training Contest 10


题目大意:给你n个任务,每个任务都有一定的利润,完成一个任务有可能需要攻克一些难题,有m个难题,解决某个难题需要一定的花费,并且难题之间有依赖关系。求最大能取得利润。感觉就是一个裸裸的最大权闭合图。题解中缩点了,我没有,直接跑,虽然A了,但是会不会是数据太水?还要想想为啥不缩点也对了。


#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <vector>#include <queue>#define INF 0x3f3f3f3f#define MAXN 1000using namespace std;int val[MAXN], x[MAXN], y[MAXN];int mark[MAXN];struct edge { int to, cap, rev; };vector<edge> G[MAXN];int level[MAXN];int iter[MAXN];void add_edge(int from, int to, int cap) {    G[from].push_back((edge){to, cap, G[to].size()});    G[to].push_back((edge){from, 0, G[from].size()});}void bfs(int s) {    memset(level, -1, sizeof level);    queue<int> que;    level[s] = 0;    que.push(s);    while (!que.empty()) {        int v = que.front(); que.pop();        for (int i = 0; i < G[v].size(); i++) {            edge &e = G[v][i];            if (e.cap > 0 && level[e.to] < 0) {                level[e.to] = level[v] + 1;                que.push(e.to);            }        }    }}int dfs(int v, int t, int f) {    if(v == t) return f;    for (int &i = iter[v]; i < G[v].size(); i++) {        edge &e = G[v][i];        if (e.cap > 0 && level[v] < level[e.to]) {            int d = dfs(e.to, t, min(f, e.cap));            if (d > 0) {                e.cap -= d;                G[e.to][e.rev].cap += d;                return d;            }        }    }    return 0;}int max_flow(int s, int t) {    int flow = 0;    for(;;) {        bfs(s);        if (level[t] < 0) return flow;        memset(iter, 0, sizeof iter);        int f;        while ((f = dfs(s, t, INF)) > 0) {            flow += f;        }    }}int main(){    int t;    scanf("%d", &t);    for (int Case = 1; Case <= t; Case++) {        int n, m;        scanf("%d%d", &n, &m);        for (int i = 0; i <= n + m + 1; i++) {            G[i].clear();        }        int sum = 0;        for (int i = 1; i <= n; i++) {            int x;            scanf("%d", &x);            sum += x;            add_edge(0, i, x);        }        for (int i = 1; i <= m; i++) {            int x;            scanf("%d", &x);            add_edge(n + i, n + m + 1, x);        }        for (int i = 1; i <= n; i++) {            int k;            scanf("%d", &k);            for (int j = 1; j <= k; j++) {                int x;                scanf("%d", &x);                add_edge(i, n + x + 1, INF);            }        }        for (int i = 1; i <= m; i++) {            for (int j = 1; j <= m; j++) {                int x;                scanf("%d", &x);                if(x) {                    add_edge(i + n, j + n, INF);                }            }        }        int ans = max_flow(0, n + m + 1);        printf("Case #%d: %d\n", Case, sum - ans);    }    return 0;}




0 0
原创粉丝点击