zoukankan      html  css  js  c++  java
  • 最小费用最大流模板

    const int INF = 0x3f3f3f3f;
    const int MAXN = ; 
     
    struct Edge{
    	int value,flow,to,rev;
    	Edge(){}
    	Edge(int a,int b,int c,int d):to(a),value(b),flow(c),rev(d){}
    };
     
    vector<Edge> E[MAXN];
     
    inline void Add(int from,int to,int flow,int value){
    	E[from].push_back(Edge(to,value,flow,E[to].size()));
    	E[to].push_back(Edge(from,-value,0,E[from].size()-1));
    }
     
    bool book[MAXN];//用于SPFA中标记是否在queue中 
    int cost[MAXN];//存费用的最短路径 
    int pre[MAXN];//存前节点 
    int pree[MAXN];//存在前节点的vector中的下标 
     
    bool Spfa(int from,int to){
    	memset(book,false,sizeof book);
    	memset(cost,INF,sizeof cost);
    	book[from] = true;
    	cost[from] = 0;
    	queue<int> Q;
    	Q.push(from);
    	while(!Q.empty()){
    		int t = Q.front();
    		book[t] = false;
    		Q.pop();
    		for(int i=0 ; i<E[t].size() ; ++i){
    			Edge& e = E[t][i];
    			if(e.flow > 0 && cost[e.to] > cost[t] + e.value){
    				cost[e.to] = cost[t] + e.value;
    				pre[e.to] = t;
    				pree[e.to] = i;
    				if(book[e.to] == false){
    					Q.push(e.to);
    					book[e.to] = true;
    				}
    			}
    		}
    	}
    	return cost[to] != INF;
    }
     
    int Work(int from,int to){
    	int sum = 0;
    	while(Spfa(from,to)){
    		int mflow = INF;//SPFA找到的最短路径的最小容量 
    		int flag = to;
    		while(flag != from){
    			mflow = min(mflow,E[pre[flag]][pree[flag]].flow);
    			flag = pre[flag];
    		}
    		flag = to;
    		while(flag != from){
    			sum += E[pre[flag]][pree[flag]].value * mflow;
    			E[pre[flag]][pree[flag]].flow -= mflow;
    			E[flag][E[pre[flag]][pree[flag]].rev].flow += mflow;
    			flag = pre[flag];
    		}
    	}
    	return sum;//返回费用
    }
     
    
  • 相关阅读:
    Python 基础知识----数据类型
    drf 之序列化组件
    Django Rest framework 框架之解析器
    css选择器
    Python 爬虫 解析库的使用 --- Beautiful Soup
    Python 爬虫 解析库的使用 --- XPath
    动态渲染页面爬取(Python 网络爬虫) ---Selenium的使用
    HDU 1014(互质数 **)
    HDU 6432(不连续环排列 ~)
    HDU 6433(2的n次方 **)
  • 原文地址:https://www.cnblogs.com/vocaloid01/p/9514057.html
Copyright © 2011-2022 走看看