How to earn more-最小割/FordFulkerson/Dinic

小明是一名计算机科学专家,每月能接多个项目。他正在思考如何在一个月内尽可能多地赚取收入。通过合理选择项目和雇佣员工,他希望在扣除员工工资后,能够获得最大的收益。本博客详细介绍了如何利用网络流算法(如Ford-Fulkerson或Dinic算法)来解决这个问题。
How to earn more
Source:HOJ-2634

Xiao Ming is an expert in computer science and technology, so he can get a lot of projects every month. The projects always bring him a lot of money, now he is thinking how to earn money as more as possible.
Every month he can get m projects, and each project Ai will bring him Xi yuan. Although Xiao Ming is an expert, he still needs to hire some other guys to help him. Of course, the employees are not as good as Xiao Ming, for they are just good at some single aspect. So, they should work together to finish one project. There is a list shows the salary of m employees, who are labeled from 0 to m-1. Xiao Ming only hires employees, in that list, and he knows who will be needed by each project.If one employee is hired, he can join in several projects.

Input
The first line is an integer c shows the number of cases. For each case, the first line has two numbers m,n(m,n <=100), denoting that there is m projects and n employees on the list.The second line has m integers, which are seperated by a single blank, the ith number Ximeans the project Ai will bring Xiao Ming Xi yuan. Xi is less the 10000. The third line has n integers, which are seperated by a single blank, the ith number Yimeans the employee Bi will cost Xiao Ming Yi yuan. And the next m lines will show which part of the employees will be needed by each project. Line i is a list of the employees, who are needed by project Ai. In each line, first a number Zi shows the number of employees needed by this project. And Zi labels of the emloyees follows, which are still seperated by a sigle blank.

Output
You should output a single integer shows the maximun money Xiao Ming can earn in a single month. The money he can earn is equall to the money he can totally get minus the money he totally cost. You should not leave any extra blanks at the end of each line.

Sample Input
1
3 5
30 40 43
55 17 23 22 11
3 0 1 2
3 1 2 3
2 2 1

Sample Output
21

源代码一(Ford-Fulkerson):
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;

queue <int> Q;
const int MAX = 208;
const int INF = 1<<30;
int  P , E , S , T;
int  map[MAX][MAX] , p[MAX] , e[MAX] , mark[MAX] , pre[MAX];
//map是网络流图,mark是bfs标志数组,pre是更新的上一个结点

int  FordFulkerson( void );
int  bfs( void );
int  update( void );
int  min( int a , int b ){ return a <= b ? a : b; }

int main( ){
        int t , i , j , n , sum;
        
        cin>>t;

        while( t-- ){
                memset( map , 0 , sizeof( map ) );
                cin>>P>>E;
                sum = 0;
                S = 0;
                T = P + E + 1;
                for( i=1 ; i<=P ; i++ ){
                        cin>>p[i];
                        sum += p[i];    //总的费用叠加
                }
                for( i=1 ; i<=E ; i++ )
                        cin>>e[i];
                for( i=1 ; i<=P ; i++ ){
                        cin>>n;
                        while( n-- ){
                                cin>>j;
                                map[j+1][i+E] = INF;    //形成employee->project流
                        }
                }
                for( i=1 ; i<=E ; i++ )
                        map[S][i] = e[i];    //形成源点->employee流
                for( i=1 ; i<=P ; i++ )
                        map[i+E][T] = p[i];    //形成project->汇点图

                cout<<sum-FordFulkerson()<<endl;        //最小费用与最大流的关系
        }

        return 0;
}

int  FordFulkerson( void ){
        int sum = 0;

        while( bfs( ) ){
                sum += update( );
        }

        return sum;
}

int  bfs( void ){
        int u , v;

        while( !Q.empty() )    //一定要清空
                Q.pop();

        memset( mark , 0 , sizeof( mark ) );
        memset( pre , -1 , sizeof( pre ) );
        Q.push( S );
        mark[S] = 1;

        while( !Q.empty() ){
                u = Q.front();
                Q.pop();

                if( u==T )
                        return 1;

                for( v=S ; v<=T ; v++ ){
                        if( !mark[v] && map[u][v] ){
                                mark[v] = 1;
                                Q.push( v );
                                pre[v] = u;    //前驱结点
                        }
                }
        }

        return 0;
}

int  update( void ){
        int v , u , minimum;

        v = T;
        minimum = INF;

        while( v!=S ){
                u = pre[v];
                minimum = min( minimum , map[u][v] );
                v = u;
        }

        v = T;
        while( v!=S ){
                u = pre[v];
                map[u][v] -= minimum;    //正减
                map[v][u] += minimum;    //逆加
                v = u;
        }

        return minimum;
}


源代码二(Dinic):
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;

queue <int> Q;
const int MAX = 208;
const int INF = 1<<30;
int  P , E , S , T;
int  map[MAX][MAX] , p[MAX] , e[MAX] , mark[MAX] , level[MAX];
//level是bfs的层次数组

int  Dinic( void );
int  bfs( void );
int  dfs( int u , int flow );
int  min( int a , int b ){ return a <= b ? a : b; }

int main( ){
        int t , i , j , n , sum;
        
        cin>>t;

        //每个变量表示的含义和上一个代码一致
        while( t-- ){
                memset( map , 0 , sizeof( map ) );
                cin>>P>>E;
                sum = 0;
                S = 0;
                T = P + E + 1;
                for( i=1 ; i<=P ; i++ ){
                        cin>>p[i];
                        sum += p[i];
                }
                for( i=1 ; i<=E ; i++ )
                        cin>>e[i];
                for( i=1 ; i<=P ; i++ ){
                        cin>>n;
                        while( n-- ){
                                cin>>j;
                                map[j+1][i+E] = INF;
                        }
                }
                for( i=1 ; i<=E ; i++ )
                        map[S][i] = e[i];
                for( i=1 ; i<=P ; i++ )
                        map[i+E][T] = p[i];

                cout<<sum-Dinic()<<endl;
        }

        return 0;
}

int  Dinic( void ){
        int sum = 0;

        while( bfs( ) ){    //bfs分层
                sum += dfs( S , INF );    //dfs更新多条增广路
        }

        return sum;
}

int  bfs( void ){
        int u , v;

        while( !Q.empty() )    //一定要有清空队列操作
                Q.pop();

        memset( mark , 0 , sizeof( mark ) );
        memset( level , -1 , sizeof( level ) );
        Q.push( S );
        mark[S] = 1;
        level[S] = 0;

        while( !Q.empty() ){
                u = Q.front();
                Q.pop();

                if( u==T )
                        return 1;

                for( v=S ; v<=T ; v++ ){
                        if( !mark[v] && map[u][v] ){
                                mark[v] = 1;
                                Q.push( v );
                                level[v] = level[u] + 1;    //层次加一
                        }
                }
        }

        return 0;
}

int  dfs( int u , int flow ){
        int v , flowSum , sum=0;

        if( u==T )    //递归到了汇点
                return flow;

        for( v=S ; v<=T ; v++ ){
                if( level[v]==level[u]+1 && map[u][v] ){
                        flowSum = dfs( v , min( map[u][v] , flow ) );    //该结点拓展的所有流出量
                        map[u][v] -= flowSum;    //正减
                        map[v][u] += flowSum;    //逆加
                        flow -= flowSum;        //可流的流向减少
                        sum += flowSum;    //总的流量累加
                }
        }

        return sum;
}

代码分析:之前使用了Dinic算法TLE了,然后就很纳闷,这都能TLE,该不成要学更先进的算法SAP什么的?然后就看了标程才发现bfs队列忘了每次都清空..囧..总之,Dinic是FordFulkerson算法的优化,一次更新多条增广路,dfs那个地方不大好调试,当个模板记住也行.hiahia..
随着人类对生命健康需求的不断增长,新药研发面临着前所未有的挑战。传统的药物研发流程通常耗时长达十年以上,耗资数十亿美元,且最终成功率极低,这在制药界被称为“反摩尔定律”困境。近年来,人工智能技术的飞速发展,特别是深度学习和大数据分析的广泛应用,为新药发现带来了革命性的契机。人工智能能够从海量的化学和生物数据中挖掘潜在规律,显著加速药物靶点发现、先导化合物优化等关键环节。在此背景下,本研究旨在设计并实现一个基于人工智能的新药发现辅助系统,以期为传统药物研发流程提供高效的智能化辅助工具,从而有效缩短研发周期并大幅降低研发成本。本研究以Python作为主要开发语言,深度结合PyTorch和TensorFlow两大主流深度学习框架,并集成RDKit化学信息学工具包,构建了一个功能完善的新药发现辅助系统。系统的核心目标是利用先进的人工智能技术辅助新药分子的设计与活性评估。在研究方法上,本文创新性地提出了一种融合多模态数据的新药发现算法。该算法综合处理分子的多种表示形式,包括一维的SMILES序列、二维的分子图结构以及三维的空间构象数据。通过构建多通道神经网络,系统能够有效提取并融合不同模态的特征,从而全面捕捉分子的理化性质与生物学活性之间的复杂非线性关系。 【课程报告内容】 摘要 第1章 绪论 第2章 相关技术与理论 第3章 系统需求分析 第4章 系统总体设计 第5章 系统详细设计与实现 第6章 系统测试与分析 第7章 总结与展望 参考文献 附件-实现指南
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值