---
id: 1646
lang: en
translation_of: index.md
source_sha256: 51bb02422d295eb5d6859e91bec9f2ca51e3c4f02940c7b4f98a7716e0c3b28f
title: "[Fortuna OJ] 4682 [GDOI2017 Mock 8.11] Biologist"
description: "The answer is the total weight minus a minimum cut over sex-change costs, forgone sponsorships and tea-drinking expenses."
tags: ["OI","Network Flow","Minimum Cut","Maximum Flow"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "雄性连入雄性 (literally “males connect into males”) kept as written, although the code connects males to the sink."
  - "喝茶费用 rendered literally as “tea-drinking expenses”; it refers to the extra cost g paid when a friend's wish is not met."
---

## Main Idea

The original solution author's approach to this problem is very clear. Let me lay it out.

First, think about what the answer means: it must be the total sum of the weights minus:

- the sex-change costs
- the sponsorship money we give up
- the tea-drinking expenses

We can build a network flow out of the three elements above, and compute the minimum cut to make the answer as large as possible.

Consider connecting the source into the females, and the males into the males, with the capacity being the sex-change cost: if such an edge is cut, that means no sex change is needed. Consider the edges for the friends: if one leans toward female, connect the source into it, and add edges from it to every corresponding index; if male, connect it into the sink, and add edges from every corresponding index to that friend.

<!--more-->

## Code

```cpp
// FOJ4682.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 1e5 + 200, INF = 0x3f3f3f3f;

int head[MAX_N], current, st, ed, dep[MAX_N], cur[MAX_N];
int n, m, g, sex[MAX_N];

struct edge
{
    int to, nxt, weight;
} edges[MAX_N << 2];

void addpath(int src, int dst, int weight)
{
    edges[current].to = dst, edges[current].nxt = head[src];
    edges[current].weight = weight, head[src] = current++;
}

void addtube(int src, int dst, int weight)
{
    addpath(src, dst, weight);
    addpath(dst, src, 0);
}

bool bfs()
{
    memset(dep, 0, sizeof(dep));
    queue<int> q;
    q.push(st), dep[st] = 1;
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        for (int i = head[u]; i != -1; i = edges[i].nxt)
            if (edges[i].weight > 0 && dep[edges[i].to] == 0)
                q.push(edges[i].to), dep[edges[i].to] = dep[u] + 1;
    }
    return dep[ed] != 0;
}

int dfs(int u, int flow)
{
    if (u == ed || flow == 0)
        return flow;
    for (int &i = cur[u]; i != -1; i = edges[i].nxt)
        if (edges[i].weight > 0 && dep[edges[i].to] == dep[u] + 1)
            if (int di = dfs(edges[i].to, min(flow, edges[i].weight)))
            {
                edges[i].weight -= di, edges[i ^ 1].weight += di;
                return di;
            }
    return 0;
}

int dinic()
{
    int ans = 0;
    while (bfs())
    {
        memcpy(cur, head, sizeof(head));
        while (int di = dfs(st, INF))
            ans += di;
    }
    return ans;
}

int main()
{
    memset(head, -1, sizeof(head));
    scanf("%d%d%d", &n, &m, &g);
    st = n + m + 1, ed = st + 1;
    for (int i = 1; i <= n; i++)
        scanf("%d", &sex[i]);
    for (int i = 1, ci; i <= n; i++)
    {
        scanf("%d", &ci);
        if (sex[i] == 0)
            addtube(st, i, ci);
        else
            addtube(i, ed, ci);
    }
    int answer = 0;
    for (int i = 1, wi, certainSex, ki, id, isFriend; i <= m; i++)
    {
        scanf("%d%d%d", &certainSex, &wi, &ki);
        answer += wi;
        while (ki--)
        {
            scanf("%d", &id);
            if (certainSex == 0)
                addtube(i + n, id, INF);
            else
                addtube(id, i + n, INF);
        }
        scanf("%d", &isFriend);
        if (certainSex == 0)
            addtube(st, i + n, wi + ((isFriend) ? g : 0));
        else
            addtube(i + n, ed, wi + ((isFriend) ? g : 0));
    }
    answer -= dinic();
    printf("%d", answer);
    return 0;
}
```
