# UVA 10779 【maximum flow】

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;
void add_edge(int u, int v, int w)
{
E[total_edge].v = v;
E[total_edge].w = w;
}
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(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])
{
}
}
for (int j = 2; j <= n; j++)
{
for (int k = 1; k <= m; k++)
{
if (ppl_sticker[j][k] > 1)
{
}
else if (ppl_sticker[j][k] == 0)
{
add_edge(k + 1, j + m, 1);
}
}

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

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

}

• 本文已收录于以下专栏：

举报原因： 您举报文章：UVA 10779 【maximum flow】 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)