HDU 4289 Control(拆点,最大流)

/*
拆点:
将每一个节点拆为a,a1,边的权值给节点的最小消耗,如果节点a、b之间原来有边相连,则a1,b和b1,a之间连接两条边,权值为INF。
于是转换为最大流问题
新收获:我原来打算用vector存储邻接表,最后发现反向遍无法处理。终于明白了②、③处的用法,目的方便对反向遍进行运算。异或使两边对应起来
*/

//dinic实现
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int nMax = 407;
const int mMax = 200000;
const int INF = 0x3fffffff;
struct Adj
{
	int v, w;
	int next;
}adj[nMax + mMax];//②
int head[nMax];
int cnt;

int N, M, S, D;
int dis[nMax];

void addEdge(int u, int v, int w)
{
	adj[cnt].v = v;
	adj[cnt].w = w;
	adj[cnt].next = head[u];
	head[u] = cnt ++;

	adj[cnt].v = u;
	adj[cnt].w = 0;
	adj[cnt].next = head[v];
	head[v] = cnt ++;
}

int bfs(int s, int d)
{
	queue<int> que;
	que.push(s);
	memset(dis, -1, sizeof(dis));
	dis[s] = 0;
	int i;
	while(!que.empty())
	{
		int u = que.front();
		que.pop();
		for(i = head[u]; i != -1; i = adj[i].next)
		{
			int v = adj[i].v;
			if(adj[i].w && dis[v] == -1)
			{
				dis[v] = dis[u] + 1;
				que.push(v);
			}
		}
	}
	return dis[d];
}

int min(int a, int b)
{
	return a < b ? a : b;
}

int dfs(int s, int d, int cost)
{
	if(s == d) return cost;
	int t, ans = 0;
	int i;
	for(i = head[s]; i != -1; i = adj[i].next)
	{
		int v = adj[i].v;
		if(dis[v] == dis[s] + 1 && adj[i].w && (t = dfs(v, d, min(adj[i].w, cost))) != -1)
			//①原来这里漏写了adj[i].w != 0,结果超时
		{
			adj[i].w -= t;
			adj[i ^ 1].w += t;//③
			cost -= t;
			ans += t;
			if(!cost) break;
		}
	}
	return ans;
}

int dinic(int s, int d)
{
	int ans = 0;
	while(bfs(s, d) != -1)
	{
		ans += dfs(s, d, INF);
	}
	return ans;
}

int main()
{
	//freopen("e://data.in", "r",stdin);
	while(scanf("%d%d%d%d", &N, &M, &S, &D) != EOF)
	{
		int i;
		int a, b;
		memset(head, -1, sizeof(head));
		cnt = 0;
		for(i = 1; i <= N; ++ i)
		{
			scanf("%d", &a);
			addEdge(i, i + N, a);
		}
		for(i = 1; i <= M; ++ i)
		{
			scanf("%d%d", &a, &b);
			addEdge(a + N, b, INF);
			addEdge(b + N, a, INF);
		}
		int ans = dinic(S, D + N);
		printf("%d\n", ans);
	}
	return 0;
}


//isap实现

#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int nMax = 407;
const int mMax = 200000;
const int INF = 0x3fffffff;
struct Adj
{
	int v, w;
	int next;
}adj[nMax + mMax];
int head[nMax];
int cnt;
int num[nMax];

int N, M, S, D;
int dis[nMax];
int NN;

void addEdge(int u, int v, int w)
{
	adj[cnt].v = v;
	adj[cnt].w = w;
	adj[cnt].next = head[u];
	head[u] = cnt ++;

	adj[cnt].v = u;
	adj[cnt].w = 0;
	adj[cnt].next = head[v];
	head[v] = cnt ++;
}

int min(int a, int b)
{
	return a < b ? a : b;
}

int dfs(int u, int s, int d, int cost)
{
	if(u == d) return cost;
	int i;
	int ans = 0;
	int _min = NN;
	for(i = head[u]; i != -1; i = adj[i].next)
	{
		int v = adj[i].v;
		if(adj[i].w)
		{
			if(dis[v] + 1 == dis[u])
			{
				int t = dfs(v, s, d, min(adj[i].w, cost));
				adj[i].w -= t;
				adj[i ^ 1].w += t;
				cost -= t;
				ans += t;
				if(dis[s] == NN) return ans;
				if(!cost) break;
			}
			if(_min > dis[v])
				_min = dis[v];
		}
	}
	if(!ans)
	{
		if(-- num[dis[u]] == 0) dis[s] = NN;
		dis[u] = _min + 1;
		++ num[dis[u]];
	}
	return ans;
}

int isap(int s, int d)
{
	memset(dis, 0, sizeof(dis));
	memset(num, 0, sizeof(num));
	num[0] = NN;
	int ans = 0;
	while(dis[s] < NN)
		ans += dfs(s, s, d, INF);
	return ans;
}

int main()
{
	//freopen("e://data.in", "r",stdin);
	while(scanf("%d%d%d%d", &N, &M, &S, &D) != EOF)
	{
		NN = 2 * N;
		int i;
		int a, b;
		memset(head, -1, sizeof(head));
		cnt = 0;
		for(i = 1; i <= N; ++ i)
		{
			scanf("%d", &a);
			addEdge(i, i + N, a);
		}
		for(i = 1; i <= M; ++ i)
		{
			scanf("%d%d", &a, &b);
			addEdge(a + N, b, INF);
			addEdge(b + N, a, INF);
		}
		int ans = isap(S, D + N);
		printf("%d\n", ans);
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值