POJ 1149 PIGS ( 网络流 )

主要是建图有些小烦。若几个节点流量的来源或者去向全然同样,且流量为 INF,将它们合并成一个节点。
若从两点间有且仅有一条容量为 INF 的边。将两点合并成一个节点。


#include <iostream>
#include <cstring>
#include <queue>
using namespace std;

const int INF = 0x7fffffff;
const int MAXN = 110;

int capacity[MAXN][MAXN], flow[MAXN][MAXN];
int parent[MAXN], residual[MAXN];
int max_flow, node_num;


void edmond_karp( int source, int target ) {

    queue< int > Q;
    memset( flow, 0, sizeof( flow ) );
    memset( parent, 0, sizeof( parent ) );
    max_flow = 0;

    while ( true ) {
        memset( residual, 0, sizeof( residual ) );
        residual[source] = INF;
        Q.push( source );
        while ( !Q.empty() ) {
            int u = Q.front();
            Q.pop();
            for( int v = 0; v <= node_num + 1; ++v ) {
	            if( !residual[v] && capacity[u][v] > flow[u][v] ) {
	                parent[v] = u;
					Q.push( v );
	                residual[v] = residual[u] < capacity[u][v] - flow[u][v] ?

residual[u] : capacity[u][v] - flow[u][v]; } } } if( residual[target] == 0 ) break; for( int u = target; u != source; u = parent[u] ) { flow[parent[u]][u] += residual[target]; flow[u][parent[u]] -= residual[target]; } max_flow += residual[target]; } } int main() { int pighouses_num, customers_num; int pighouses_capacity[1010]; int visited_by_somebody[1010]; while( cin >> pighouses_num >> customers_num ) { memset( capacity, 0, sizeof( capacity ) ); memset( visited_by_somebody, 0, sizeof( visited_by_somebody ) ); int source = 0, target = customers_num + 1; node_num = customers_num; for( int i = 1; i <= pighouses_num; ++i ) cin >> pighouses_capacity[i]; for( int i = 1; i <= customers_num; ++i ) { int keys_num; cin >> keys_num; for( int j = 0; j < keys_num; ++j ) { int temp; cin >> temp; if( visited_by_somebody[temp] == 0 ) capacity[source][i] += pighouses_capacity[temp]; else capacity[visited_by_somebody[temp]][i] = INF; visited_by_somebody[temp] = i; } cin >> capacity[i][target]; } edmond_karp( source, target ); cout << max_flow << endl; } return 0; }



转载于:https://www.cnblogs.com/ljbguanli/p/7116659.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值