UVA 10779 【maximum flow】

原创 2016年05月31日 23:07:18

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;

}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

UVA 10779 - Collectors Problem(网络流)

题目链接:点击打开链接 思路: 我们以1~m建立一列结点,表示Bob的物品, 以2~n建立一列结点, 表示其他的人。 源点和1~m相连, 容量为Bob的初始数量; 汇点也和1~m相连, 容量为1,...

UVA 10779 Collectors Problem(最大流)

题意:有T(T≤20)组数据。Bob在与他的n−1(2≤n≤10)个同伴交换糖纸,一共有m(5≤m≤25)种糖纸。Bob希望能和同伴交换使得手上的糖纸数尽量多。他的同伴只会用手上的重复的交换手上没有的...

UVA10779 Collectors Problem(最大流)

白皮书上的练习题0 0 大意:Bob在与他的n−1(2≤n≤10)n-1(2 \leq n \leq 10)个朋友交换糖纸,一共有m(5≤m≤25)m(5 \leq m \leq 25)种糖纸。每个...

【SPOJ-FASTFLOW】Fast Maximum Flow【最大流】

虽然数据很大,但是dinic可过。 注意开LL。 #include #include using namespace std; typedef long long LL; ...

UVA - 10594 Data Flow (最小费用最大流)

题目大意: 在无向图G中把D单位的数据从1号点传到N号点,每条边允许传输数据的最大容量均为K,给出每条边传输单位数据的费用,求最小传输D单位数据的费用。 解析: 注意是无向图,必然要用邻接表...

UVa10594 Data Flow

题意:网络中传送数据量为D的数据,每条边容量为K,

uva 10594 Data Flow

uva 10594 Data Flow

UVa #10366 Faucet Flow (习题8-24)

#define UVa "8-24.10366.cpp" //Faucet Flow char fileIn[30] = UVa, fileOut[30] = UVa; #include #in...

uva 10594 - Data Flow(最小费用最大流)

Problem F Data Flow Time Limit 5 Seconds   In the latest Lab of IIUC, i...

UVa - 10366 - Faucet Flow

考虑细节比较多,直接上代码,WA了三次,好伤,最近几天做题有些慢。 AC代码: #include #include #include #include #include #include...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)