Skip to content

Commit

Permalink
Minify some files and add tested comment
Browse files Browse the repository at this point in the history
  • Loading branch information
OmeletWithoutEgg committed Oct 6, 2023
1 parent ff157b8 commit 253adb0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 58 deletions.
25 changes: 11 additions & 14 deletions codes/Geometry/MinMaxEnclosingRect.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
// from 8BQube, plz ensure p is strict convex hull
const llf INF = 1e18, qi = acos(-1) / 2 * 3;
pair<llf, llf> solve(vector<P> &p) {
#define Z(v) (p[v] - p[i])
llf mx = 0, mn = INF;
int n = (int)p.size(); p.emplace_back(p[0]);
pair<llf, llf> solve(const vector<P> &p) {
llf mx = 0, mn = INF; int n = (int)p.size();
for (int i = 0, u = 1, r = 1, l = 1; i < n; ++i) {
#define Z(v) (p[(v) % n] - p[i])
P e = Z(i + 1);
while (cross(e, Z(u + 1)) > cross(e, Z(u)))
u = (u + 1) % n;
while (dot(e, Z(r + 1)) > dot(e, Z(r)))
r = (r + 1) % n;
if (!i) l = (r + 1) % n;
while (dot(e, Z(l + 1)) < dot(e, Z(l)))
l = (l + 1) % n;
P D = p[r] - p[l];
mn = min(mn, dot(e, D) / llf(norm(e)) * cross(e, Z(u)));
while (cross(e, Z(u + 1)) > cross(e, Z(u))) ++u;
while (dot(e, Z(r + 1)) > dot(e, Z(r))) ++r;
if (!i) l = r + 1;
while (dot(e, Z(l + 1)) < dot(e, Z(l))) ++l;
P D = p[r % n] - p[l % n];
llf H = cross(e, Z(u)) / llf(norm(e));
mn = min(mn, dot(e, D) * H);
llf B = sqrt(norm(D)) * sqrt(norm(Z(u)));
llf deg = (qi - acos(dot(D, Z(u)) / B)) / 2;
mx = max(mx, B * sin(deg) * sin(deg));
}
return {mn, mx};
}
} // test @ UVA 819
80 changes: 37 additions & 43 deletions codes/Matching_and_Flow/Dinic.cpp
Original file line number Diff line number Diff line change
@@ -1,46 +1,40 @@
template <typename Cap = int64_t> class Dinic {
private:
struct E { int to, rev; Cap cap; }; int n, st, ed;
vector<vector<E>> G; vector<int> lv, idx;
bool BFS() {
lv.assign(n, -1);
queue<int> bfs; bfs.push(st); lv[st] = 0;
while (not bfs.empty()) {
int u = bfs.front(); bfs.pop();
for (auto e: G[u]) {
if (e.cap <= 0 or lv[e.to] != -1) continue;
bfs.push(e.to); lv[e.to] = lv[u] + 1;
}
}
return lv[ed] != -1;
}
Cap DFS(int u, Cap f) {
if (u == ed) return f;
Cap ret = 0;
for(int &i = idx[u]; i < int(G[u].size()); ++i) {
auto &e = G[u][i];
if (e.cap <= 0 or lv[e.to] != lv[u]+1) continue;
Cap nf = DFS(e.to, min(f, e.cap));
ret += nf; e.cap -= nf; f -= nf;
G[e.to][e.rev].cap += nf;
if (f == 0) return ret;
}
if (ret == 0) lv[u] = -1;
return ret;
}
struct E { int to, rev; Cap cap; }; int n, st, ed;
vector<vector<E>> G; vector<size_t> lv, idx;
bool BFS() {
lv.assign(n, 0); idx.assign(n, 0);
queue<int> bfs; bfs.push(st); lv[st] = 1;
while (not bfs.empty()) {
int u = bfs.front(); bfs.pop();
for (auto e: G[u]) if (e.cap > 0 and !lv[e.to])
bfs.push(e.to), lv[e.to] = lv[u] + 1;
}
return lv[ed];
}
Cap DFS(int u, Cap f = numeric_limits<Cap>::max()) {
if (u == ed) return f;
Cap ret = 0;
for (auto &i = idx[u]; i < G[u].size(); ++i) {
auto &[to, rev, cap] = G[u][i];
if (cap <= 0 or lv[to] != lv[u] + 1) continue;
Cap nf = DFS(to, min(f, cap));
ret += nf; cap -= nf; f -= nf;
G[to][rev].cap += nf;
if (f == 0) return ret;
}
if (ret == 0) lv[u] = 0;
return ret;
}
public:
void init(int n_) { G.assign(n = n_, vector<E>()); }
void add_edge(int u, int v, Cap c) {
G[u].push_back({v, int(G[v].size()), c});
G[v].push_back({u, int(G[u].size())-1, 0});
}
Cap max_flow(int st_, int ed_) {
st = st_, ed = ed_; Cap ret = 0, f;
while (BFS()) {
idx.assign(n, 0);
ret += (f = DFS(st, numeric_limits<Cap>::max()));
if (f == 0) break;
}
return ret;
}
};
void init(int n_) { G.assign(n = n_, vector<E>()); }
void add_edge(int u, int v, Cap c) {
G[u].push_back({v, int(G[v].size()), c});
G[v].push_back({u, int(G[u].size())-1, 0});
}
Cap max_flow(int st_, int ed_) {
st = st_, ed = ed_; Cap ret = 0;
while (BFS()) ret += DFS(st);
return ret;
}
}; // test @ luogu P3376
2 changes: 1 addition & 1 deletion codes/Misc/PrefixSubstringLCS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ void all_lcs(string S, string T) { // 0-base
// here, LCS(s[0, a], t[b, c]) =
// c - b + 1 - sum([h[i] > b] | i <= c)
}
}
} // test @ yosupo judge

0 comments on commit 253adb0

Please sign in to comment.