分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

Network-Flow

发布时间:2023-09-06 01:30责任编辑:彭小芳关键词:暂无标签
//Created by pritryint graph[MAX][MAX]; //原图int source; ?????????//起点,这里为0int sink; ???????????//终点,这里为n-1int e[MAX]; ?????????//余流int h[MAX]; ?????????//高度int n; ??????????????//顶点数struct Label{ ???int index; ???friend bool operator < (const Label& a, const Label& b) ???{ ???????return h[a.index] > h[b.index]; ???}};void Push(int u, int v){ ???int df = min(e[u], graph[u][v]); ???graph[u][v] -= df; ???graph[v][u] += df; ???e[u] -= df; ???e[v] += df;}void Relabel(int u){ ???int min_h = INF; ???for(int i = 0; i < n; ++i) ???{ ???????if(graph[u][i] && h[i] < min_h) ???????{ ???????????min_h = h[i]; ???????} ???} ???h[u] = min_h + 1;}int HLPP(){ ???int i; ???Label l; ???priority_queue<Label> Q; ???memset(e, 0, sizeof(e)); ???memset(h, 0, sizeof(h)); ???h[source] = n; ???for(i = 0; i < n; ++i) ???{ ???????if(graph[source][i]) ???????{ ???????????int df = graph[source][i]; ???????????e[source] -= df; ???????????e[i] += df; ???????????graph[source][i] -= df; ???????????graph[i][source] += df; ???????????l.index = i; ???????????if(i != source && i != sink) Q.push(l); ???????} ???} ???while(!Q.empty()) ???{ ???????l = Q.top(); ???????Q.pop(); ???????int u = l.index; ???????while(e[u]) ???????{ ???????????for(i = 0; i < n; ++i) ???????????????if(graph[u][i] && h[u] > h[i]) ???????????????????break; ???????????if(i == n) Relabel(u); ???????????for(i = 0; i < n; ++i) ???????????{ ???????????????if(h[u] == h[i] + 1 && graph[u][i]) ???????????????{ ???????????????????Push(u, i); ???????????????????l.index = i; ???????????????????if(e[i] > 0 && i != source && i != sink) Q.push(l); ???????????????} ???????????} ???????} ???} ???return e[sink];}

Network-Flow

原文地址:http://www.cnblogs.com/TheRoadToAu/p/8035237.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved