6
9
2010
1

最小费用最大流模板(SPFA,慢)

 

int n, K;
const int MAXN = 64*64*2;
const int MAXM = MAXN*8;
const int SIZE_QUEUE = 13;
int mat[64][64];
int w[MAXN];
int cost[MAXM],cap[MAXM];
int nxt[MAXM], name[MAXM];
int st[MAXN];
int nedges;
int flow[MAXM];
int sp[MAXN], from[MAXN];
int oo = 0x7f7f7f7f;

//循环队列
struct CicleQueue{
	int _data[two(SIZE_QUEUE)];
	int& operator[](int a){
		return _data[a&two(SIZE_QUEUE)-1];
	}
};

CicleQueue Q;
bool spfa(int src, int dst, int n){
	bitset<MAXN> mk;
	mk.reset();
	memset(sp,0x3f,(sizeof sp));
	memset(from,-1,(sizeof from));
	
	mk[src] = true;
	sp[src] = 0;

	int qs = 0, qe = 1;
	Q[qs] = src;
	for(;qs != qe;){
		int cur = Q[qs++];
		for(int i = st[cur]; i != -1; i = nxt[i] )if( flow[i] ){
			int son = name[i];
			int better = sp[cur] + cost[i];
			if( better < sp[son] ){
				sp[son] = better;
				from[son] = i;
				if( !mk[ son ] ){
					mk[son] = true;
					Q[qe++] = son;
				}
			}
		}
		mk[cur] = false;
	}
        //判断是否找到了一条最短路
	return from[dst] != -1;
}

int min_cost_max_flow(int src, int dst, int n){
	int maxflow = 0, mincost = 0;
	for(;;){
		if( !spfa(src, dst, n)){
			return mincost ;
		}
		int aug = oo;
		for(int i = from[dst]; i != -1; i = from[name[i^1]] ){
			checkMin( aug, flow[i] );
		}
		if( !aug ) return mincost;

		maxflow += aug;
		mincost += sp[dst] * aug;
		for(int i = from[dst]; i != -1; i = from[name[i^1]] ){
			flow[i] -= aug;
			flow[i^1] += aug;
		}
	}
	return mincost;
}

void init_edge(){
	memset(nxt,-1,(sizeof nxt));
	memset(st,-1,(sizeof st));
	
	nedges = 0;
}
void add_edge(int a, int b, int w, int c){
	cap[nedges] = w;
	cost[nedges] = c;
	name[nedges] = b;
	nxt[nedges] = st[a];
	st[a] = nedges;
	nedges++;
}

Category: 未分类 | Tags: | Read Count: 2420
Avatar_small
pavzi.com 说:
2024年1月24日 18:00

Pavzi.com provides all the news about Gadgets, the Economy, Technology, Business, Finance and many more. The main concept or our aim behind this website has been the will to provide resources with full information on each topic which can be accessed through the Internet. To ensure that every reader gets what is important and worthy about the topic they search and link to hear from us. pavzi.com Our site is a multiple Niche or category website which will ensure to provide information and resources on each and every topic. Some of the evergreen topics you will see on our website are Career, Job Recruitment, Educational, Technology, Reviews and others. We are targeting mostly so it is true that Tech, Finance, and Product Reviews. The only reason we have started this website is to make this site the need for your daily search use.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com