[Fortuna OJ] Jul 5th - Group A Problem-Solving Report / National Training Team Mutual Test 2013
A - Family
This problem really is a free-points problem (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 stupid).
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.
// 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 nasty. 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.
// 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 nasty problem once I have fully figured out polynomials, convolution and stuff like that.