CSES - Aalto Competitive Programming 2024 - wk7 - Wed - Results
Submission details
Task:Airport
Sender:aalto2024h_007
Submission time:2024-10-23 16:43:47 +0300
Language:C++ (C++11)
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#3ACCEPTED0.00 sdetails
#4ACCEPTED0.00 sdetails
#5ACCEPTED0.00 sdetails
#60.00 sdetails
#7ACCEPTED0.00 sdetails
#8ACCEPTED0.00 sdetails
#9ACCEPTED0.00 sdetails
#100.01 sdetails
#11ACCEPTED0.00 sdetails
#12ACCEPTED0.00 sdetails
#130.00 sdetails

Compiler report

input/code.cpp: In function 'int Ford_Fulkerson(int, int, int, std::vector<std::vector<long long int> >&)':
input/code.cpp:60:20: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   60 |     while (min_cap = bfs(source, target, n, parent, graph)) {
      |            ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Code

#include <climits>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

#define ll long long int

int bfs(int source, int target, int n, vector<int>& parent, vector<vector<ll>>& graph) {
    // Update the parent vector as each node value to be -1
    fill(parent.begin(), parent.end(), -1);
    // parent of source node to be -2
    parent[source] = -2;
    // Initializing queue for storing and min capacity so far
    queue<pair<int, ll>> q;
    // Source node min capacity to be 1e9
    q.push({source, 1e9});

    // Looping while queue is not empty
    while (!q.empty()) {
        // storing top node and min capacity so far
        int u = q.front().first;
        ll cap = q.front().second;
        // Removing top node from queue
        q.pop();
        // Looping all edges from u
        for (int v = 0; v < n; v++) {
            // finding v node has edge from u
            if (u != v && graph[u][v] != 0 && parent[v] == -1) {
                // storing parent v to be u
                parent[v] = u;
                // Updating the minimum capacity
                int min_cap = min(cap, graph[u][v]);
                // If v is the target node then return minimum capacity
                if (v == target) {
                    return min_cap;
                }
                // if we didn't find target node
                // Insert the v node and minimum capacity so far in queue
                q.push({v, min_cap});
            }
        }
    }
    // if we didn't find path between source to target return 0
    return 0;
}

int Ford_Fulkerson(int source, int target, int n, vector<vector<ll>>& graph) {
    // Initializing parent vector for finding the path from source to target
    // In which we add parent of the node
    vector<int> parent(n, -1);
    // Initializing maximum flow for storing ans
    int max_flow = 0;
    int min_cap = 0;  // storing minimum capacity in each path

    // looping while minimum capacity become zero
    // For finding path and minimum capacity we call function bfs()
    while (min_cap = bfs(source, target, n, parent, graph)) {


        // Adding the min_cap from this path
        max_flow += min_cap;
        // storing target node in v
        int v = target;

        // while we didn't find the source node
        // Looping through path stored in parent
        while (v != source) {
            // finding parent of v node
            int u = parent[v];
            // Subtracting minimum capacity from u to v
            // And adding minimum capacity from v to u
            graph[u][v] -= min_cap;
            graph[v][u] += min_cap;
            v = u;
        }
    }
    // Returning maximum flow in the graph
    return max_flow;
}

void addEdge(vector<vector<ll>>& graph,
             int u, int v, ll w) {
    graph[u][v] = w;
}



int main() {
    int n, m;

    cin >> n >> m;
    vector<vector<ll>> graph(n, vector<ll>(n, 0));
    vector<ll> values(m);

    for (int i = 0; i < n; i++)
    {
        int x;
        cin >> x;
        if (x == -1) values[i] = INT_MAX;
        else values[i] = x;

    }
    
    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        --a; --b;

        int bottleneck = min(values[a], values[b]);

        addEdge(graph, a, b, bottleneck);
    }

    int k = Ford_Fulkerson(0, n - 1, n, graph);

    cout << k << endl;
}

Test details

Test 1

Verdict: ACCEPTED

input
10 20
-1 85 7 19 90 72 11 46 65 -1
6 7
9 7
8 4
...

correct output
7

user output
7

Test 2

Verdict: ACCEPTED

input
10 20
-1 80 77 57 77 95 63 98 30 -1
6 7
8 9
7 8
...

correct output
30

user output
30

Test 3

Verdict: ACCEPTED

input
10 20
-1 63 16 42 62 70 9 94 68 -1
10 9
6 8
10 6
...

correct output
25

user output
25

Test 4

Verdict: ACCEPTED

input
10 20
-1 3 86 -1 32 34 9 50 -1 -1
6 7
7 8
9 2
...

correct output
3

user output
3

Test 5

Verdict: ACCEPTED

input
10 20
-1 43 38 -1 7 54 26 97 76 -1
3 9
9 10
6 7
...

correct output
76

user output
76

Test 6

Verdict:

input
100 1000
-1 425576195 274150382 1021768...

correct output
6091126630

user output
-223573888

Test 7

Verdict: ACCEPTED

input
100 1000
-1 769953265 -1 389517741 2323...

correct output
769953265

user output
769953265

Test 8

Verdict: ACCEPTED

input
100 1000
-1 584988267 763129662 6781413...

correct output
1699511766

user output
1699511766

Test 9

Verdict: ACCEPTED

input
100 1000
-1 921671366 121044688 2933366...

correct output
1805833567

user output
1805833567

Test 10

Verdict:

input
100 1000
-1 763842763 612011030 4532521...

correct output
3342235784

user output
-952731512

Test 11

Verdict: ACCEPTED

input
3 3
-1 1 -1
1 2
2 3
2 2

correct output
1

user output
1

Test 12

Verdict: ACCEPTED

input
3 4
-1 1 -1
1 2
1 2
2 3
...

correct output
1

user output
1

Test 13

Verdict:

input
7 8
-1 1 1 1 1 1 -1
1 2
1 3
2 4
...

correct output
1

user output
2