Submission details
Task:Internet connection
Sender:sspilsbury
Submission time:2020-09-19 15:01:00 +0300
Language:C++ (C++11)
Status:READY
Result:
Test results
testverdicttime
#1--details
#2--details
#3ACCEPTED0.01 sdetails
#4--details
#5ACCEPTED0.01 sdetails
#6--details
#7--details
#8--details
#9--details
#10--details
#11ACCEPTED0.01 sdetails
#12ACCEPTED0.01 sdetails
#13ACCEPTED0.01 sdetails
#14ACCEPTED0.01 sdetails
#15ACCEPTED0.01 sdetails
#16ACCEPTED0.01 sdetails
#17ACCEPTED0.01 sdetails
#18ACCEPTED0.01 sdetails
#19ACCEPTED0.01 sdetails
#20ACCEPTED0.01 sdetails
#21--details
#22--details

Code

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

int main(void) {
  unsigned long R[100][100];

  int m, n;
  std::cin >> n;
  std::cin >> m;

  memset(R, 0, sizeof(unsigned long) * n * n);

  // Populate the matrix
  for (int i = 0; i < m; ++i) {
    long x, y, z;
    std::cin >> x;
    std::cin >> y;
    std::cin >> z;

    R[x - 1][y - 1] = z;

    // std::cout << x << " " << y << " " << R[x][y] << std::endl;
  }

  unsigned long flow = 0;

  // std::cout << "Got input" << std::endl;

  for (int j = 0; j < n; ++j) {
    std::vector<bool> visited(n);
    std::vector<int> parent(n);
    std::queue<int> q;

    q.push(0);

    // Try to find a path from 0 to n - 1
    while (!q.empty()) {
      int node = q.front();
      q.pop();

      // std::cout << "visit " << node << std::endl;

      // Is this the target node? break the loop
      if (node == n - 1) {
        visited[node] = true;
        break;
      }

      // Have we already visited this node?
      if (visited[node] == true) {
        continue;
      }

      // We've now visited this node
      visited[node] = true;

      // We haven't visited this node. Enumerate the node's children
      // by reference to the residual matrix
      for (int i = 0; i < n; ++i) {
        // std::cout << "check " << node << " " << i << " " << R[node][i] << std::endl;
        if (R[node][i] > 0) {
          parent[i] = node;
          q.push(i);
        }
      }
    }

    // No more paths, break out
    if (!visited[n - 1]) {
      break;
    }

    // We have found a path. Update the residual matrix
    // and add the flow from this path to the next path
    int x = n - 1;
    unsigned long w = std::numeric_limits<unsigned long>::max();
    while (x != 0) {
      // std::cout << "Link " << parent[x] << " " << x << R[parent[x]][x] << std::endl;
      w = std::min(w, R[parent[x]][x]);
      x = parent[x];
    }

    // Now that we know the flow, we need to subtract that from the
    // residual matrix and add it back to the reverse edges
    x = n - 1;
    while (x != 0) {
      R[parent[x]][x] -= w;
      R[x][parent[x]] += w;
      x = parent[x];
    }

    // Add this flow to the maximum flow
    flow += w;
  }

  std::cout << flow << std::endl;
}

Test details

Test 1

Verdict:

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

correct output
73

user output
(empty)

Test 2

Verdict:

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

correct output
110

user output
(empty)

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:

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

correct output
95

user output
(empty)

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:

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

correct output
4397669179

user output
(empty)

Test 7

Verdict:

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

correct output
5298558023

user output
(empty)

Test 8

Verdict:

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

correct output
5466229311

user output
(empty)

Test 9

Verdict:

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

correct output
4893925638

user output
(empty)

Test 10

Verdict:

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

correct output
6956499595

user output
(empty)

Test 11

Verdict: ACCEPTED

input
2 1
1 2 1

correct output
1

user output
1

Test 12

Verdict: ACCEPTED

input
2 1
2 1 1

correct output
0

user output
0

Test 13

Verdict: ACCEPTED

input
2 2
1 2 1
2 1 1

correct output
1

user output
1

Test 14

Verdict: ACCEPTED

input
100 1000
1 2 539540023
2 3 244306651
3 4 253259012
3 5 630461598
...

correct output
0

user output
0

Test 15

Verdict: ACCEPTED

input
4 5
1 2 2
1 3 5
2 4 3
3 2 2
...

correct output
4

user output
4

Test 16

Verdict: ACCEPTED

input
2 0

correct output
0

user output
0

Test 17

Verdict: ACCEPTED

input
100 0

correct output
0

user output
0

Test 18

Verdict: ACCEPTED

input
100 196
1 2 1000000000
2 100 1000000000
1 3 1000000000
3 100 1000000000
...

correct output
98000000000

user output
98000000000

Test 19

Verdict: ACCEPTED

input
100 99
1 2 1000000000
2 3 1000000000
3 4 1000000000
4 5 1000000000
...

correct output
1000000000

user output
1000000000

Test 20

Verdict: ACCEPTED

input
2 2
2 1 1
1 2 1

correct output
1

user output
1

Test 21

Verdict:

input
4 6
1 2 1000000000
1 3 1000000000
2 3 1
2 4 1000000000
...

correct output
2000000000

user output
(empty)

Test 22

Verdict:

input
4 6
1 2 1000000000
1 3 1000000000
2 4 1000000000
2 3 1
...

correct output
2000000000

user output
(empty)