---
id: 1404
lang: en
translation_of: index.md
source_sha256: b30f3d79bf7a5565a32854dc581e7fccc7700ce1ee60efea6f487378d4d91de1
title: "[Fortuna OJ] Jul 5th - Group A Problem-Solving Report / National Training Team Mutual Test 2013"
description: "A: enumerate frequency ranges with a DSU. B: min-cost feasible flow with bounds, quadratic costs split into unit edges. C: linked elsewhere."
tags: ["OI","Graph Theory","Disjoint-Set Union","Brute Force","Network Flow","Min-Cost Flow","Flow with Lower Bounds"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "集训队互测 2013 in the title rendered as “National Training Team Mutual Test 2013”."
  - "Problem names 家族, 供电网络 and 城市规划 rendered literally as Family, Power Supply Network and City Planning."
  - "The link text “Available Here” was already English and is kept as is."
---

## A - Family

This problem really is a <span class="slang" data-zh="送分题" data-gloss="sòngfēn tí, “point-giving problem”. A problem so easy it is basically free points.">free-points problem</span> (I was just about to come up with straight brute-force enumeration + DSU when I went and looked at the solution, and in the end it turned out to be exactly this <span class="slang" data-zh="sb" data-gloss="Initials of 傻逼 (shǎbī). A vulgar insult, roughly “idiot” or “stupid”.">stupid</span>).

Consider enumerating the frequency interval $[L, R]$ (sort the edges, fix the lower bound and then enumerate the upper bound); this part is $O(m^2)$. Every time we enumerate a lower bound we have to initialize the DSU, and then when merging two sets we just update the answer according to their sizes.

<!--more-->

```cpp
// A.cpp
#include <bits/stdc++.h>
#define ll long long

using namespace std;

const ll INF = 0x3f3f3f3f3f3f3f3f, MAX_N = 1010, MAX_E = 5050;

ll n, m, k, ans = INF, fa[MAX_N], current_ans, siz[MAX_N], dat[MAX_N];

struct edge
{
    ll from, to, frequency;
    bool operator<(const edge& e) const { return frequency < e.frequency; }
} edges[MAX_E];

void ufs_initialize()
{
    for (ll i = 1; i <= n; i++)
        fa[i] = i, siz[i] = 1;
}

ll find(ll x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }

void unite(ll x, ll y) 
{
    x = find(x), y = find(y);
    if (x == y)
        return;
    if (siz[x] < siz[y])
        swap(x, y);
    current_ans -= dat[siz[x]] + dat[siz[y]];
    fa[y] = fa[x], siz[x] += siz[y];
    current_ans += dat[siz[x]];
}

signed main()
{
    scanf("%lld%lld%lld", &n, &m, &k);
    for (ll i = 1; i <= n; i++)
        scanf("%lld", &dat[i]);
    for (ll i = 1; i <= m; i++)
        scanf("%lld%lld%lld", &edges[i].from, &edges[i].to, &edges[i].frequency);
    sort(edges + 1, edges + 1 + m);
    for (ll lbound = 1, lower = edges[1].frequency; lbound <= m; lbound++, lower = edges[lbound].frequency)
    {
        ufs_initialize(), current_ans = dat[1] * n;
        for (ll rbound = lbound, upper = edges[rbound].frequency; rbound <= m; rbound++, upper = edges[rbound].frequency)
        {
            ll width = upper - lower;
            unite(edges[rbound].from, edges[rbound].to);
            if (current_ans >= k)
            {
                ans = min(ans, width);
                break;
            }
        }
    }
    if (ans == INF)
        puts("T_T");
    else
        printf("%lld", ans);
    return 0;
}
```

## B - Power Supply Network

By the second problem things already started getting <span class="slang" data-zh="毒瘤" data-gloss="dúliú, “tumor”. A problem (or problem setter) that is painfully hard, tedious, or full of traps.">nasty</span>. Boiled down, the statement is this:

> Given a network with lower and upper bounds that has a source and a sink, where the cost of each edge is a quadratic function of its flow. Just find the minimum-cost feasible flow.

First, ignoring the quadratic relation, the modeling alone is very simple: it is the standard model of a feasible flow with lower and upper bounds and a source and a sink. Connect each city to the super source and sink according to its surplus, and connect it to the source and sink according to its prices for buying and selling electricity.

After that, we can discretize the continuous quadratic relation: that is, the flow pushed through each time is fixed at $1$, and after each shortest-path run and tallying of the answer, we consider adding more edges: with a flow upper bound of $1$ and a lower bound of $0$, and a cost equal to the difference between this flow and the previous one, namely $a_i (x + 1)^2 - b_i (x + 1) - a_i x^2 - b_i x_i = 2 a_i x _+ a_i + b_i$. Based on this formula we can add a new edge. Then just check whether it is feasible.

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

using namespace std;

const int MAX_N = 330, MAX_E = 5050, INF = 0x3f3f3f3f;

int n, m, head[MAX_N], current, di[MAX_N], s, t, S, T, answer, flag[MAX_N][MAX_N];
int dist[MAX_N], upward[MAX_N], flow[MAX_N];

bool vis[MAX_N];

struct edge
{
    int to, nxt, weight, cost;
} edges[MAX_E << 1];

struct side
{
    int u, v, a, b, L, R;
} original_data[MAX_E << 1];

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

void addtube(int src, int dst, int lower, int upper, int cost, bool doDi = true)
{
    addpath(src, dst, upper - lower, cost), addpath(dst, src, 0, -cost);
    if (doDi)
        di[dst] += lower, di[src] -= lower;
}

bool spfa()
{
    for (int i = 1; i <= m; i++)
        if (flag[original_data[i].u][original_data[i].v] == original_data[i].L && original_data[i].L < original_data[i].R)
        {
            original_data[i].L++;
            addtube(original_data[i].u, original_data[i].v, 0, 1, original_data[i].a * (2 * original_data[i].L - 1) + original_data[i].b, false);
        }
    memset(dist, 0x3f, sizeof(dist));
    queue<int> q;
    q.push(S), dist[S] = 0, vis[S] = true;
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        vis[u] = false;
        for (int i = head[u]; i != -1; i = edges[i].nxt)
            if (dist[edges[i].to] > dist[u] + edges[i].cost && edges[i].weight > 0)
            {
                dist[edges[i].to] = dist[u] + edges[i].cost, upward[edges[i].to] = i;
                if (!vis[edges[i].to])
                    vis[edges[i].to] = true, q.push(edges[i].to);
            }
    }
    return dist[T] != INF;
}

void find()
{
    int u = T, i = upward[u];
    while (u != S)
    {
        edges[i].weight--, edges[i ^ 1].weight++;
        answer += edges[i].cost;
        flag[edges[i ^ 1].to][edges[i].to]++, flag[edges[i].to][edges[i ^ 1].to]--;
        u = edges[i ^ 1].to, i = upward[u];
    }
}

int main()
{
    memset(head, -1, sizeof(head));
    scanf("%d%d", &n, &m);
    s = n + 1, t = s + 1, S = t + 1, T = S + 1;
    for (int i = 1, lft, in, out; i <= n; i++)
    {
        scanf("%d%d%d", &lft, &in, &out);
        if (lft > 0)
            addtube(s, i, lft, lft, 0);
        else if (lft < 0)
            addtube(i, t, -lft, -lft, 0);
        addtube(s, i, 0, INF, in), addtube(i, t, 0, INF, out);
    }
    for (int i = 1; i <= m; i++)
    {
        scanf("%d%d%d%d%d%d", &original_data[i].u, &original_data[i].v,
              &original_data[i].a, &original_data[i].b, &original_data[i].L, &original_data[i].R);
        addtube(original_data[i].u, original_data[i].v, original_data[i].L, original_data[i].L, 0);
        answer += original_data[i].L * (original_data[i].a * original_data[i].L + original_data[i].b);
        flag[original_data[i].u][original_data[i].v] += original_data[i].L;
        flag[original_data[i].v][original_data[i].u] -= original_data[i].L;
    }
    // in searching the possible stream;
    addtube(t, s, 0, INF, 0);
    for (int i = 1; i <= t; i++)
        if (di[i] > 0)
            addtube(S, i, 0, di[i], 0);
        else if (di[i] < 0)
            addtube(i, T, 0, -di[i], 0);
    while (spfa())
        find();
    printf("%d", answer);
    return 0;
}
```

## C - City Planning

~~I will come back and write up this <span class="slang" data-zh="毒瘤" data-gloss="dúliú, “tumor”. A problem (or problem setter) that is painfully hard, tedious, or full of traps.">nasty</span> problem once I have fully figured out polynomials, convolution and stuff like that.~~

[Available Here](../1479-bzoj-3456-city-planning/index.en.html)
