Submission details
Task:Internet connection
Sender:natalia
Submission time:2018-09-15 19:31:05 +0300
Language:C++
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.02 sdetails
#2ACCEPTED0.01 sdetails
#3ACCEPTED0.02 sdetails
#4ACCEPTED0.01 sdetails
#5ACCEPTED0.02 sdetails
#6ACCEPTED0.01 sdetails
#7ACCEPTED0.03 sdetails
#8ACCEPTED0.02 sdetails
#9ACCEPTED0.02 sdetails
#10ACCEPTED0.03 sdetails

Compiler report

input/code.cpp: In function 'int main(int, const char**)':
input/code.cpp:96:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i <= n; i++){
                    ~~^~~~
input/code.cpp:97:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int j = 0; j <= n; j++){
                        ~~^~~~
input/code.cpp:102:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < m; i++){
                    ~~^~~
input/code.cpp: In instantiation of 'std::__cxx11::list<T_NODE> bfs(const T_VALUE*, T_NODE, T_NODE, T_NODE) [with T_NODE = unsigned int; T_VALUE = long unsigned int]':
input/code.cpp:80:27:   required from 'T_VALUE max_flow(T_VALUE*, T_NODE, T_NODE, T_NODE) [with T_NODE = unsigned int; T_VALUE = long unsigned int]'
input/code.cpp:107:69:   required from here
input/code.cpp:9:22: warning: comparison between signed and unsigned...

Code

#include <iostream>
#include <list>
#include <queue>

template<typename T_NODE, typename T_VALUE>
std::list<T_NODE> bfs(const T_VALUE *data, const T_NODE n, const T_NODE v1, const T_NODE v2){
    
    T_NODE visited[n + 1];
    for(int i = 1; i <= n; i++)
        visited[i] = 0;
    visited[v1] = v1;
    
    std::queue<T_NODE> queue = {};
    queue.push(v1);
    
    while(!queue.empty()){
        T_NODE v = queue.front();
        queue.pop();
        if(data[v * (n + 1) + v2] > 0){
            visited[v2] = v;
            break;
        }
        for(int i = 1; i <= n; i++){
            if(visited[i] == 0 && data[v * (n + 1) + i] > 0){
                visited[i] = v;
                queue.push(i);
            }
        }
    }
    
    std::list<T_NODE> result = {};
    
    if(visited[v2] != 0){
        result.push_front(v2);
        T_NODE v = v2;
        while(v != v1){
            v = visited[v];
            result.push_front(v);
        }
    }
    return result;
}

template<typename T_NODE, typename T_VALUE>
T_VALUE path_max_throughput(const T_VALUE *data, const T_NODE n, std::list<T_NODE> path){
    typename std::list<T_NODE>::iterator it = path.begin();
    T_NODE v1 = *(it++);
    T_VALUE result = data[v1 * (n + 1) + *it];
    
    if(it != path.end()){
        v1 = *(it++);
        while(it != path.end()){
            T_VALUE value = data[v1 * (n + 1) + *it];
            if(value < result){
                result = value;
            }
            v1 = *(it++);
        }
    }
    return result;
}

template<typename T_NODE, typename T_VALUE>
void path_add_flow(T_VALUE *data, const T_NODE n, std::list<T_NODE> path, const T_VALUE flow){
    typename std::list<T_NODE>::iterator it = path.begin();
    T_NODE v1 = *(it++);
    while(it != path.end()){
        data[v1 * (n + 1) + *it] -= flow;
        data[*it * (n + 1) + v1] += flow;
        v1 = *(it++);
    }
}

template<typename T_NODE, typename T_VALUE>
T_VALUE max_flow(T_VALUE *data, const T_NODE n, const T_NODE source, const T_NODE target){
    typename std::list<T_NODE> path;
    T_VALUE flow = 0;
    
    do{
        path = bfs<T_NODE>(data, n, source, target);
        if(!path.empty()){
            T_VALUE throughput = path_max_throughput<T_NODE, T_VALUE>(data, n, path);
            path_add_flow<T_NODE, T_VALUE>(data, n, path, throughput);
            flow += throughput;
        }
    } while (!path.empty());
    
    return flow;
}

int main(int argc, const char * argv[]) {
    unsigned int n, m;
    std::cin >> n >> m;
    
    unsigned long *data = (unsigned long *)malloc((n + 1) * (n + 1) * sizeof(unsigned long));
    for(int i = 0; i <= n; i++){
        for(int j = 0; j <= n; j++){
            data[i * (n + 1) + j] = 0;
        }
    }

    for(int i = 0; i < m; i++){
        unsigned int a, b;
        std::cin >> a >> b >> data[a * (n + 1) + b];
    }
    
    std::cout << max_flow<unsigned int, unsigned long>(data, n, 1, n) << "\n";
    
    return 0;
}

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