关闭

【网络流】 HDOJ 4289 Control

标签: 网络流
235人阅读 评论(0) 收藏 举报
分类:

就是求全图的点割集。。。。先拆点,那么就变成一般的割集了。。。然后求最大流就行了。。。。

#include <iostream>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <climits>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 505
#define maxm 100005
#define eps 1e-7
#define mod 1000000007
#define INF 0x3f3f3f3f
#define PI (acos(-1.0))
#define lowbit(x) (x&(-x))
#define mp make_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid 
#define rson o<<1 | 1, mid+1, R
#define pii pair<int, int>
#pragma comment(linker, "/STACK:16777216")
typedef long long LL;
typedef unsigned long long ULL;
//typedef int LL; 
using namespace std;
LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
// head

struct Edge
{
	int v, c, next;
	Edge(int v = 0, int c = 0, int next = 0) : v(v), c(c), next(next) {}
}E[maxm];

queue<int> q;
int cntE, H[maxn];
int dis[maxn];
int cnt[maxn];
int cur[maxn];
int pre[maxn];
int nv, s, t, flow, n, m;

void addedges(int u, int v, int c)
{
	E[cntE] = Edge(v, c, H[u]);
	H[u] = cntE++;
	E[cntE] = Edge(u, 0, H[v]);
	H[v] = cntE++;
}

void bfs()
{
	memset(cnt, 0, sizeof cnt);
	memset(dis, -1, sizeof dis);
	q.push(t);
	dis[t] = 0, cnt[0]++;
	while(!q.empty()) {
		int u = q.front();
		q.pop();
		for(int e = H[u]; ~e; e = E[e].next) {
			int v = E[e].v;
			if(dis[v] == -1) {
				dis[v] = dis[u] + 1;
				cnt[dis[v]]++;
				q.push(v);
			}
		}
	}
}

int isap()
{
	memcpy(cur, H, sizeof H);
	flow = 0;
	bfs();
	int e, f, u = pre[s] = s, minv, pos;
	while(dis[s] < nv) {
		if(u == t) {
			f = INF;
			for(int i = s; i != t; i = E[cur[i]].v) if(E[cur[i]].c < f) {
				f = E[cur[i]].c;
				pos = i;
			}
			for(int i = s; i != t; i = E[cur[i]].v) {
				E[cur[i]].c -= f;
				E[cur[i] ^ 1].c += f;
			}
			flow += f;
			u = pos;
		}
		for(e = H[u]; ~e; e = E[e].next) if(E[e].c && dis[E[e].v] + 1 == dis[u]) break;
		if(~e) {
			cur[u] = e;
			pre[E[e].v] = u;
			u = E[e].v;
		}
		else {
			if(--cnt[dis[u]] == 0) break;
			for(e = H[u], minv = nv; ~e; e = E[e].next) if(E[e].c && minv > dis[E[e].v]) {
				minv = dis[E[e].v];
				cur[u] = e;
			}
			dis[u] = minv + 1;
			cnt[dis[u]]++;
			u = pre[u];
		}
	}
	return flow;
}

void init()
{
	cntE = 0;
	memset(H, -1, sizeof H);
}

void work()
{
	int u, v, x, S, T;
	s = 0, t = 2 * n + 1, nv = t + 1;
	scanf("%d%d", &S, &T);
	addedges(s, S, INF);
	addedges(T + n, t, INF);
	for(int i = 1; i <= n; i++) {
		scanf("%d", &x);
		addedges(i, i + n, x);
	}
	for(int i = 1; i <= m; i++) {
		scanf("%d%d", &u, &v);
		addedges(u + n, v, INF);
		addedges(v + n, u, INF);
	}
	printf("%d\n", isap());
}

int main()
{
	while(scanf("%d%d", &n, &m)!=EOF) {
		init();
		work();
	}

	return 0;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:195642次
    • 积分:7503
    • 等级:
    • 排名:第2976名
    • 原创:587篇
    • 转载:9篇
    • 译文:0篇
    • 评论:17条
    最新评论