POJ-3436 ACM Computer Factory (Dinic或Edmonds_Karp)

1.编码

用一个int表示电脑的一种状态,其中最低的二进制位表示第一个部件的状态,最高的二进制位表示最后一个部件的状态,1代表存在,0代表不存在

2.建图

图中的点包括电脑所有的状态,共有2^p个点;和机器,共n个点。由题意可知,一台机器可以加工多种不同状态的电脑,但是产生的电脑的状态可以唯一确定。加边过程为:对于每个机器。将它与它产生的电脑状态点之间建立一条容量为机器的performance的边;对于每个状态点,将它与它满足条件的机器之间建立一条边,容量为performance或者inf都可以无所谓了.........

3.算法

求最大流即可

dinic算法或者EK都可以,本题中C++编译器Ek快一些,G++ Dnic快一些

4.输出

本题还要求输出机器之间的流量,我采用的直观但是低效方法如下:

在执行算法时把进入每台机器的流量记录下来

建立一个从pair 到int的map,用来储存first到secon的流量

前面已经提到过,每台机器生产出的产品是固定的一个,所以对于每台机器,找到它的产品,然后遍历这个产品的边,如果流量大于0,就存到map里。直到这台机器的流量用完,需要这么做是因为有可能出现多台机器产出同样的产品的情况,需要避免重复

C++ EK 算法 0ms hhhh

#include
#include
#include
#include
#include
#include
#define maxs 1100
#define inf 0x3f3f3f3f
using namespace std;
struct mach
{
	int bef[11], perf,nxt,in;
} mac[52];
struct Edge
{
	int from, to, cap, flow;
	Edge() {}
	Edge(int u,int v, int cap) :from(u),to(v), cap(cap), flow(0){}
} ;
vector edge;
vector g[maxs];
int p, n,st,vnum;
bool isSatisfied(int x, int* bef)
{
	for (int i = 0; i < p; i++)
		if ((bef[i] == 0 && (x&(1 << i))) || (bef[i] == 1 && !(x&(1 << i))))
			return false;
	return true;
}
void addEdge(int u, int v, int c)
{
	edge.push_back(Edge(u,v, c));
	edge.push_back(Edge(v,u, 0));
	int m = edge.size();
	g[u].push_back(m - 2);
	g[v].push_back(m - 1);
}
void build()
{
	for (int i = 0; i < n; i++)
	{
		mac[i].in = 0;
		addEdge(st + i, mac[i].nxt, mac[i].perf);
	}
	for (int i = 0; i < st; i++)
		for (int j = 0; j < n; j++)
			if (isSatisfied(i, mac[j].bef))
				addEdge(i, st + j,inf);
}
int a[maxs], fa[maxs];
int ek()
{
	int flow = 0;
	while (true)
	{
		queue q;
		q.push(0);
		memset(a, 0, sizeof(int)*vnum);
		a[0] = inf;
		while (!q.empty())
		{
			int x = q.front(); q.pop();
			for (int i = 0; i < g[x].size(); i++)
			{
				Edge& e = edge[g[x][i]];
				if (!a[e.to] && e.cap > e.flow)
				{
					a[e.to] = min(e.cap - e.flow, a[x]);
					fa[e.to] = g[x][i];
					q.push(e.to);
				}
			}
			if (a[st - 1]) break;
		}
		if (!a[st - 1]) break;
		for (int u = st-1; u; u = edge[fa[u]].from)
		{
			if (u >= st) mac[u - st].in += a[st - 1];
			edge[fa[u]].flow += a[st - 1];
			edge[fa[u] ^ 1].flow -= a[st - 1];
		}
		flow += a[st - 1];
	}
	return flow;
}
void display()
{
	map, int> mp;
	for (int i = 0; i < n; i++)
	{
		int tem = mac[i].nxt;
		for (int j = 0; j < g[tem].size(); j++)
		{
			Edge& e = edge[g[tem][j]];
			if (e.flow>0&&mac[i].in>0)
			{
				pair t(i+1, e.to - st+1);
				int fnum = min(mac[i].in, e.flow);
				if (mp.count(t)) mp[t] += fnum;
				else mp[t] = fnum;
				mac[i].in -= fnum;
			}
		}
	}
	printf("%d\n", mp.size());
	for (map,int>::iterator  it = mp.begin(); it != mp.end(); it++)
		printf("%d %d %d\n", (it->first).first, (it->first).second, it->second);
}
int main()
{
	scanf("%d%d", &p, &n);
	st = 1 << p;
	vnum = st +  n;
	int temp[11];
	for (int i=0;i= 0;j--) m.nxt = m.nxt * 2 + temp[j];
	}
	build();
	printf("%d ", ek());
	display();
	return 0;
}

Dinic

#include
#include
#include
#include
#include
#include
#define maxs 1100
#define inf 0x3f3f3f3f
using namespace std;
struct mach
{
	int bef[11], perf,nxt,in;
} mac[52];
struct Edge
{
	int to, cap, flow;
	Edge() {}
	Edge(int v, int cap) :to(v), cap(cap), flow(0){}
} ;
vector edge;
vector g[maxs];
int p, n,st,vnum;
bool isSatisfied(int x, int* bef)
{
	for (int i = 0; i < p; i++)
		if ((bef[i] == 0 && (x&(1 << i))) || (bef[i] == 1 && !(x&(1 << i))))
			return false;
	return true;
}
void addEdge(int u, int v, int c)
{
	edge.push_back(Edge(v, c));
	edge.push_back(Edge(u, 0));
	int m = edge.size();
	g[u].push_back(m - 2);
	g[v].push_back(m - 1);
}
void build()
{
	for (int i = 0; i < n; i++)
	{
		mac[i].in = 0;
		addEdge(st + i, mac[i].nxt, mac[i].perf);
	}
	for (int i = 0; i < st; i++)
		for (int j = 0; j < n; j++)
			if (isSatisfied(i, mac[j].bef))
				addEdge(i, st + j,inf);
}
int cur[maxs], d[maxs];
bool vis[maxs];
bool bfs()
{
	queue q;
	q.push(0);
	memset(vis, 0, sizeof(bool)*vnum);
	d[0] = 0;
	vis[0] = true;
	while (!q.empty())
	{
		int x = q.front(); q.pop();
		for (int i = 0; i < g[x].size(); i++)
		{
			Edge& e = edge[g[x][i]];
			if (!vis[e.to] && e.cap > e.flow)
			{
				vis[e.to] = true;
				d[e.to] = d[x] + 1;
				q.push(e.to);
			}
		}
	}
	return vis[st - 1];
}
int dfs(int x,int a)
{
	if (x == st - 1 || a == 0) return a;
	int flow = 0, f;
	for (int& i = cur[x]; i < g[x].size(); i++)
	{
		Edge& e = edge[g[x][i]];
		if (d[e.to] == d[x] + 1 && (f = dfs(e.to, min(a, e.cap - e.flow))) > 0)
		{
			if (e.to >= st) mac[e.to - st].in += f;
			e.flow += f;
			edge[g[x][i] ^ 1].flow -= f;
			a -= f;
			flow += f;
			if (!a) break;
		}
	}
	return flow;
}
int dinic()
{
	int flow = 0;
	while (bfs())
	{
		memset(cur, 0, sizeof(int)*vnum);
		flow += dfs(0, inf);
	}
	return flow;
}
void display()
{
	map, int> mp;
	for (int i = 0; i < n; i++)
	{
		int tem = mac[i].nxt;
		for (int j = 0; j < g[tem].size(); j++)
		{
			Edge& e = edge[g[tem][j]];
			if (e.flow>0&&mac[i].in>0)
			{
				pair t(i+1, e.to - st+1);
				int fnum = min(mac[i].in, e.flow);
				if (mp.count(t)) mp[t] += fnum;
				else mp[t] = fnum;
				mac[i].in -= fnum;
			}
		}
	}
	printf("%d\n", mp.size());
	for (map,int>::iterator  it = mp.begin(); it != mp.end(); it++)
		printf("%d %d %d\n", (it->first).first, (it->first).second, it->second);
}
int main()
{
	scanf("%d%d", &p, &n);
	st = 1 << p;
	vnum = st +  n;
	int temp[11];
	for (int i=0;i= 0;j--) m.nxt = m.nxt * 2 + temp[j];
	}
	build();
	printf("%d ", dinic());
	display();
	return 0;
}

 

你可能感兴趣的:(网络流)