HDU 3879 Base Station 最大权闭包 2011 Multi-University Training Contest 5 - Host by BNU

/*
建立点到汇点的边,边权的点值
抽象边为点(边点),建立源点到边点的边,边权为原的边权
建立边点到其原先连的点的两条边,边权无穷,
求最大流。答案为所有原边权和减去最大流
*/
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <queue>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <string>

using namespace std;
const int maxn=62000;
const int inf=2000000000;

//最大流模版
//************************************************************************
struct edge  
{   
    int v, next;   
    int val;   
} net[ 500010 ];   
  
int level[maxn], Qu[maxn], out[maxn],next[maxn];  
class Dinic {   
public:   
    int end;  
    Dinic() {   
        end = 0;   
        memset( next, -1, sizeof(next) );   
    }   
    inline void insert( int x, int y, int c) {   
        net[end].v = y, net[end].val = c,  
        net[end].next = next[x],   
        next[x] = end ++;   
        net[end].v = x, net[end].val = 0,  
        net[end].next = next[y],   
        next[y] = end ++;   
    }   
    bool BFS( int S, int E ) {   
        memset( level, -1, sizeof(level) );   
        int low = 0, high = 1;   
        Qu[0] = S, level[S] = 0;   
        for( ; low < high; ) {   
            int x = Qu[low];   
            for( int i = next[x]; i != -1; i = net[i].next ) {   
                if( net[i].val == 0 ) continue;   
                int y = net[i].v;   
                if( level[y] == -1 ) {   
                    level[y] = level[x] + 1;   
                    Qu[ high ++] = y;   
                }   
            }   
            low ++;   
        }   
        return level[E] != -1;   
    }    
    
    int MaxFlow( int S, int E ){   
        int maxflow = 0;   
        for( ; BFS(S, E) ; ) {   
            memcpy( out, next, sizeof(out) );   
            int now = -1;   
            for( ;; ) {   
                if( now < 0 ) {   
                    int cur = out[S];   
                    for(; cur != -1 ; cur = net[cur].next )    
                        if( net[cur].val && out[net[cur].v] != -1 && level[net[cur].v] == 1 )   
                            break;   
                    if( cur >= 0 ) Qu[ ++now ] = cur, out[S] = net[cur].next;   
                    else break;   
                }   
                int u = net[ Qu[now] ].v;   
                if( u == E ) {   
                    int flow = inf;   
                    int index = -1;   
                    for( int i = 0; i <= now; i ++ ) {   
                        if( flow > net[ Qu[i] ].val )   
                            flow = net[ Qu[i] ].val, index = i;   
                    }   
                    maxflow += flow;   
                    for( int i = 0; i <= now; i ++ )   
                        net[Qu[i]].val -= flow, net[Qu[i]^1].val += flow;   
                    for( int i = 0; i <= now; i ++ ) {   
                        if( net[ Qu[i] ].val == 0 ) {   
                            now = index - 1;   
                            break;   
                        }   
                    }   
                }   
                else{   
                    int cur = out[u];   
                    for(; cur != -1; cur = net[cur].next )    
                        if (net[cur].val && out[net[cur].v] != -1 && level[u] + 1 == level[net[cur].v])   
                            break;   
                    if( cur != -1 )   
                        Qu[++ now] = cur, out[u] = net[cur].next;   
                    else out[u] = -1, now --;   
                }   
            }   
        }   
        return maxflow;   
    }   
};   
//************************************************************************

int n,m,tmp;
int main()
{
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		Dinic my;
		int sum=0;
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&tmp);
			my.insert(i,0,tmp);//0为汇点
		}
		int a,b,c;
		for(int i=1;i<=m;i++)
		{
			scanf("%d%d%d",&a,&b,&c);
			my.insert(i+n,b,inf);
			my.insert(i+n,a,inf);
			my.insert(n+m+1,i+n,c);//n+m+1为源点
			sum+=c;
		}
		printf("%d\n",sum-my.MaxFlow(m+n+1,0));
	}
}
  


你可能感兴趣的:(HDU 3879 Base Station 最大权闭包 2011 Multi-University Training Contest 5 - Host by BNU)