Task: | Download Speed |
Sender: | fabiank |
Submission time: | 2024-10-21 14:25:34 +0300 |
Language: | C++ (C++17) |
Status: | READY |
Result: | TIME LIMIT EXCEEDED |
test | verdict | time | |
---|---|---|---|
#1 | ACCEPTED | 0.00 s | details |
#2 | ACCEPTED | 0.00 s | details |
#3 | ACCEPTED | 0.00 s | details |
#4 | ACCEPTED | 0.00 s | details |
#5 | TIME LIMIT EXCEEDED | -- | details |
#6 | ACCEPTED | 0.00 s | details |
#7 | ACCEPTED | 0.00 s | details |
#8 | ACCEPTED | 0.00 s | details |
#9 | ACCEPTED | 0.00 s | details |
#10 | ACCEPTED | 0.00 s | details |
#11 | TIME LIMIT EXCEEDED | -- | details |
#12 | ACCEPTED | 0.00 s | details |
Compiler report
input/code.cpp: In function 'int ford_fulkerson(std::vector<std::vector<std::pair<int, int> > >&, int, int, int)': input/code.cpp:146:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare] 146 | for (int i = 1; i < path_reversed.size(); i++) | ~~^~~~~~~~~~~~~~~~~~~~~~
Code
#include <bits/stdc++.h> #define REP(i, a, b) for (int i = a; i < b; i++) // Type Aliases for 1D and 2D vectors with initialization #define vll(n, val) vector<long long>(n, val) // 1D vector of long longs with size n, initialized to val #define ll long long #define vvi(n, m, val) vector<vector<int>>(n, vector<int>(m, val)) // 2D vector of ints (n x m), initialized to val #define vvll(n, m, val) vector<vector<long long>>(n, vector<long long>(m, val)) // 2D vector of long longs (n x m), initialized to val using namespace std; void print_vector(vector<int> &x) { for (int v : x) { cout << v << " "; } cout << "\n"; } void print_matrix(vector<vector<int>> &matrix) { cout << "\n" << "----------------" << "\n"; for (vector<int> row : matrix) { print_vector(row); } cout << "\n" << "----------------" << "\n"; } int calc_max_digit(int n) { int max_digit = 0; while (n > 0 && max_digit < 9) { int digit = n % 10; if (digit > max_digit) { max_digit = digit; } n /= 10; } return max_digit; } // edges as edge list for outgoing node as pairs (end, cost) vector<ll> dijkstras(int start_point, vector<vector<pair<int, int>>> edges) { int n = edges.size(); vector<bool> processed(n, false); vector<ll> distances(n, LLONG_MAX); distances[start_point] = 0; priority_queue<pair<ll, int>> pq; pq.push({0, start_point}); while (!pq.empty()) { int curr = pq.top().second; pq.pop(); if (processed[curr]) { continue; } processed[curr] = true; ll distance = distances[curr]; for (pair<int, int> edge : edges[curr]) { if (distance + edge.second < distances[edge.first]) { distances[edge.first] = distance + edge.second; pq.push({-distances[edge.first], edge.first}); } } } return distances; } int bfs_edmondson_karp(const vector<vector<pair<int, int>>> &connections, const int source, const int target, vector<int> &path_reversed) { int n = connections.size(); queue<pair<int, int>> queue; queue.push({source, INT_MAX}); vector<int> predecessor(n, -2); predecessor[source] = -1; while (!queue.empty()) { int current = queue.front().first; int current_bottleneck = queue.front().second; queue.pop(); // cout << "BFS - Current node: " << current << endl; if (current == target) { while (current != -1) { path_reversed.push_back(current); current = predecessor[current]; } // cout << "Found path of capacity" << current_bottleneck << endl; return current_bottleneck; } for (pair<int, int> neigh : connections[current]) { int edge_end = neigh.first, edge_cap = neigh.second; if (edge_cap > 0 && predecessor[edge_end] == -2) { predecessor[edge_end] = current; queue.push({edge_end, min(current_bottleneck, edge_cap)}); } } } return -1; } int ford_fulkerson(vector<vector<pair<int, int>>> &residual_graph, const int source, const int target, int starting_capacity) { int n = residual_graph.size(); int flow = 0; bool augmenting_path_exists = true; while (augmenting_path_exists) { vector<int> path_reversed; vector<bool> visited(n, false); // cout << "Starting BFS" << endl; int path_capacity = bfs_edmondson_karp(residual_graph, source, target, path_reversed); // cout << "Result " << path_capacity << endl; if (path_capacity < 0) { augmenting_path_exists = false; break; } flow += path_capacity; for (int i = 1; i < path_reversed.size(); i++) { int edge_end = path_reversed[i - 1]; int edge_start = path_reversed[i]; // reduce forwards edge for (pair<int, int> &edge : residual_graph[edge_start]) { if (edge.first == edge_end) { edge.second -= path_capacity; break; } } // add to backwards edge for (pair<int, int> &edge : residual_graph[edge_end]) { if (edge.first == edge_start) { edge.second += path_capacity; break; } } } } return flow; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; vector<vector<pair<int, int>>> connections(n + 1, vector<pair<int, int>>(0)); int max_capacity = 0; for (int i = 1; i <= m; i++) { int start, end, capacity; cin >> start >> end >> capacity; connections[start].push_back({end, capacity}); connections[end].push_back({start, 0}); if (capacity > max_capacity) { max_capacity = capacity; } } cout << ford_fulkerson(connections, 1, n, max_capacity) << 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: TIME LIMIT EXCEEDED
input |
---|
2 1000 1 2 1000000000 1 2 1000000000 1 2 1000000000 1 2 1000000000 ... |
correct output |
---|
1000000000000 |
user output |
---|
(empty) |
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: TIME LIMIT EXCEEDED
input |
---|
10 999 1 2 1000000000 1 2 1000000000 1 2 1000000000 1 2 1000000000 ... |
correct output |
---|
111000000000 |
user output |
---|
(empty) |
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 |