hdu 3987 Harry Potter and the Forbidden Forest

求最小割集的最少边数。

很神的方法 http://hi.baidu.com/mengyun1993/blog/item/c30d193c9a85932870cf6cda.html

用的党的模板。

#include <set>
#include <map>
#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <limits.h>
#include <string.h>
#include <string>
#include <algorithm>
#define MID(x,y) ( ( x + y ) >> 1 )
#define L(x) ( x << 1 )
#define R(x) ( x << 1 | 1 )
#define FOR(i,s,t) for(int i=s; i<t; i++)
#define BUG puts("here!!!")
const long long  INF = 2100000000000560ll;
using namespace std;
class Arc{
public:
	int to;
    long long w;
	Arc *next,*anti;
	Arc *ass( int tt,long long ww,Arc* &b ){
		to = tt;w = ww;next = b;b = this;return this;
	}
};
template<class Tp,int Vsize, int Esize >
class Network{
private:
    Arc A[ 2*Esize+10 ],*biao[ Vsize ];
    int countt,d[ Vsize ],S,T,N;
    int bfs( void ){
    	queue<int> q;
    	fill( d,d+Vsize,-1 );
    	d[ S ] = 0;
    	q.push( S );
    	while( !q.empty() ){
    		int u = q.front();q.pop();
    		for( Arc *p=biao[u]; p != NULL; p=p->next )
    			if( d[p->to] == -1 && p->w > 0 )
    				d[p->to] = d[u]+1,q.push(p->to);
    	}
    	return d[T] != -1;
    }
    Tp dinic( int u,Tp sum ){
    	Tp f,o;
    	if( u == T ) return sum;
    	o = sum;
    	for( Arc *p=biao[u]; p != NULL && sum; p=p->next )
    		if( d[p->to] == d[u]+1 && p->w > 0 ){
    			f = dinic(p->to,min(p->w,sum));
    			p->w -= f;p->anti->w += f;
    			sum -= f;
    		}
    	return o-sum;
    }
public:
    Network( void ) {}
    Network( int n ) { init(n) ;}
    void init( int n ){
        countt = 0;N = n;
        for( int i = 0; i <= n; i++ )
            biao[i] = NULL;
    }
    void add( int from,int to,Tp w ){
    	Arc *p1 = A[countt++].ass( to,w ,biao[from]);
    	Arc *p2 = A[countt++].ass( from,0,biao[ to] );
    	p1->anti = p2; p2->anti = p1;
    }
    Tp MaxFlowDinic( int s, int t ){
        S = s;T = t;
        Tp total = 0;
        while( bfs() )
            total += dinic( S,INF );
        return total;
    }
};

Network<long long, 1010, 400010> net;
int main()
{
	int n, m, ncases;
	int u, v, cost, dir;
	int ind = 1;
	
	scanf("%d", &ncases);
	
	while( ncases-- )
	{
		scanf("%d%d", &n, &m);
		net.init(n);
		FOR(i, 0, m)
		{
			scanf("%d%d%d%d", &u, &v, &cost, &dir);
			net.add(u, v, cost*1ll*(m+1)+1);
			if( dir)
				net.add(v, u, cost*1ll*(m+1)+1);
		}
		long long ans = net.MaxFlowDinic(0, n-1);
		
		printf("Case %d: %I64d\n", ind++, ans%(m+1));
	}
	
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值