关闭

UVA 10779 【maximum flow】

标签: uvamaximum flow
136人阅读 评论(0) 收藏 举报
分类:

Thought :

We consider Bob as source ,  then according to what bob has, we add_edge between Bob and stickers with weight number[sticker]. When it comes to his friends, since they will keep at least one sticker for each kind they have originally, if(number[sticker] > 1) we add_edge from friendA to certain sticker with weight number[sticker]-1. When friendA don't have certain sticker,add_edge from sticker that he doesn't have to him with weight 1to make sure he can only get one sticker of this kind.

At last, for each sticker, add_edge from sticker to t(sink)(which has been pre-defined) with weight 1.(to get maximum number of kinds of stickers).

CALCULATE MAXIMUM FLOW AND THAT'S THE ANSWER.


AC code:

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#define bob 0

#define t 99
#define MIN(a,b) ((a)<(b)?(a):(b))
using namespace std;


int f[1000];
struct edge{
	int v, nxt, w;
};
edge E[1000];
int total_edge;
int head[100];
void add_edge(int u, int v, int w)
{
	E[total_edge].v = v;
	E[total_edge].w = w;
	E[total_edge].nxt = head[u];
	head[u] = total_edge++;
}
int d[100];
bool bfs(int S, int T)
{
	int u, v;
	memset(d, -1, sizeof(d));
	queue<int> q;
	d[S] = 0;
	q.push(S);
	while (!q.empty())
	{
		u = q.front();
		q.pop();
		for (int e = head[u]; e != -1; e = E[e].nxt)
		{
			v = E[e].v;
			if (d[v] == -1 && E[e].w > f[e])
			{
				d[v] = d[u] + 1;
				q.push(v);
			}
		}
	}
	return d[T] >= 0;
}
int dinic(int u, int T, int sum)
{
	if (u == T) return sum;
	int v, tp = sum;
	for (int e = head[u]; e != -1; e = E[e].nxt)
	{
		v = E[e].v;
		if (d[v] == d[u] + 1 && E[e].w > f[e])
		{
			int canflow = dinic(v, T, MIN(sum, E[e].w - f[e]));
			f[e] += canflow;
			f[e ^ 1] -= canflow;
			sum -= canflow;
		}
		
	}
	return tp - sum;
}
int maxflow(int S,int T)
{
	int ans = 0;
	while (bfs(S, T))
	{
		ans += dinic(S, T, 1000000);
	}
	return ans;
	
}
int ppl_sticker[11][27];
int main()
{
	//freopen("in.txt", "r", stdin);
	int n, T, m;
	cin >> T;
	for (int i = 1; i <= T; i++)
	{
		memset(ppl_sticker, 0, sizeof(ppl_sticker));
		memset(head, -1, sizeof(head));
		memset(f, 0, sizeof(f));
		total_edge = 0;
		scanf("%d%d", &n, &m);
		for (int j = 1; j <= n; j++)
		{
			int temp;
			cin >> ppl_sticker[j][0];
			for (int k = 1; k <= ppl_sticker[j][0]; k++)
			{
				cin >> temp;
				ppl_sticker[j][temp]++;
			}
		}
		for (int j = 1; j <= m; j++)
		{
			if (ppl_sticker[1][j])
			{
				add_edge(1, j + 1, ppl_sticker[1][j]);
				add_edge(j + 1, 1, 0);
			}
		}
		for (int j = 2; j <= n; j++)
		{
			for (int k = 1; k <= m; k++)
			{
				if (ppl_sticker[j][k] > 1)
				{
					add_edge(j+m, k+1, ppl_sticker[j][k] - 1);
					add_edge(k + 1, j+m, 0);
				}
				else if (ppl_sticker[j][k] == 0)
				{
					add_edge(k + 1, j + m, 1);
					add_edge(j + m, k+1, 0);
				}
			}

		}
		for (int j = 1; j <= m; j++)
		{
			add_edge(j + 1, t, 1);

			add_edge(t, j+1, 0);
		}
		int ans = maxflow(1, t);
		printf("Case #%d: %d\n", i, ans);
	}
	return 0;

}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2865次
    • 积分:272
    • 等级:
    • 排名:千里之外
    • 原创:25篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条