POJ 2987 Firing (最大权闭合子图Dinic)

题意:公司打算裁员,裁掉某些员工可以获得正收益,而裁掉某些员工会遭受损失。并且员工之间往往存在一定的关系,当某个员工被裁掉之后,在他的关系之下的所有员工都必须被裁掉。现在要求如何裁员才能获得最大收益。
题解:s->正权, 负权->t。  ans = 正权和 - maxflow, 或者 ans = 正权和 - 没有被裁的正权和 - abs(被裁的负权和) ( 正边权进入最小割表示该人没被炒,非正边权进入最小割表示该人被炒)。至于边权放大什么的就不是很理解了····

#include <algorithm>
#include <iostream>
using namespace std;

#define MAX 40000
#define INF 99999999999999999
#define min(a,b) (a<b?a:b)

struct Edge
{
	int st, ed;
	int next;
	__int64 flow;
} edge[MAX*10];

int head[MAX], out[MAX];
int stk[MAX], que[MAX];
int leve[MAX];
bool vis[MAX];
int E, src, sink;

void add_edge ( int u, int v, __int64 flw )
{
	edge[E].st = u;
	edge[E].ed = v;
	edge[E].flow = flw;
	edge[E].next = head[u];
	head[u] = E++;

	edge[E].st = v;
	edge[E].ed = u;
	edge[E].flow = 0;
	edge[E].next = head[v];
	head[v] = E++;
}

bool dinic_bfs()
{
	memset(leve,-1,sizeof(leve));
	int front, rear, u, v, i;
	front = rear = 0;
	que[rear++] = src;
	leve[src] = 0;
	while ( front != rear )
	{
		u = que[front];
		front = ( front + 1 ) % MAX;
		for ( i = head[u]; i != -1; i = edge[i].next )
		{
			v = edge[i].ed;
			if ( edge[i].flow > 0 && leve[v] == -1 )
			{
				leve[v] = leve[u] + 1;
				que[rear] = v;
				rear = ( rear + 1 ) % MAX;
			}
		}
	}
	return leve[sink] != -1;
}

__int64 Dinic ()
{
	__int64 maxFlow = 0;
	while ( dinic_bfs() )
	{
		int top = 0, u = src, i;
		for ( i = src; i <= sink; i++ ) out[i] = head[i];
	
		while ( out[src] != -1 )
		{
			if ( u == sink )
			{
				__int64 dd = INF;
				for ( i = top - 1; i >= 0; i-- )
					dd = min ( edge[stk[i]].flow, dd );
				for ( i = top - 1; i >= 0; i-- )
				{
					edge[stk[i]].flow -= dd;
					edge[stk[i]^1].flow += dd;
					if ( edge[stk[i]].flow == 0 ) top = i;
				}
				maxFlow += dd;
				u = edge[stk[top]].st;
			}
			else if ( out[u] != -1 && edge[out[u]].flow > 0 && leve[u] + 1 == leve[edge[out[u]].ed] )
			{
				stk[top++] = out[u];
				u = edge[out[u]].ed;
			}
			else
			{
				while ( top > 0 && u != src && out[u] == -1 )
					u = edge[stk[--top]].st;
				out[u] = edge[out[u]].next;
			}
		}
	}
	return maxFlow;
}


int dfs ( int u )
{
	vis[u] = true;
	int cnt = 1;
	for ( int i = head[u]; i != -1; i = edge[i].next )
		if ( ! vis[edge[i].ed] && edge[i].flow > 0 )
     		cnt += dfs(edge[i].ed);
	return cnt;
}

int main()
{
	__int64 sum = 0;
	int n, m, u, v, b;
	scanf("%d%d",&n,&m);
	E = src = 0;
	sink = n + 1;
	memset(head,-1,sizeof(head));

	for ( int i = 1; i <= n; i++ )
	{
		scanf("%d",&b);
		if ( b > 0 )
		{
			sum += b;
		    add_edge ( src, i, b );
		}
		else add_edge ( i, sink, -b );
	}
	
	while ( m-- )
	{
		scanf("%d%d",&u,&v);
		add_edge ( u, v, INF );
	}

	memset(vis,0,sizeof(vis));
	__int64 maxFlow = Dinic();
	int cnt = dfs ( src );
	printf("%d %I64d\n", cnt - 1, sum - maxFlow );
	return 0;
}


#include <algorithm>
#include <iostream>
using namespace std;

#define MAX 60000
#define INF 9999999999999
#define min(a,b) ((a)<(b)?(a):(b))

struct Edge
{
	int st, ed;
	int flow;
	int next;
} edge[MAX*3];

int head[MAX], out[MAX];
int stk[MAX], que[MAX];
int leve[MAX], vis[MAX];
int E, src, sink;

void add_edge ( int u, int v, __int64 flw )
{
	edge[E].st = u;
	edge[E].ed = v;
	edge[E].flow = flw;
	edge[E].next = head[u];
	head[u] = E++;

	edge[E].st = v;
	edge[E].ed = u;
	edge[E].flow = 0;
	edge[E].next = head[v];
	head[v] = E++;
}

__int64 Dinic()
{
    __int64 maxFlow = 0;
	int front, rear, top;
	int cur, u, i;
    while ( 1 )
    {
        for ( i = src; i <= sink; i++ ) leve[i] = -1;
        front = rear = leve[src] = 0;
        que[rear++] = src;

        while ( front != rear )
        {
            u = que[front];
			front = ( front + 1 ) % MAX;
            for ( i = head[u]; i != -1; i = edge[i].next)
            {
                if ( edge[i].flow > 0 && leve[edge[i].ed] == -1 )
                {
                    leve[edge[i].ed] = leve[u] + 1;
                    que[rear]= edge[i].ed;
					rear = ( rear + 1 ) % MAX;
                }
            }
        }
        if ( leve[sink] == -1 ) break;
        for ( i = src; i <= sink; i++ ) out[i] = head[i];

        top = 0;
        while ( 1 )
        {
            if ( top == 0 )
            {
                for ( cur = out[src]; cur != -1; cur = edge[cur].next )
                    if ( edge[cur].flow > 0 && out[edge[cur].ed] != -1 && leve[edge[cur].ed] == 1 )
                        break;

                if ( cur < 0 ) break;
				stk[top++] = cur;
				out[src] = edge[cur].next;
               
            }
            u = edge[stk[top-1]].ed;

            if ( u == sink )
            {
                __int64 dd = INF, index = -1;
                for ( i = top - 1; i >= 0; i-- )
                {
                    if ( dd >= edge[stk[i]].flow )
                    {
                        dd = edge[stk[i]].flow;
                        index = i;
                    }
                }
				maxFlow += dd;

                for ( i = 0; i < top; i++ )
                {
                    edge[stk[i]].flow -= dd;
                    edge[stk[i]^1].flow += dd;    
                }
				top = index;
            }
            else
            {
                for( cur = out[u]; cur != -1; cur = edge[cur].next )
                    if ( edge[cur].flow && out[edge[cur].ed] != -1 && leve[u] + 1 == leve[edge[cur].ed] )
                        break;

                if ( cur != -1 )
                {
                    stk[top++] = cur;
                    out[u] = edge[cur].next;
                }
                else
                {
                    out[u] = -1;
                    top--;
                }
            }
        }
    }
    return maxFlow;
}

int dfs ( int u )
{
	vis[u] = true;
	int v, i, cnt = 1;
	for ( i = head[u]; i != -1; i = edge[i].next )
	{
		v = edge[i].ed;
		if ( ! vis[v] && edge[i].flow > 0 )
			cnt += dfs(v);
	}
	return cnt;
}

int main()
{
	__int64 sum = 0;
	int n, m, u, v, b;
	scanf("%d%d",&n,&m);

	E = src = 0;
	sink = n + 1;
	memset(head,-1,sizeof(head));

	for ( int i = 1; i <= n; i++ )
	{
		scanf("%d",&b);
		if ( b > 0 )
		{
			sum += b;
		    add_edge ( src, i, b );
		}
		else add_edge ( i, sink, -b );
	}
	
	while ( m-- )
	{
		scanf("%d%d",&u,&v);
		add_edge ( u, v, INF );
	}

	memset(vis,0,sizeof(vis));
	__int64 maxFlow = Dinic();
	int cnt = dfs ( src ) - 1;
	printf("%d %I64d\n", cnt, sum - maxFlow);
    return 0;
}





你可能感兴趣的:(POJ 2987 Firing (最大权闭合子图Dinic))