Skip to content
Snippets Groups Projects
Commit 4542fc7a authored by Bruno Freitas Tissei's avatar Bruno Freitas Tissei
Browse files

Add things

parent a5408daf
No related branches found
No related tags found
No related merge requests found
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
// Edge struct to be used in adjacency list similar to vector<ii> graph[MAX], // Edge struct to be used in adjacency list similar to vector<ii> graph[MAX],
// but storing more information than ii // but storing more information than ii
typedef struct edge { struct edge {
int u; int u;
int flow, cap; int flow, cap;
...@@ -17,7 +17,7 @@ typedef struct edge { ...@@ -17,7 +17,7 @@ typedef struct edge {
edge(int u, int flow, int cap, int rev) : edge(int u, int flow, int cap, int rev) :
u(u), flow(flow), cap(cap), rev(rev) u(u), flow(flow), cap(cap), rev(rev)
{} {}
} egde; };
int depth[MAX]; int depth[MAX];
...@@ -27,8 +27,8 @@ vector<edge> graph[MAX]; ...@@ -27,8 +27,8 @@ vector<edge> graph[MAX];
// Adds edge between s and t with capacity c // Adds edge between s and t with capacity c
void add_edge(int s, int t, int c) { void add_edge(int s, int t, int c) {
edge forward(t, 0, c, graph[t].sz); edge forward(t, 0, c, graph[t].size());
edge backward(s, 0, 0, graph[s].sz); edge backward(s, 0, 0, graph[s].size());
graph[s].pb(forward); graph[s].pb(forward);
graph[t].pb(backward); graph[t].pb(backward);
...@@ -65,7 +65,7 @@ int dfs(int s, int t, int flow) { ...@@ -65,7 +65,7 @@ int dfs(int s, int t, int flow) {
return flow; return flow;
// Start iteration from where it last stopped to avoid repetitions // Start iteration from where it last stopped to avoid repetitions
for ( ; start[s] < graph[s].sz; ++start[s]) { for ( ; start[s] < graph[s].size(); ++start[s]) {
edge &e = graph[s][start[s]]; edge &e = graph[s][start[s]];
// If the next vertex is further from the source (and closer to the sink) // If the next vertex is further from the source (and closer to the sink)
......
...@@ -13,7 +13,7 @@ bool cont[MAX]; ...@@ -13,7 +13,7 @@ bool cont[MAX];
// Finds if there's a path between s and t using non-full // Finds if there's a path between s and t using non-full
// residual edges // residual edges
bool path(int s, int t) { bool bfs(int s, int t) {
queue<int> Q; queue<int> Q;
Q.push(s); Q.push(s);
cont[s] = true; cont[s] = true;
...@@ -45,7 +45,7 @@ int edmonds_karp(int s, int t) { ...@@ -45,7 +45,7 @@ int edmonds_karp(int s, int t) {
memcpy(rg, graph, sizeof(graph)); memcpy(rg, graph, sizeof(graph));
// Repeat while there's a valid path between s and t // Repeat while there's a valid path between s and t
while (path(s, t)) { while (bfs(s, t)) {
int flow = inf; int flow = inf;
// Get the minimum capacity among all edges of the chosen path // Get the minimum capacity among all edges of the chosen path
......
...@@ -13,7 +13,7 @@ bool cont[MAX]; ...@@ -13,7 +13,7 @@ bool cont[MAX];
// Finds if there's a path between s and t using non-full // Finds if there's a path between s and t using non-full
// residual edges // residual edges
bool path(int s, int t) { bool dfs(int s, int t) {
cont[s] = true; cont[s] = true;
if (s == t) if (s == t)
return true; return true;
...@@ -22,7 +22,7 @@ bool path(int s, int t) { ...@@ -22,7 +22,7 @@ bool path(int s, int t) {
if (!cont[i] && rg[s][i]) { if (!cont[i] && rg[s][i]) {
par[i] = s; par[i] = s;
if (path(i, t)) if (dfs(i, t))
return true; return true;
} }
...@@ -39,7 +39,7 @@ int ford_fulkerson(int s, int t) { ...@@ -39,7 +39,7 @@ int ford_fulkerson(int s, int t) {
memcpy(rg, graph, sizeof(graph)); memcpy(rg, graph, sizeof(graph));
// Repeat while there's a valid path between s and t // Repeat while there's a valid path between s and t
while (path(s, t)) { while (dfs(s, t)) {
int flow = inf; int flow = inf;
// Get the minimum capacity among all edges of the chosen path // Get the minimum capacity among all edges of the chosen path
......
...@@ -5,19 +5,19 @@ ...@@ -5,19 +5,19 @@
* Complexity (Space): O(n) * Complexity (Space): O(n)
*/ */
typedef struct avl_node_t { struct avl_node_t {
int key; int key;
int size; // number of nodes bellow (optional) int size; // number of nodes bellow (optional)
int height; // height of node int height; // height of node
struct avl_node_t *left; avl_node_t *left;
struct avl_node_t *right; avl_node_t *right;
} avl_node_t; };
typedef struct avl_t { struct avl_t {
avl_node_t *root; avl_node_t *root;
} avl_t; };
static inline int get_height(avl_node_t *node) { static inline int get_height(avl_node_t *node) {
......
...@@ -11,12 +11,12 @@ ...@@ -11,12 +11,12 @@
typedef pair<double, double> point; typedef pair<double, double> point;
typedef vector<point> pset; typedef vector<point> pset;
typedef struct node { struct node {
double radius; double radius;
point center; point center;
node *left, *right; node *left, *right;
} node; };
double distance(point &a, point &b) { double distance(point &a, point &b) {
......
...@@ -43,7 +43,6 @@ int main() { ...@@ -43,7 +43,6 @@ int main() {
assert(false); assert(false);
}; };
int flo = n / k; int flo = n / k;
int cei = (n - 1) / k + 1; int cei = (n - 1) / k + 1;
......
...@@ -45,6 +45,7 @@ bool solve(int i, int r) { ...@@ -45,6 +45,7 @@ bool solve(int i, int r) {
return dp[i][r] = false; return dp[i][r] = false;
} }
int main() { int main() {
ios::sync_with_stdio(0); ios::sync_with_stdio(0);
cin.tie(0); cin.tie(0);
...@@ -55,4 +56,3 @@ int main() { ...@@ -55,4 +56,3 @@ int main() {
cout << (!solve(0, 0) ? "*" : s) << ende; cout << (!solve(0, 0) ? "*" : s) << ende;
return 0; return 0;
} }
...@@ -141,12 +141,14 @@ int query(int p, int q) { ...@@ -141,12 +141,14 @@ int query(int p, int q) {
else return max(ans, max(cost[p][0], cost[q][0])); else return max(ans, max(cost[p][0], cost[q][0]));
} }
int main() { int main() {
ios::sync_with_stdio(0); ios::sync_with_stdio(0);
cin.tie(0); cin.tie(0);
int n, r; cin >> n >> r; int n, r; cin >> n >> r;
map<ii,int> M; map<ii,int> M;
for (int i = 0; i < r; ++i) { for (int i = 0; i < r; ++i) {
int a, b, c; cin >> a >> b >> c; int a, b, c; cin >> a >> b >> c;
a--, b--; a--, b--;
......
#include <bits/stdc++.h>
#define MAX 3010
#define EPS 1e-6
#define MOD 1000000007
#define inf 0x3f3f3f3f
#define llinf 0x3f3f3f3f3f3f3f3f
#define fi first
#define se second
#define pb push_back
#define ende '\n'
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define mset(x, y) memset(&x, (y), sizeof(x))
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
struct edge {
int u;
int flow, cap;
int rev;
edge(int u, int flow, int cap, int rev) :
u(u), flow(flow), cap(cap), rev(rev)
{}
};
int depth[MAX];
int start[MAX];
vector<edge> graph[MAX];
void add_edge(int s, int t, int c) {
edge forward(t, 0, c, graph[t].size());
edge backward(s, 0, 0, graph[s].size());
graph[s].pb(forward);
graph[t].pb(backward);
}
bool bfs(int s, int t) {
queue<int> Q;
Q.push(s);
mset(depth, -1);
depth[s] = 0;
while (!Q.empty()) {
int v = Q.front(); Q.pop();
for (auto i : graph[v]) {
if (depth[i.u] == -1 && i.flow < i.cap) {
depth[i.u] = depth[v] + 1;
Q.push(i.u);
}
}
}
return depth[t] != -1;
}
int dfs(int s, int t, int flow) {
if (s == t)
return flow;
for ( ; start[s] < graph[s].size(); ++start[s]) {
edge &e = graph[s][start[s]];
if (depth[e.u] == depth[s] + 1 && e.flow < e.cap) {
int min_flow = dfs(e.u, t, min(flow, e.cap - e.flow));
if (min_flow > 0) {
e.flow += min_flow;
graph[e.u][e.rev].flow -= min_flow;
return min_flow;
}
}
}
return 0;
}
int dinic(int s, int t) {
int ans = 0;
while (bfs(s, t)) {
mset(start, 0);
while (int flow = dfs(s, t, inf))
ans += flow;
}
return ans;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int p, r, c; cin >> p >> r >> c;
int s = 0, t = MAX - 1;
int dem = 0;
vector<int> dems(p);
for (auto &i : dems) {
cin >> i;
dem += i;
}
vector<int> ests(r);
for (auto &i : ests) cin >> i;
vector<pair<ii,int>> total;
for (int i = 0; i < c; ++i) {
int a, b, T; cin >> a >> b >> T;
total.pb(make_pair(ii(a, b), T));
}
int L = 0, R = 1010101;
for (int i = 0; i < 20; ++i) {
int m = (L + R) / 2;
for (int j = 0; j < p; ++j)
add_edge(j + 1 + r, t, dems[j]);
for (int j = 0; j < r; ++j)
add_edge(s, j + 1, ests[j]);
for (auto j : total)
if (j.se <= m)
add_edge(j.fi.se, j.fi.fi + r, inf);
if (dinic(s, t) < dem)
L = m + 1;
else
R = m - 1;
for (int j = 0; j < MAX; ++j)
graph[j].clear();
}
cout << (L >= 1010101 ? -1 : L) << ende;
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment