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

Code

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

unsigned long *data;
int n, m;

std::list<unsigned int> get_path(){
    unsigned int visited[n + 1];
    for(int i = 0; i <= n; i++)
        visited[i] = 0;
    
    visited[1] = 1;
    
    std::queue<unsigned int> queue({1});
    
    while(!queue.empty()){
        unsigned int v = queue.front();
        queue.pop();
        if(data[v * (n + 1) + n] > 0){
            visited[n] = 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<unsigned int> result = {};
    
    if(visited[n] != 0){
        result.push_front (n);
        unsigned int last = n;
        
        while(last != 1){
            last = visited[last];
            result.push_front (last);
        }
    }
    
    return result;
}

unsigned long get_path_throughput(std::list<unsigned int> path){
    unsigned long result = ULONG_MAX;
    std::list<unsigned int>::iterator it = path.begin();
    unsigned int v1 = *it;
    it++;
    for(; it != path.end(); ++it) {
        unsigned long val = data[v1 * (n + 1) + *it];
        if(val < result){
            result = val;
        }
        v1 = *it;
    }
    return result;
}

void process_path(std::list<unsigned int> path, unsigned long throughput){
    std::list<unsigned int>::iterator it = path.begin();
    unsigned int v1 = *it;
    it++;
    for(; it != path.end(); ++it){
        //std::cout << v1 << "," << *it << std::endl;
        data[v1 * (n + 1) + *it] -= throughput;
        data[*it * (n + 1) + v1] += throughput;
        v1 = *it;
    }
}

int main(int argc, const char * argv[]) {
    
    std::cin >> n >> m;
    //std::cout << "n=" << n << " m=" << m << "\n";
    
    data = (unsigned long *)malloc((n + 1) * (n + 1) * sizeof(unsigned long));
    unsigned int a, b;
    unsigned long c;
    
    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++){
        std::cin >> a >> b >> c;
        //std::cout << "a=" << a << " b=" << b << " c=" << c << "\n";
        data[a * (n + 1) + b] = c;
    }
    
    /*
    for(int i = 0; i < n + 1; i++){
        for(int j = 0; j < n + 1; j++){
            std::cout << data[i * (n + 1) + j] << "\t";
        }
        std::cout << "\n";
    }
    std::cout << "\n";
     */
    
    std::list<unsigned int> mypath;
    unsigned long total_throughput = 0;
    do{
        mypath = get_path();
        
        /*
        std::cout << "path  \n";
        for (std::list<unsigned int>::const_iterator i = mypath.begin(); i != mypath.end(); ++i)
            std::cout << *i << ' ';
        std::cout << "\n";
         */
        
        if(!mypath.empty()){
            unsigned long throughput = get_path_throughput(mypath);
            total_throughput += throughput;
            //std::cout << "throughput " << throughput << std::endl;
            
            process_path(mypath, throughput);
            
            /*
            for(int i = 0; i < n + 1; i++){
                for(int j = 0; j < n + 1; j++){
                    std::cout << data[i * (n + 1) + j] << "\t";
                }
                std::cout << "\n";
            }
            std::cout << "\n";
             */
        }
      
        
    } while (!mypath.empty());
    
    
    std::cout << total_throughput << "\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