Submission details
Task:Download Speed
Sender:ileska
Submission time:2025-10-20 11:30:02 +0300
Language:C++ (C++20)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#3ACCEPTED0.00 sdetails
#4ACCEPTED0.00 sdetails
#5ACCEPTED0.01 sdetails
#6ACCEPTED0.01 sdetails
#7ACCEPTED0.01 sdetails
#8ACCEPTED0.00 sdetails
#9ACCEPTED0.00 sdetails
#10ACCEPTED0.00 sdetails
#11ACCEPTED0.00 sdetails
#12ACCEPTED0.00 sdetails

Code

#include <bits/stdc++.h>
#define ll long long

template <typename T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
  os << "[";
  for (int i = 0; i < v.size(); ++i) {
    os << v[i];
    if (i != v.size() - 1)
      os << ", ";
  }
  os << "]";
  return os;
}

ll something(std::vector<std::unordered_map<ll, ll>> &costMap,
             std::vector<char> &checked, std::vector<ll> &path, ll start,
             ll wholeEnd) {
  path.push_back(start);
  if (start == wholeEnd) {
    return 1e15;
  }
  checked[start] = 1;

  for (const auto &[end, speed] : costMap[start]) {
    if (speed > 0 && !checked[end]) {
      ll ret = something(costMap, checked, path, end, wholeEnd);
      if (ret != -1) {
        return std::min(ret, costMap[start][end]);
      }
    }
  }
  path.pop_back();
  return -1;
}

ll bfs(std::vector<std::unordered_map<ll, ll>> &costMap, ll start, ll end,
       ll compCount, std::vector<ll> &path) {
  std::queue<ll> qq;
  std::vector<char> checked(compCount, 0);

  qq.push(start);
  checked[start] = 1;
  while (!qq.empty()) {
    ll cur = qq.front();
    qq.pop();

    for (const auto &[next, speed] : costMap[cur]) {
      if (!checked[next] && speed > 0) {
        qq.push(next);
        checked[next] = 1;
        path[next] = cur;
        if (next == end) {
          return 1;
        }
      }
    }
  }
  return checked[end];
}

int main() {
  ll compCount, conCount;
  std::cin >> compCount;
  std::cin >> conCount;

  //   std::cout << compCount << " " << conCount << std::endl;
  std::vector<std::unordered_map<ll, ll>> costMap(compCount,
                                                  std::unordered_map<ll, ll>());
  for (ll ii = 0; ii < conCount; ii++) {
    ll start, end, speed;
    // std::cout << start << " " << end << " " << speed << std::endl;
    std::cin >> start;
    std::cin >> end;
    std::cin >> speed;
    // if (end < start) {
    //   ll tmp = start;
    //   start = end;
    //   end = tmp;
    //   speed = -speed;
    // }

    if (costMap[start - 1].contains(end - 1)) {
      costMap[start - 1][end - 1] += speed;
    } else {
      costMap[start - 1][end - 1] = speed;
    }
    if (!costMap[end - 1].contains(start - 1)) {
      costMap[end - 1][start - 1] = 0;
    }
  }
  ll minSpeed = 0;
  ll throughtPut = 0;
  ll start = 0;
  ll end = compCount - 1;
  std::vector<char> checked(compCount, 0);
  while (1) {
    std::vector<ll> path(compCount, -1);
    ll success = bfs(costMap, start, end, compCount, path);
    if (!success) {
      break;
    }
    minSpeed = 1e15;

    ll ss = end;
    // std::cout << path << std::endl;
    while (ss != start) {
      minSpeed = std::min(minSpeed, costMap[path[ss]][ss]);
      ss = path[ss];
    }

    throughtPut += minSpeed;
    // std::cout << minSpeed << std::endl;

    ll vv = end;
    while (vv != start) {
      ll uu = path[vv];
      costMap[uu][vv] -= minSpeed;
      costMap[vv][uu] += minSpeed;
      vv = uu;
    }
  }
  std::cout << throughtPut << std::endl;
}

Test details

Test 1

Verdict: ACCEPTED

input
4 3
1 2 5
2 3 3
3 4 6

correct output
3

user output
3

Test 2

Verdict: ACCEPTED

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

correct output
2

user output
2

Test 3

Verdict: ACCEPTED

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

correct output
2000000000

user output
2000000000

Test 4

Verdict: ACCEPTED

input
2 1
2 1 100

correct output
0

user output
0

Test 5

Verdict: ACCEPTED

input
2 1000
1 2 1000000000
1 2 1000000000
1 2 1000000000
1 2 1000000000
...

correct output
1000000000000

user output
1000000000000

Test 6

Verdict: ACCEPTED

input
500 998
1 2 54
1 3 59
1 4 83
2 5 79
...

correct output
60

user output
60

Test 7

Verdict: ACCEPTED

input
500 998
1 2 530873053
1 3 156306296
1 4 478040476
3 5 303609600
...

correct output
1093765123

user output
1093765123

Test 8

Verdict: ACCEPTED

input
2 1
1 2 1

correct output
1

user output
1

Test 9

Verdict: ACCEPTED

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

correct output
6

user output
6

Test 10

Verdict: ACCEPTED

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

correct output
3

user output
3

Test 11

Verdict: ACCEPTED

input
10 999
1 2 1000000000
1 2 1000000000
1 2 1000000000
1 2 1000000000
...

correct output
111000000000

user output
111000000000

Test 12

Verdict: ACCEPTED

input
7 9
1 2 1
1 3 1
1 4 1
2 5 1
...

correct output
2

user output
2