C++:
#include <algorithm>
#include <cstdio>
#include <cstring>
#define N 2000
#define M 110000
using namespace std;
int A[N], B[N], C[N], D[N], S, T, X, a[N], b[M], c[M], e[N], h[N], m, n, q[N], s, t;
double E[N], d[M];
inline void add(int u, int v, double w) {
b[++t] = a[u], c[a[u] = t] = v, d[t] = w, b[++t] = a[v], c[a[v] = t] = u, d[t] = 0;
}
double dfs(int u, double f) {
if (u == X)
return f;
double v = 0, x;
for (int& i = e[u]; i; i = b[i])
if (d[i] && h[c[i]] == h[u] + 1 && (x = dfs(c[i], min(d[i], f)))) {
f -= x, v += x, d[i] -= x, d[i ^ 1] += x;
if (!f)
return v;
}
h[u] = -1;
return v;
}
inline bool bfs() {
memset(h + 1, -1, X << 2), *h = 0;
for (int l = 0, r = 1; l < r;)
for (int u = q[l++], i = a[u]; i; i = b[i])
if (d[i] && h[c[i]] < 0)
h[q[r++] = c[i]] = h[u] + 1;
return h[X] >= 0;
}
inline bool dinic() {
while (bfs()) memcpy(e, a, X + 1 << 2), dfs(0, 1e18);
for (int i = *a; i; i = b[i])
if (d[i])
return false;
return true;
}
int main() {
freopen("cheese.in", "r", stdin), freopen("cheese.out", "w", stdout);
scanf("%d", &T);
while (T--) {
scanf("%d%d", &m, &n);
double l = 0, o, r = 3000000;
for (int i = 1; i <= m; i++) scanf("%d%d%d", A + i, B + i, C + i);
for (int i = 1; i <= n; i++) scanf("%d", D + i);
sort(D + 1, D + n + 1), reverse(D + 1, D + n + 1), D[n + 1] = 0;
for (int i = 1; i <= n; i++) D[i] -= D[i + 1];
while (r - l >= 1e-8) {
o = (l + r) / 2, S = 0, t = 1;
for (int i = 1; i <= m; i++) E[++S] = B[i], E[++S] = C[i] + o, add(0, i, A[i]);
sort(E + 1, E + S + 1), s = 1;
for (int i = 2; i <= S; i++)
if (E[i] != E[s])
E[++s] = E[i];
S = m, X = (s - 1) * n + m + 1;
for (int i = 1; i < s; S += n, i++) {
for (int j = 1; j <= n; j++) add(S + j, X, (E[i + 1] - E[i]) * D[j] * j);
for (int j = 1; j <= m; j++)
if (E[i] - B[j] >= -1e-8 && C[j] + o - E[i + 1] >= -1e-8)
for (int k = 1; k <= n; k++) add(j, S + k, (E[i + 1] - E[i]) * D[k]);
}
dinic() ? r = o : l = o, memset(a, 0, X + 1 << 2);
}
printf("%.8lf\n", l);
}
return 0;
}