Алгоритм Эдмондса-Карпа С++

// IO LR4.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
//

#include <iostream>
#include <vector>
#include <queue>
#include <fstream>
using namespace std;
const int INF = 100000000;
int n;
vector<vector<int>> capacity;
vector<vector<int>> g(6);
vector<int> p;
int bfs(int s, int t, vector<int>& parent) {
    fill(parent.begin(), parent.end(), -1);
    parent[s] = -2;
    queue<pair<int, int>> q;
    q.push({ s, INF });

    while (!q.empty()) {
        int cur = q.front().first;
        int flow = q.front().second;
        q.pop();

        for (int next : g[cur]) {
            if (parent[next] == -1 && capacity[cur][next]) {
                parent[next] = cur;
                int new_flow = min(flow, capacity[cur][next]);
                if (next == t)
                    return new_flow;
                q.push({ next, new_flow });
            }
        }
    }

    return 0;
}

int maxflow(int s, int t) {
    int flow = 0;
    vector<int> parent(n);
    int new_flow;

    while (new_flow = bfs(s, t, parent)) {
        flow += new_flow;
        int cur = t;
        while (cur != s) {
            int prev = parent[cur];
            capacity[prev][cur] -= new_flow;
            capacity[cur][prev] += new_flow;
            cur = prev;
        }
    }

    return flow;
}
int main() { //0x795AFC66 
    setlocale(LC_ALL, "");
    ifstream in("input.txt");
    int m,x,y,z;
    in >> n;

    in >> m;
    g.resize(m);
    capacity.resize(m);
    for (int i = 0; i < m; i++)
    {
        in >> x >> y >> z;
    //  cout << x <<" " <<y <<" "<< z<<endl;
        g[x].push_back(y);
        g[y].push_back(x);
        g[i].push_back(z);
    }
    for (int i = 0; i < m; i++)
        for (int j = 0; j < m; j++)
            cout << g[i][j];
    //bfs(1, 6, p);
    //maxflow(1, 6);
    return 0;
}

Нашел реализацию алгоритма Эдмондса-Карпа на С++. У меня вылазит ошибка vector subscript is out of range. Я думаю,что как-то не так заполняю список смежности g. Что у меня не так? И в bfs что надо вводить в vector& parent?


Ответы (0 шт):