Task: | Internet connection |
Sender: | lautat |
Submission time: | 2018-09-15 15:18:39 +0300 |
Language: | C++ |
Status: | READY |
Result: | ACCEPTED |
test | verdict | time | |
---|---|---|---|
#1 | ACCEPTED | 0.02 s | details |
#2 | ACCEPTED | 0.02 s | details |
#3 | ACCEPTED | 0.01 s | details |
#4 | ACCEPTED | 0.01 s | details |
#5 | ACCEPTED | 0.01 s | details |
#6 | ACCEPTED | 0.02 s | details |
#7 | ACCEPTED | 0.02 s | details |
#8 | ACCEPTED | 0.02 s | details |
#9 | ACCEPTED | 0.01 s | details |
#10 | ACCEPTED | 0.03 s | details |
Code
#include <algorithm> #include <cstdint> #include <deque> #include <iostream> #include <vector> using std::cin; using std::cout; using std::deque; using std::endl; using std::make_pair; using std::min; using std::pair; using std::vector; #define MAX_FLOW 1000000000 pair<uint32_t, vector<size_t>> find_path( const size_t n, const vector<vector<uint32_t>> weights, const size_t start, const size_t finish) { vector<size_t> visited(n + 1, 0); visited[start] = start; deque<size_t> next; next.push_back(start); while (next.size() > 0 && next.front() != finish) { const size_t current = next.front(); next.pop_front(); for (size_t j = 1; j <= n; j++) { if (visited[j] == 0 && weights[current - 1][j - 1]) { visited[j] = current; next.push_back(j); } } } vector<size_t> path; uint32_t max_flow = MAX_FLOW + 1; for ( size_t current = finish; current != 0 && current != start; current = visited[current] ) { const size_t next = visited[current]; if (next != current && next != 0) { max_flow = min(max_flow, weights[next - 1][current - 1]); path.push_back(current); } } path = vector<size_t>(path.rbegin(), path.rend()); return make_pair(max_flow % (MAX_FLOW + 1), path); } int main() { size_t n; size_t m; cin >> n >> m; const size_t start = 1; const size_t finish = n; vector<vector<uint32_t>> weights(n, vector<uint32_t>(n, 0)); for (size_t i = 0; i < m; i++) { size_t a; size_t b; uint32_t c; cin >> a >> b >> c; weights[a - 1][b - 1] = c; } uint64_t flow = 0; while (true) { const auto result = find_path(n, weights, start, finish); const uint32_t current_flow = result.first; const vector<size_t> path = result.second; if (current_flow == 0) { break; } flow += current_flow; size_t previous = start; for (auto i : path) { weights[previous - 1][i - 1] -= current_flow; weights[i - 1][previous - 1] += current_flow; previous = i; } } cout << flow << endl; 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 |