mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-11-09 15:58:47 +00:00
Compare commits
5 Commits
0b2d1d4c45
...
c3dc655d1c
Author | SHA1 | Date | |
---|---|---|---|
c3dc655d1c | |||
c0d8b1dd63 | |||
939dc609f8 | |||
2ec62c8df4 | |||
96ccc02052 |
90
LibreOJ/6009/6009.cpp
Normal file
90
LibreOJ/6009/6009.cpp
Normal file
@ -0,0 +1,90 @@
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <queue>
|
||||
|
||||
using std::cin;
|
||||
using std::cout;
|
||||
const char endl = '\n';
|
||||
|
||||
const int N = 25,
|
||||
M = 105;
|
||||
const int INF = 0x3f3f3f3f;
|
||||
|
||||
int n, m;
|
||||
std::array<int, M> a, b1, b2, f1, f2;
|
||||
std::array<int, 1 << N> dist;
|
||||
std::array<bool, 1 << N> vis;
|
||||
|
||||
bool check(int s, int i) {
|
||||
if ((b1[i] | s) != s) return false;
|
||||
if (b2[i] & s) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void spfa(int s) {
|
||||
std::fill(dist.begin(), dist.end(), INF);
|
||||
|
||||
std::queue<int> q;
|
||||
q.push(s);
|
||||
dist[s] = 0;
|
||||
vis[s] = true;
|
||||
|
||||
while (!q.empty()) {
|
||||
int u = q.front();
|
||||
q.pop();
|
||||
|
||||
for (int i = 1; i <= m; i++) {
|
||||
if (!check(u, i)) continue;
|
||||
|
||||
int v = u ^ (u & f1[i]) | f2[i];
|
||||
|
||||
if (dist[u] + a[i] < dist[v]) {
|
||||
dist[v] = dist[u] + a[i];
|
||||
|
||||
if (!vis[v]) {
|
||||
q.push(v);
|
||||
vis[v] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
vis[u] = false;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
cin >> n >> m;
|
||||
|
||||
for (int i = 1; i <= m; i++) {
|
||||
std::string b, f;
|
||||
|
||||
cin >> a[i] >> b >> f;
|
||||
|
||||
for (int j = 0; j < b.size(); j++) {
|
||||
if (b[j] == '+') {
|
||||
b1[i] = b1[i] | 1 << j;
|
||||
} else if (b[j] == '-') {
|
||||
b2[i] = b2[i] | 1 << j;
|
||||
}
|
||||
}
|
||||
|
||||
for (int j = 0; j < f.size(); j++) {
|
||||
if (f[j] == '-') {
|
||||
f1[i] = f1[i] | 1 << j;
|
||||
} else if (f[j] == '+') {
|
||||
f2[i] = f2[i] | 1 << j;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
spfa((1 << n) - 1);
|
||||
|
||||
cout << (dist[0] == INF ? 0 : dist[0]) << endl;
|
||||
|
||||
return 0;
|
||||
}
|
BIN
LibreOJ/6009/data/bug1.in
(Stored with Git LFS)
Normal file
BIN
LibreOJ/6009/data/bug1.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
LibreOJ/6009/data/bug1.out
(Stored with Git LFS)
Normal file
BIN
LibreOJ/6009/data/bug1.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
LibreOJ/6009/data/bug10.in
(Stored with Git LFS)
Normal file
BIN
LibreOJ/6009/data/bug10.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
LibreOJ/6009/data/bug10.out
(Stored with Git LFS)
Normal file
BIN
LibreOJ/6009/data/bug10.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
LibreOJ/6009/data/bug2.in
(Stored with Git LFS)
Normal file
BIN
LibreOJ/6009/data/bug2.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
LibreOJ/6009/data/bug2.out
(Stored with Git LFS)
Normal file
BIN
LibreOJ/6009/data/bug2.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
LibreOJ/6009/data/bug3.in
(Stored with Git LFS)
Normal file
BIN
LibreOJ/6009/data/bug3.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
LibreOJ/6009/data/bug3.out
(Stored with Git LFS)
Normal file
BIN
LibreOJ/6009/data/bug3.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
LibreOJ/6009/data/bug4.in
(Stored with Git LFS)
Normal file
BIN
LibreOJ/6009/data/bug4.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
LibreOJ/6009/data/bug4.out
(Stored with Git LFS)
Normal file
BIN
LibreOJ/6009/data/bug4.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
LibreOJ/6009/data/bug5.in
(Stored with Git LFS)
Normal file
BIN
LibreOJ/6009/data/bug5.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
LibreOJ/6009/data/bug5.out
(Stored with Git LFS)
Normal file
BIN
LibreOJ/6009/data/bug5.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
LibreOJ/6009/data/bug6.in
(Stored with Git LFS)
Normal file
BIN
LibreOJ/6009/data/bug6.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
LibreOJ/6009/data/bug6.out
(Stored with Git LFS)
Normal file
BIN
LibreOJ/6009/data/bug6.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
LibreOJ/6009/data/bug7.in
(Stored with Git LFS)
Normal file
BIN
LibreOJ/6009/data/bug7.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
LibreOJ/6009/data/bug7.out
(Stored with Git LFS)
Normal file
BIN
LibreOJ/6009/data/bug7.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
LibreOJ/6009/data/bug8.in
(Stored with Git LFS)
Normal file
BIN
LibreOJ/6009/data/bug8.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
LibreOJ/6009/data/bug8.out
(Stored with Git LFS)
Normal file
BIN
LibreOJ/6009/data/bug8.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
LibreOJ/6009/data/bug9.in
(Stored with Git LFS)
Normal file
BIN
LibreOJ/6009/data/bug9.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
LibreOJ/6009/data/bug9.out
(Stored with Git LFS)
Normal file
BIN
LibreOJ/6009/data/bug9.out
(Stored with Git LFS)
Normal file
Binary file not shown.
156
LibreOJ/6122/6122.cpp
Normal file
156
LibreOJ/6122/6122.cpp
Normal file
@ -0,0 +1,156 @@
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <queue>
|
||||
#include <string>
|
||||
|
||||
using std::cin;
|
||||
using std::cout;
|
||||
const char endl = '\n';
|
||||
|
||||
const int N = 105,
|
||||
M = 5005,
|
||||
INF = 0x3f3f3f3f;
|
||||
|
||||
int n, m, s, t, flow, cost;
|
||||
bool flag;
|
||||
std::map<std::string, int> map;
|
||||
std::string a[N];
|
||||
|
||||
// Graph
|
||||
int idx, head[N], ver[M << 1], next[M << 1];
|
||||
std::pair<int, int> edge[M << 1];
|
||||
|
||||
void add(int u, int v, int flow, int cost) {
|
||||
next[idx] = head[u];
|
||||
ver[idx] = v;
|
||||
edge[idx] = std::make_pair(flow, cost);
|
||||
head[u] = idx++;
|
||||
}
|
||||
|
||||
// Dinic
|
||||
int dist[N];
|
||||
bool vis[N];
|
||||
|
||||
bool spfa() {
|
||||
memset(vis, 0x00, sizeof(vis));
|
||||
memset(dist, 0x3f, sizeof(dist));
|
||||
|
||||
std::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; i = next[i]) {
|
||||
int v = ver[i],
|
||||
c = edge[i].first,
|
||||
w = edge[i].second;
|
||||
|
||||
if (dist[v] > dist[u] + w && c > 0) {
|
||||
dist[v] = dist[u] + w;
|
||||
if (!vis[v]) {
|
||||
q.push(v);
|
||||
vis[v] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return dist[t] != INF;
|
||||
}
|
||||
|
||||
int dinic(int u, int limit) {
|
||||
if (u == t) return limit;
|
||||
|
||||
int flow = 0;
|
||||
vis[u] = true;
|
||||
for (int i = head[u]; ~i && flow < limit; i = next[i]) {
|
||||
int v = ver[i],
|
||||
c = edge[i].first,
|
||||
w = edge[i].second;
|
||||
|
||||
if (dist[v] == dist[u] + w && c && !vis[v]) {
|
||||
int k = dinic(v, std::min(c, limit - flow));
|
||||
if (!k) dist[v] = INF;
|
||||
edge[i].first -= k;
|
||||
edge[i ^ 1].first += k;
|
||||
flow += k;
|
||||
cost += k * w;
|
||||
}
|
||||
}
|
||||
vis[u] = false;
|
||||
|
||||
return flow;
|
||||
}
|
||||
|
||||
void dfs1(int u) {
|
||||
vis[u] = true;
|
||||
cout << a[u - n] << endl;
|
||||
for (int i = head[u]; ~i; i = next[i]) {
|
||||
if (ver[i] <= n && !edge[i].first) {
|
||||
dfs1(ver[i] + n);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void dfs2(int u) {
|
||||
for (int i = head[u]; ~i; i = next[i]) {
|
||||
if (ver[i] <= n && !edge[i].first && !vis[ver[i] + n]) {
|
||||
dfs2(ver[i] + n);
|
||||
}
|
||||
}
|
||||
cout << a[u - n] << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
|
||||
memset(head, 0xff, sizeof(head));
|
||||
|
||||
cin >> n >> m;
|
||||
for (int i = 1; i <= n; i++) {
|
||||
cin >> a[i];
|
||||
map[a[i]] = i;
|
||||
if (i == 1 || i == n) {
|
||||
add(i, i + n, 2, 0);
|
||||
add(i + n, i, 0, 0);
|
||||
} else {
|
||||
add(i, i + n, 1, 0);
|
||||
add(i + n, i, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
s = 1, t = n * 2;
|
||||
for (int i = 1; i <= m; i++) {
|
||||
std::string u, v;
|
||||
cin >> u >> v;
|
||||
int uu = map[u],
|
||||
vv = map[v];
|
||||
if (uu == 1 && vv == n) flag = true;
|
||||
add(uu + n, vv, 1, -1);
|
||||
add(vv, uu + n, 0, 1);
|
||||
}
|
||||
|
||||
while (spfa()) flow += dinic(s, INF);
|
||||
|
||||
if (flow == 2) {
|
||||
cout << -cost << endl;
|
||||
dfs1(n + 1);
|
||||
dfs2(n + 1);
|
||||
} else if (!flag) { // flow != 2
|
||||
cout << "No Solution!" << endl;
|
||||
} else { // flow != 2 && flag
|
||||
cout << 2 << endl
|
||||
<< a[1] << endl
|
||||
<< a[n] << endl
|
||||
<< a[1] << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
BIN
LibreOJ/6122/data/airl0.in
(Stored with Git LFS)
Normal file
BIN
LibreOJ/6122/data/airl0.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
LibreOJ/6122/data/airl0.out
(Stored with Git LFS)
Normal file
BIN
LibreOJ/6122/data/airl0.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
LibreOJ/6122/data/airl1.in
(Stored with Git LFS)
Normal file
BIN
LibreOJ/6122/data/airl1.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
LibreOJ/6122/data/airl1.out
(Stored with Git LFS)
Normal file
BIN
LibreOJ/6122/data/airl1.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
LibreOJ/6122/data/airl10.in
(Stored with Git LFS)
Normal file
BIN
LibreOJ/6122/data/airl10.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
LibreOJ/6122/data/airl10.out
(Stored with Git LFS)
Normal file
BIN
LibreOJ/6122/data/airl10.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
LibreOJ/6122/data/airl2.in
(Stored with Git LFS)
Normal file
BIN
LibreOJ/6122/data/airl2.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
LibreOJ/6122/data/airl2.out
(Stored with Git LFS)
Normal file
BIN
LibreOJ/6122/data/airl2.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
LibreOJ/6122/data/airl3.in
(Stored with Git LFS)
Normal file
BIN
LibreOJ/6122/data/airl3.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
LibreOJ/6122/data/airl3.out
(Stored with Git LFS)
Normal file
BIN
LibreOJ/6122/data/airl3.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
LibreOJ/6122/data/airl4.in
(Stored with Git LFS)
Normal file
BIN
LibreOJ/6122/data/airl4.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
LibreOJ/6122/data/airl4.out
(Stored with Git LFS)
Normal file
BIN
LibreOJ/6122/data/airl4.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
LibreOJ/6122/data/airl5.in
(Stored with Git LFS)
Normal file
BIN
LibreOJ/6122/data/airl5.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
LibreOJ/6122/data/airl5.out
(Stored with Git LFS)
Normal file
BIN
LibreOJ/6122/data/airl5.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
LibreOJ/6122/data/airl6.in
(Stored with Git LFS)
Normal file
BIN
LibreOJ/6122/data/airl6.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
LibreOJ/6122/data/airl6.out
(Stored with Git LFS)
Normal file
BIN
LibreOJ/6122/data/airl6.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
LibreOJ/6122/data/airl7.in
(Stored with Git LFS)
Normal file
BIN
LibreOJ/6122/data/airl7.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
LibreOJ/6122/data/airl7.out
(Stored with Git LFS)
Normal file
BIN
LibreOJ/6122/data/airl7.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
LibreOJ/6122/data/airl8.in
(Stored with Git LFS)
Normal file
BIN
LibreOJ/6122/data/airl8.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
LibreOJ/6122/data/airl8.out
(Stored with Git LFS)
Normal file
BIN
LibreOJ/6122/data/airl8.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
LibreOJ/6122/data/airl9.in
(Stored with Git LFS)
Normal file
BIN
LibreOJ/6122/data/airl9.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
LibreOJ/6122/data/airl9.out
(Stored with Git LFS)
Normal file
BIN
LibreOJ/6122/data/airl9.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
LibreOJ/6122/data/spj.cpp
(Stored with Git LFS)
Normal file
BIN
LibreOJ/6122/data/spj.cpp
(Stored with Git LFS)
Normal file
Binary file not shown.
99
Luogu/P1642/P1642.cpp
Normal file
99
Luogu/P1642/P1642.cpp
Normal file
@ -0,0 +1,99 @@
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <iomanip>
|
||||
#include <limits>
|
||||
#include <vector>
|
||||
|
||||
using std::cin;
|
||||
using std::cout;
|
||||
const char endl = '\n';
|
||||
|
||||
const int N = 105;
|
||||
const double eps = 1e-4;
|
||||
|
||||
int n, m;
|
||||
std::array<int, N> siz;
|
||||
std::array<double, N> d;
|
||||
std::array<std::array<double, N>, N> f;
|
||||
std::array<std::vector<int>, N> g;
|
||||
std::array<std::pair<int, int>, N> a;
|
||||
|
||||
void dfs(int u, int fa) {
|
||||
f[u][0] = 0;
|
||||
siz[u] = 1;
|
||||
|
||||
for (int v : g[u]) {
|
||||
if (v == fa) continue;
|
||||
|
||||
dfs(v, u);
|
||||
|
||||
siz[u] += siz[v];
|
||||
|
||||
for (int i = std::min(siz[u], m); ~i; i--) {
|
||||
for (int j = 0; j <= std::min(siz[v], i); j++) {
|
||||
f[u][i] = std::max(f[u][i], f[u][i - j] + f[v][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = std::min(m, siz[u]); i; i--) {
|
||||
f[u][i] = f[u][i - 1] + d[u];
|
||||
}
|
||||
}
|
||||
|
||||
bool check(double mid) {
|
||||
for (int i = 1; i <= n; i++) {
|
||||
d[i] = static_cast<double>(a[i].first) - mid * a[i].second;
|
||||
}
|
||||
|
||||
std::for_each(f.begin(), f.end(), [&](auto &x) {
|
||||
std::fill(x.begin(), x.end(), std::numeric_limits<int>::min());
|
||||
});
|
||||
|
||||
dfs(1, -1);
|
||||
|
||||
for (int i = 1; i <= n; i++) {
|
||||
if (f[i][m] > -eps) return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
cin >> n >> m;
|
||||
|
||||
m = n - m;
|
||||
|
||||
for (int i = 1; i <= n; i++) {
|
||||
cin >> a[i].first;
|
||||
}
|
||||
|
||||
for (int i = 1; i <= n; i++) {
|
||||
cin >> a[i].second;
|
||||
}
|
||||
|
||||
for (int i = 1, u, v; i < n; i++) {
|
||||
cin >> u >> v;
|
||||
|
||||
g[u].emplace_back(v);
|
||||
g[v].emplace_back(u);
|
||||
}
|
||||
|
||||
double l = 0,
|
||||
r = 10000;
|
||||
|
||||
while (r - l > eps) {
|
||||
double mid = (l + r) / 2;
|
||||
|
||||
if (check(mid)) l = mid;
|
||||
else r = mid;
|
||||
}
|
||||
|
||||
cout << std::fixed << std::setprecision(1) << l << endl;
|
||||
|
||||
return 0;
|
||||
}
|
90
Luogu/P2761/P2761.cpp
Normal file
90
Luogu/P2761/P2761.cpp
Normal file
@ -0,0 +1,90 @@
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <queue>
|
||||
|
||||
using std::cin;
|
||||
using std::cout;
|
||||
const char endl = '\n';
|
||||
|
||||
const int N = 22,
|
||||
M = 105;
|
||||
const int INF = 0x3f3f3f3f;
|
||||
|
||||
int n, m;
|
||||
std::array<int, M> a, b1, b2, f1, f2;
|
||||
std::array<int, 1 << N> dist;
|
||||
std::array<bool, 1 << N> vis;
|
||||
|
||||
bool check(int s, int i) {
|
||||
if ((b1[i] | s) != s) return false;
|
||||
if (b2[i] & s) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void spfa(int s) {
|
||||
std::fill(dist.begin(), dist.end(), INF);
|
||||
|
||||
std::queue<int> q;
|
||||
q.push(s);
|
||||
dist[s] = 0;
|
||||
vis[s] = true;
|
||||
|
||||
while (!q.empty()) {
|
||||
int u = q.front();
|
||||
q.pop();
|
||||
|
||||
for (int i = 1; i <= m; i++) {
|
||||
if (!check(u, i)) continue;
|
||||
|
||||
int v = u ^ (u & f1[i]) | f2[i];
|
||||
|
||||
if (dist[u] + a[i] < dist[v]) {
|
||||
dist[v] = dist[u] + a[i];
|
||||
|
||||
if (!vis[v]) {
|
||||
q.push(v);
|
||||
vis[v] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
vis[u] = false;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
cin >> n >> m;
|
||||
|
||||
for (int i = 1; i <= m; i++) {
|
||||
std::string b, f;
|
||||
|
||||
cin >> a[i] >> b >> f;
|
||||
|
||||
for (int j = 0; j < b.size(); j++) {
|
||||
if (b[j] == '+') {
|
||||
b1[i] = b1[i] | 1 << j;
|
||||
} else if (b[j] == '-') {
|
||||
b2[i] = b2[i] | 1 << j;
|
||||
}
|
||||
}
|
||||
|
||||
for (int j = 0; j < f.size(); j++) {
|
||||
if (f[j] == '-') {
|
||||
f1[i] = f1[i] | 1 << j;
|
||||
} else if (f[j] == '+') {
|
||||
f2[i] = f2[i] | 1 << j;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
spfa((1 << n) - 1);
|
||||
|
||||
cout << (dist[0] == INF ? 0 : dist[0]) << endl;
|
||||
|
||||
return 0;
|
||||
}
|
78
Luogu/P4322/P4322.cpp
Normal file
78
Luogu/P4322/P4322.cpp
Normal file
@ -0,0 +1,78 @@
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <iomanip>
|
||||
#include <limits>
|
||||
#include <vector>
|
||||
|
||||
using std::cin;
|
||||
using std::cout;
|
||||
const char endl = '\n';
|
||||
|
||||
const int N = 2505;
|
||||
const double eps = 1e-4;
|
||||
|
||||
int k, n;
|
||||
std::array<int, N> siz;
|
||||
std::array<double, N> d;
|
||||
std::array<std::array<double, N>, N> f;
|
||||
std::array<std::vector<int>, N> g;
|
||||
std::array<std::pair<int, int>, N> a;
|
||||
|
||||
void dfs(int u) {
|
||||
f[u][1] = d[u];
|
||||
siz[u] = 1;
|
||||
|
||||
for (int v : g[u]) {
|
||||
dfs(v);
|
||||
|
||||
siz[u] += siz[v];
|
||||
|
||||
for (int i = std::min(siz[u], k + 1); i; i--) {
|
||||
for (int j = 0; j <= std::min(siz[v], i - 1); j++) {
|
||||
f[u][i] = std::max(f[u][i], f[u][i - j] + f[v][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool check(double mid) {
|
||||
for (int i = 1; i <= n; i++) {
|
||||
d[i] = static_cast<double>(a[i].second) - mid * a[i].first;
|
||||
}
|
||||
|
||||
std::for_each(f.begin(), f.end(), [&](auto &x) {
|
||||
std::fill(x.begin(), x.end(), std::numeric_limits<int>::min());
|
||||
});
|
||||
|
||||
dfs(0);
|
||||
|
||||
return f[0][k + 1] >= 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
cin >> k >> n;
|
||||
|
||||
for (int i = 1, r; i <= n; i++) {
|
||||
cin >> a[i].first >> a[i].second >> r;
|
||||
|
||||
g[r].emplace_back(i);
|
||||
}
|
||||
|
||||
double l = 0,
|
||||
r = 10000;
|
||||
|
||||
while (r - l > eps) {
|
||||
double mid = (l + r) / 2;
|
||||
|
||||
if (check(mid)) l = mid;
|
||||
else r = mid;
|
||||
}
|
||||
|
||||
cout << std::fixed << std::setprecision(3) << l << endl;
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user