本题解主要警示后人,如果你完全不懂,建议去楼上看其他大佬的题解。
本质与 P3227 [HNOI2013] 切糕 一致。
你考虑将选择哪套题目转化为网络流上的割边,令题目代价为边的流量,则最小割即为答案。
对于每条约束,连边 ,权值为 。
这样使得 时不能仅割断 两条边使得图不连通。容易证明多选一条边是不优的。
以下是作者整合和亲身经历的错误:
-
题意:没说明若第 题答错,直接结束答题。
(怎么还没修啊) -
题意:是“大 ”,而不是“差大于 ”。
-
如使用
cout,若不加std::fixed会用科学计数法输出。 -
double自带的 #INF 相减的值为 NaN。 -
用总流量判无解,而不是一次增广的流量。
-
多测清空要一直到 (网络流汇点)。
-
自定义的
Point(x,y),即二维点转点编号应如下(除非你不是用 虚点来避免取不合法点的):
#define Point(x, y) ((x - 1) * 82 + y)// 错误示范
#define Point(x, y) ((x - 1) * 80 + y)- 建边不能只建距离为 的,要将边缘长度不足 的继续连边,因为他们同样不满足条件,具体可以看以下代码区别:
// 正确的
for (int i = max(1, 1 - k); i <= m + 1; i++)
add_edge(Point(y, i), Point(x, min(i + k, m + 1)), inf);// 错误的
for (int i = 1; i <= m + 1; i++)
if (i + k <= m + 1 && i + k >= 1)
add_edge(Point(y, i), Point(x, i + k), inf);附代码。
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T &x)
{
x = 0;
register char c = getchar();
register short f = 1;
while (c < '0' || c > '9')
{
if (c == '-')
f = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
{
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
x *= f;
}
template <typename T, typename... Args>
inline void read(T &x, Args &...temps)
{
read(x), read(temps...);
}
const int N = 82;
typedef double ll;
const ll inf = 1e18;
const ll eps = 1e-9;
int S, T;
vector<int> tag[N * N * 4], dep, res;
vector<pair<int, ll>> g;
#define Point(x, y) ((x - 1) * 82 + y)
void add_edge(int x, int y, ll w)
{
tag[x].emplace_back(g.size());
g.emplace_back(y, w);
tag[y].emplace_back(g.size());
g.emplace_back(x, 0);
}
#define is0(x) (x < eps ? 1 : 0)
bool bfs()
{
dep.assign(T + 1, 0);
res.assign(T + 1, 0);
queue<int> q;
dep[S] = 1;
q.emplace(S);
while (!q.empty())
{
int x = q.front();
q.pop();
for (auto id : tag[x])
{
auto [y, w] = g[id];
if (dep[y] || is0(w))
continue;
dep[y] = dep[x] + 1;
if (y == T)
return 1;
q.emplace(y);
}
}
return dep[T];
}
ll dfs(int x, ll flow)
{
if (x == T)
return flow;
ll cnt = 0;
for (int &i = res[x]; i < tag[x].size(); i++)
{
int id = tag[x][i];
auto &[y, w] = g[id];
if (dep[x] + 1 != dep[y] || is0(w))
continue;
ll fw = dfs(y, min(flow, w));
if (fw)
{
cnt += fw;
flow -= fw;
w -= fw;
g[id ^ 1].second += fw;
if (!flow)
break;
}
}
return cnt;
}
int TwT, n, m, P, Q;
ll v[N][N];
ll dinic()
{
ll ans = 0;
while (bfs())
{
ans += dfs(S, inf);
if (ans >= inf)
return -1;
}
return ans;
}
void clear()
{
for (int i = 0; i <= T; i++)
tag[i].clear();
g.clear();
}
signed main()
{
read(TwT);
while (TwT--)
{
read(n, m, P, Q);
vector<int> c(P);
for (auto &x : c)
read(x);
for (int j = 1; j <= m; j++)
for (int i = 1; i <= n; i++)
{
v[i][j] = 0;
ll x = 0, y = 1;
for (int k = 0; k < P; k++)
{
scanf("%lf", &x);
v[i][j] += (y *= x) * c[k];
}
}
S = 0, T = Point(n, m + 1) + 1;
clear();
for (int i = 1; i <= n; i++)
{
add_edge(S, Point(i, 1), inf);
add_edge(Point(i, m + 1), T, inf);
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
add_edge(Point(i, j), Point(i, j + 1), v[i][j]);
for (int x, y, k; Q--;)
{
read(x, y, k);
for (int i = max(1, 1 - k); i <= m + 1; i++)
add_edge(Point(y, i), Point(x, min(i + k, m + 1)), inf);
}
auto ans = dinic();
if (ans < 0)
puts("-1");
else
printf("%.9lf\n", ans);
}
return 0;
}