Submission details
Task:Internet connection
Sender:jeroenrobben
Submission time:2018-09-21 16:58:57 +0300
Language:C++
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.02 sdetails
#2ACCEPTED0.02 sdetails
#3ACCEPTED0.02 sdetails
#4ACCEPTED0.01 sdetails
#5ACCEPTED0.02 sdetails
#6ACCEPTED0.03 sdetails
#7ACCEPTED0.02 sdetails
#8ACCEPTED0.03 sdetails
#9ACCEPTED0.01 sdetails
#10ACCEPTED0.02 sdetails

Code

#include <iostream>
#include <queue>
#include <vector>
#define index(source, sink) ((source * amountNodes) + sink)
#define MAX_CAPACITY 1000000000

using namespace std;


int amountNodes, amountEdges;
long totalFlow;

bool bfs(int capacities [], int flows [], vector<int> neighbours [], int predecesor []) {
    int currentNode;

    queue<int> queueNodesToCheck;
    queueNodesToCheck.push(0);
    
    fill_n(predecesor, amountNodes, -1);

    while(!queueNodesToCheck.empty()) {
        currentNode = queueNodesToCheck.front(); 
        queueNodesToCheck.pop();
        for(int neighbourNode: neighbours[currentNode]) {
            if(neighbourNode != 0 && predecesor[neighbourNode] == -1 && capacities[index(currentNode, neighbourNode)] > flows[index(currentNode, neighbourNode)]) {
                predecesor[neighbourNode] = currentNode;
                queueNodesToCheck.push(neighbourNode);


                // cout << currentNode;
                // cout << " currentNode\n";

                // cout << neighbourNode;
                // cout << " neighbourNode\n";

            }
        }
        if(predecesor[amountNodes-1] != -1){
            break;
        }
    }
    // cout << predecesor[amountNodes-1];
    // cout << " pred sink\n";
    return predecesor[amountNodes-1] != -1;
}


void increaseFlow(int capacities [], int flows [], int predecesor []) {
    //get flow amount
    int currentNode = amountNodes-1;
    int df = MAX_CAPACITY;
    int nextNode = predecesor[currentNode];
    while(nextNode != -1) {
        df = min(df, capacities[index(nextNode, currentNode)] - flows[index(nextNode, currentNode)]);
        
        // cout << currentNode;
        // cout << " currentNode\n";
        // cout << nextNode;
        // cout << " nextNode\n";
        // cout <<  predecesor[nextNode];
        // cout << "  predecesor[nextNode]\n";


        currentNode = nextNode;
        nextNode = predecesor[currentNode];
    }
    if(df < 0) {
        //cout << df;
        cout << '\n';
    }

    currentNode = amountNodes-1;
    nextNode = predecesor[currentNode];

    while(nextNode != -1) {
        flows[index(nextNode, currentNode)] += df;
        flows[index(currentNode, nextNode)] -= df;
        currentNode = nextNode;
        nextNode = predecesor[currentNode];
    }
    totalFlow += df;
}

long edmondsKarp(int capacities [], int flows [], vector<int> neighbours []) {
    int * predecesor = new int [amountNodes];
    totalFlow = 0;
    while(bfs(capacities, flows, neighbours, predecesor)) {
        increaseFlow(capacities, flows, predecesor);
    }

    return totalFlow;

}




int main() {
    cin >> amountNodes >> amountEdges;
    vector<int> neighbours [amountNodes];

    int * capacities = new int [amountNodes*amountNodes];
    int * flows = new int [amountNodes*amountNodes];

    fill_n(capacities, amountNodes*amountNodes, 0);
    fill_n(flows, amountNodes*amountNodes, 0);

    int source, sink, cap;
    for(int edgeId = 0; edgeId < amountEdges; edgeId++ ) {
        cin >> source >> sink >> cap;
        source--;sink--;
        neighbours[source].push_back(sink);
        neighbours[sink].push_back(source);
        capacities[index(source, sink)] = capacities[index(sink, source)] = flows[index(sink, source)] = cap;
    }


    cout << edmondsKarp(capacities, flows, neighbours) << endl;
}

Test details

Test 1

Verdict: ACCEPTED

input
10 20
5 6 19
4 5 47
3 5 7
4 9 62
...

correct output
73

user output
73

Test 2

Verdict: ACCEPTED

input
10 20
2 4 63
7 9 54
6 7 16
2 3 9
...

correct output
110

user output
110

Test 3

Verdict: ACCEPTED

input
10 20
5 6 90
2 3 46
7 8 80
6 7 60
...

correct output
29

user output
29

Test 4

Verdict: ACCEPTED

input
10 20
3 4 76
5 7 8
3 8 71
4 7 24
...

correct output
95

user output
95

Test 5

Verdict: ACCEPTED

input
10 20
1 8 22
6 7 40
4 5 20
8 10 77
...

correct output
156

user output
156

Test 6

Verdict: ACCEPTED

input
100 1000
63 85 864540192
22 91 974117435
64 66 953124912
85 88 6080960
...

correct output
4397669179

user output
4397669179

Test 7

Verdict: ACCEPTED

input
100 1000
36 93 760720873
12 75 175717522
78 79 340128710
80 83 181753465
...

correct output
5298558023

user output
5298558023

Test 8

Verdict: ACCEPTED

input
100 1000
20 60 909693891
55 91 570199535
21 41 118646902
37 82 824735480
...

correct output
5466229311

user output
5466229311

Test 9

Verdict: ACCEPTED

input
100 1000
26 44 753330451
62 67 821574279
70 95 219303983
7 44 980013084
...

correct output
4893925638

user output
4893925638

Test 10

Verdict: ACCEPTED

input
100 1000
15 89 501388091
50 71 396801720
15 92 324349822
29 85 184420157
...

correct output
6956499595

user output
6956499595