Am I use Q.front on wrong way because this is false

your usage of .front() is fine
the description of your problem is awful, ¿what is false? ¿why is that a problem?

ps: I would consider a conceptual error to compare against true or false
Hello Freedent,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



#include<bits/stdc++.h> . This is not a standard C++ header file. Forget that you ever learned it. It is not helping you. Also it includes many header files that you do not need or have even heard of. You are better off learning what header files you need for a program.

You should compile your program before posting. Fix the errors that you can and post the complete error message of what you do not understand.

bool visited[a+1]={false};. This is non standard C++ as C++ does not allow VLAs, Variable Length Arrays. The size of the array needs to be known at compile time so stack memory can be set aside.

Try to avoid using global variables. Just as easy as they appear to be easy to use they are also easy to change. Tracking down where the unwanted change is made could take you hours to find. Especially as your programs grow in the number of lines and functions when you get there.

I noticed int path = 1e9;. Here you are trying to stuff a "double" into a smaller "int". There will be data loss when the decimal part is dropped.

Single letter variable names may be easy to type, but they have no meaning to anyone except you. Give your variables a good name that describes what it is or does. This makes the program much easier to read and follow and the 1st benefit is to you.

The comments in the program should help:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
#include <queue>  // <--- Added.

//#include<bits/stdc++.h>  // <--- Not a standard C++ header file. DO NOT USE.

using namespace std;  // <--- Best not to use.

constexpr unsigned MAXSIZE{ 10 };  // <--- Added.

int a, b;  // <--- ALWAYS initialize all your variables.
int graph[MAXSIZE][MAXSIZE]{ 0 };
int residualGraph[MAXSIZE][MAXSIZE]{ 0 };
int parent[MAXSIZE];
int mx = 0;

bool bfs(int s, int e)
{
    bool visited[a + 1] = { false };  // <--- Array size needs to be a constant value.
    queue<int>Q;

    Q.push(s);

    visited[s] = true;

    parent[s] = -1;

    while (!Q.empty())
    {
        int u = Q.front();

        Q.pop();

        for (int v = 0; v <= a; v++)
        {
            if (visited[v] == false && residualGraph[ u][v] > 0)
            {
                Q.push(v);

                parent[v] = u;

                visited[v] = true;
            }
        }
    }

    if (visited[e])
        return true;
    else
        return false;
}

void edmondsKarpov(int s, int e)
{
    for (int u = 1; u <= a; u++)
    {
        for (int v = 1; v <= a; v++)
            residualGraph[  u][v] = graph[ u][v];
    }

    while (bfs(s, e) == true)
    {
        int path = 1e9;  // <--- Wrong variable type.

        for (int v = e; v != s; v = parent[v])
        {
            int u = parent[v];

            path = min(path, residualGraph[ u][v]);
        }

        for (int v = e; v != s; v = parent[v])
        {
            int u = parent[v];

            residualGraph[ u][v] -= path;
            residualGraph[v][ u] += path;
        }

        mx = mx + path;
    }
}

int main()
{
    // <--- The "cin" ALWAYS needs a prompt.
    cin >> a >> b;

    int u, v, w;  // <--- ALWAYS initialize all your variables.

    for (int i = 1; i <= b; i++)
    {
        // <--- Needs a prompt.
        cin >> u >> v >> w;  // <--- "u" and "v" could end up accessing the array past its end.

        graph[ u][v] = w;
    }

    edmondsKarpov(1, a);

    cout << mx << endl;

    return 0;
}

In the array dimension using "u" I had to put a space before the "u" so it would not be read as an underline tag. Another good reason for a proper variable name.

Andy
Topic archived. No new replies allowed.