Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 12 additions & 18 deletions Ford_Bellman.cpp
Original file line number Diff line number Diff line change
@@ -1,44 +1,38 @@
#define int long long

struct Edge{
int u, v;
int weight;
friend istream &operator >> (istream &is, Edge &e){
struct Edge {
int u, v, weight;
friend istream &operator >> (istream &is, Edge &e) {
is >> e.u >> e.v >> e.weight;
--e.u, --e.v;
return is;
}

};

void Ford_Bellman() {
void Ford_Bellman(void) {
int n, m;
cin >> n >> m;
vector<int> dist(n, -1e18);
vector<int> prev(n);
vector<Edge> edges(m);
cin >> edges;
dist[0] = 0;
for (int i = 0; i < n - 1; ++i) {
for (auto e : edges) {
for (int i = 0; i < n - 1; ++i)
for (auto e : edges)
if (dist[e.v] < dist[e.u] + e.weight) {
dist[e.v] = dist[e.u] + e.weight;
prev[e.v]= e.u;
}
}
}
//if we have not got negative cycles then all evaluated

/// if we have not got negative cycles then all evaluated
Vertex *end = nullptr;
for (auto e : edges) {
if (e.second->dist > e.first->dist + weight[e.first, e.second]) {
for (auto e : edges)
if (e.second->dist > e.first->dist + weight[e.first, e.second])
end = e.second;
}
}
for (int i = 0; i < n; ++i) {
for (int i = 0; i < n; ++i)
end = end->pred;
}
Vertex *p = end;
while (p != end){
while (p != end) {
cout << p;
p = p->pred;
}
Expand Down
252 changes: 199 additions & 53 deletions adjacencyclass.cpp
Original file line number Diff line number Diff line change
@@ -1,60 +1,206 @@
#define WHITE 0
#define GRAY 1
#define BLACK 2
using namespace std;
constexpr size_t MAIN_SIZE = 64;

struct basic_vertex {
int x;
int used;
vector<Vertex *> neibh;
basic_vertex(int a) : x(a), used(WHITE)
{ neibh.reserve(MAIN_SIZE); }
};

class Vertex : public basic_vertex {
Vertex(int a) : basic_vertex(a)
{}
vector<int> weight;
Vertex *parent;
};

void bind(basic_vertex *a, basic_vertex *b) {
a->neibh.push_back(b);
b->neibh.push_back(a);
}

void dfs(basic_vertex *v) {
v->used = BLACK;
for (Vertex * &u : v->neibh)
if (u->used = WHITE)
dfs(u);
}
namespace graphs {
struct graph {
struct vertex {
struct edge {
vertex *p;
int weight;

void bind_or(basic_vertex *a, basic_vertex *b) {
a->neibh.push_back(b);
}
edge(vertex *p, int w) : p(p), weight(w) {}

void remove(basic_vertex *a, basic_vertex *b) {
auto i1 = find(a->neibh.begin(), a->neibh.end(), b);
auto i2 = find(b->neibh.begin(), b->neibh.end(), a);
a->neibh.erase(i1);
b->neibh.erase(i2);
}
bool operator<(const edge &e) const { return *p < *e.p; }
};

void remove_or(basic_vertex *a, basic_vertex *b) {
a->neibh.erase(find(a->neibh.begin(), a->neibh.end(), b));
}
vector<edge> edges;
int n, component, up, depth, degree;
int x, y;
bool visited = false;
bool artpoint;

bool operator<(const vertex &v) const { return n < v.n; }

bool operator==(const vertex &v) const { return n == v.n; }

explicit vertex(int n = 0) : component(-1), n(n), depth(1e15), up(-1), degree(0) {}

void dfs() {
for (edge &e : edges) {
e.p->dfs();
}
}

void relax() {
for (edge e : edges) {
if (e.p->depth > depth + e.weight) e.p->depth = depth + e.weight;
}
}

void set_comp(int c) {
if (component != -1) return; else component = c;
for (edge &e : edges) e.p->set_comp(c);
}

void bfs() {
up = depth = 0;
queue<vertex *> queue;
queue.push(this);
while (!queue.empty()) {
vertex *v = queue.front();
queue.pop();
for (edge e : v->edges) {
if (e.p->depth != -1) continue;
queue.push(e.p);
e.p->up = e.p->depth = v->depth + 1;
}
}
}

void top_sort(vector<vertex *> &top_sorted) {
visited = true;
for (edge e : edges) {
if (!e.p->visited) e.p->top_sort(top_sorted);
}
top_sorted.push_back(this);
}

bool find(vertex *f, vector<vertex *> way) {
if (*this == *f) return true;
visited = true;
for (edge e : edges) {
if (!e.p->visited) {
if (e.p->find(f, way)) {
way.push_back(this);
return true;
}
}
}
return false;
}

void find_artpoints(vertex *parent) {
visited = true;
int count = 0;
for (edge e : edges) {
if (!e.p->visited) {
e.p->up = e.p->depth = depth + 1;
e.p->find_artpoints(this);
count++;
up = min(up, e.p->up);
degree = max(degree, e.p->up);
}
if (e.p != parent)
up = min(up, e.p->depth);
}
if ((parent != nullptr && degree >= depth && count > 0) || (parent == nullptr && count > 1))
this->artpoint = true;
}

friend ostream &operator<<(ostream &os, vertex *v) {
os << v->n + 1;
return os;
}
};

vector<vertex *> vertices;
int vertex_count, edge_count;
bool directed, weight;

explicit graph(int n, int m, bool directed = false, bool weight = false)
: vertices(n), vertex_count(n), edge_count(m), directed(directed), weight(weight) {
for (int i = 0; i < n; ++i) {
vertices[i] = new vertex(i);
}
}

inline vertex *operator[](int idx) { return vertices[idx]; }

void link(int i, int j, int w = 1) {
vertices[i]->edges.emplace_back(vertices[j], w);
if (!directed) vertices[j]->edges.emplace_back(vertices[i], w);
}

void create(int R, int x) {
for (vertex *v : vertices) v->edges.clear();
for (int i = 0; i < vertex_count; ++i) {
for (int j = i + 1; j < vertex_count; ++j) {
if (i == x || j == x) continue;
if (((vertices[i]->x - vertices[j]->x) * (vertices[i]->x - vertices[j]->x) +
(vertices[i]->y - vertices[j]->y) * (vertices[i]->y - vertices[j]->y)) <= R)
link(i, j);
}
}
}

friend istream &operator>>(istream &is, graph &G) {
for (int i = 0; i < G.edge_count; ++i) {
int u = 0, v = 0, w = 1;
cin >> u >> v;
if (G.weight) cin >> w;
G.link(u - 1, v - 1, w);
}
return is;
}

vector<vertex *> find_artpoints() {
for (vertex *v : vertices) v->visited = false;
for (vertex *v : vertices) {
if (!v->visited) {
v->up = v->depth = 0;
v->find_artpoints(nullptr);
}
}
vector<vertex *> artpoints = *new vector<vertex *>;
for (vertex *v : vertices) {
if (v->artpoint) artpoints.push_back(v);
}
return artpoints;
}

vector<vertex *> top_sort() {
for (vertex *v : vertices) v->visited = false;
vector<vertex *> top_sorted = *new vector<vertex *>;
for (vertex *v : vertices) {
if (!v->visited) {
v->top_sort(top_sorted);
}
}
reverse(top_sorted.begin(), top_sorted.end());
return top_sorted;
}

vector<vertex *> way(int s, int t) {
for (vertex *v : vertices) v->visited = false;
vector<vertex *> ans;
vertices[s]->find(vertices[t], ans);
return ans;
}

int comp_count() {
for (vertex *v : vertices) v->component = -1;
int c = 0;
for (vertex *v : vertices) {
if (v->component == -1) v->set_comp(c++);
}
return c;
}
};

struct tree : graph {
vertex *root;

explicit tree(int n, int root = 0, bool weight = false)
: graph(n, n - 1, true, weight), root(vertices[0]) {}

friend istream &operator>>(istream &is, tree &T) {
for (int i = 0; i < T.edge_count; ++i) {
int parent = 0, weight = 0;
cin >> parent;
if (weight) cin >> weight;
T[parent]->edges.emplace_back(T[i + 1], weight);
}
return is;
}
};

bool has_cycle(basic_vertex *v)
{
v->used = GRAY;
for (auto &u : v->neibh) {
if (u->used == WHITE) {
if (dfs(u)) return true;
} else if (u->used == GRAY)
return true;
}
v->used = BLACK;
return false;
}
38 changes: 37 additions & 1 deletion adjacencymatrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class AdjMatrix {
int **g;
int n;
AgjMatrix(int _n) {
if (_n > 20000) // very rarely you have more than 512MB
throw exception("'_n' is too large for matrix creating!");
n = _n;
g = new int*[n];
for (int i = 0; i < n; i++)
Expand All @@ -18,7 +20,7 @@ class AdjMatrix {
delete[] g[i];
delete[] g;
}

void floyd()
/**
* if you want to save original weights, you should copy the graph
Expand All @@ -41,3 +43,37 @@ void floyd(int **g, int n)
for (int j = 0; j < n; j++)
g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
}

void floyd_with_path(int **g, int n) {
int **next = new int*[n];
for (int i = 0; i < n; i++)
next[i] = new int[n];
for (int k = 0; k < n; k++) i
for (int i = 0; i < n; i++) u
for (int j = 0; j < n; j++) v
if (g[i][k] + g[k][j] < g[i][j]) {
g[i][j] = g[i][k] + g[k][j];
next[i][j] = next[i][k];
}
return next;
}

void readAdjMatOr(AdjMatrix &g) {
int n;
cin >> n;
g = AgjMatrix(n);
for (int i = 0, a, b, w; i < n; i++) {
cin >> a >> b >> w;
g.g[a][b] = w;
}
}

void readAdjMatNOr(AdjMatrix &g) {
int n;
cin >> n;
g = AgjMatrix(n);
for (int i = 0, a, b, w; i < n; i++) {
cin >> a >> b >> w;
g.g[a][b] = g.g[b][a] = w;
}
}
Loading