| Task: | Airport |
| Sender: | aalto25h_007 |
| Submission time: | 2025-10-22 16:57:13 +0300 |
| Language: | C++ (C++20) |
| Status: | READY |
| Result: | ACCEPTED |
| 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 | ACCEPTED | 0.00 s | details |
| #6 | ACCEPTED | 0.01 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 | ACCEPTED | 0.00 s | details |
| #12 | ACCEPTED | 0.00 s | details |
| #13 | ACCEPTED | 0.00 s | details |
Code
// clang-format off
#include <bits/stdc++.h>
#include <optional>
using namespace std;
#ifdef DO_DBG
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#define dout cerr
#define debug true
#else
#define dbg(...)
#define dout if (0) cerr
#define debug false
#endif
typedef long long ll; typedef unsigned long long ull; typedef long double ld;
template <typename T> using v = vector<T>; template <typename T> using us = unordered_set<T>; template <typename K, typename V> using p = pair<K,V>;
template <typename K, typename V> using um = unordered_map<K, V>; template <typename K, typename V> using p = pair<K, V>;
template <typename T> using pq = priority_queue<T>; template <typename T> using nl = numeric_limits<T>; template <typename T> using il = initializer_list<T>;
constexpr int MOD = 1e9 + 7; constexpr int INF = 1e9; constexpr ld EPS = 1e-9;
#define fr(i,a,b) for (size_t i = a; i < b; i++)
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin.exceptions(cin.failbit);
#define frr(i,a,b) for (size_t i = b-1; i >= a; i--)
#define frs(i,a,b) for (ll i = a; i < b; i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define sq(x) ((x) * (x))
template <typename... Args> void read(Args&... args) { ((cin >> args), ...); }
#define d(...) int __VA_ARGS__; read(__VA_ARGS__);
#define dll(...) ll __VA_ARGS__; read(__VA_ARGS__);
#define dv(x, n) vector<int> x(n); for (int i = 0; i < n; i++) cin >> x[i];
#define dvll(x, n) vector<long long> x(n); for (int i = 0; i < n; i++) cin >> x[i];
#define dvd(x, n) vector<double> x(n); for (int i = 0; i < n; i++) cin >> x[i];
ll ipow(ll a,int b){ll r=1;for(;b;b>>=1,a*=a)if(b&1)r*=a;return r;}
template<class F>struct y_combinator{F f;template<class...Args>decltype(auto)operator()(Args&&...args)const{return f(*this,forward<Args>(args)...);}};
constexpr auto get_nums=[]<typename T>(T&&s){istringstream iss(forward<T>(s));return vector<int>{istream_iterator<int>{iss},{}};};
// clang-format on
struct edge {
ll to, w;
size_t rev;
};
constexpr auto inf = nl<ll>::max();
int main() {
fastio;
dll(n, m);
dvll(r, n);
for (auto &r1 : r) {
if (r1 == -1)
r1 = inf;
}
v<v<edge>> adj(2 * n); // first half is in, second half is out
ll threshold = inf;
while (m--) {
dll(a, b);
a--;
b--;
// corridor goes from a to b
// or, from a_out to b_in
// and reverse is from b_in to a_out
adj[a + n].push_back({b, inf, adj[b].size()});
adj[b].push_back({a + n, 0, adj[a + n].size() - 1});
}
frs(i, 0, n) {
// from i_in to i_out
adj[i].push_back({i + n, r[i], adj[i + n].size()});
// reverse
adj[i + n].push_back({i, 0, adj[i].size() - 1});
}
ll found_min_w = 0;
ll total_flow = 0;
do {
v<bool> vis(2 * n);
// if we just want to calculate min_w we could do bottom up
// but since we want to know the min_flow before bubbling up
// (because we want to decrease w) we pass the flow from the top
// to the bottom
// this also allows us to simplify the base case a bit, since we can recurse
// into the last node
found_min_w = y_combinator([&](auto dfs, ll node, ll flow_td) -> ll {
if (node == n - 1)
return flow_td;
vis[node] = true;
for (auto &[c, w, rev] : adj[node]) {
if (w >= threshold && !vis[c]) {
auto new_flow = dfs(c, min(w, flow_td));
if (new_flow) {
// we found a path
// we know new_flow is the minimum weight, since
// w was considered for each edge when recursing top down
w -= new_flow;
// increment the reverse edge
// find_if(all(adj[c]), [node](auto &p) {
// return p.first == node;
// })->second += new_flow;
adj[c][rev].w += new_flow;
return new_flow;
}
}
}
return 0;
})(0, inf);
total_flow += found_min_w;
if (!found_min_w)
threshold /= 2;
// scaling algorithm ^
} while (threshold);
cout << total_flow << "\n";
}
Test details
Test 1
Verdict: ACCEPTED
| input |
|---|
| 10 20 -1 85 7 19 90 72 11 46 65 -1 6 7 9 7 8 4 ... |
| correct output |
|---|
| 7 |
| user output |
|---|
| 7 |
Test 2
Verdict: ACCEPTED
| input |
|---|
| 10 20 -1 80 77 57 77 95 63 98 30 -1 6 7 8 9 7 8 ... |
| correct output |
|---|
| 30 |
| user output |
|---|
| 30 |
Test 3
Verdict: ACCEPTED
| input |
|---|
| 10 20 -1 63 16 42 62 70 9 94 68 -1 10 9 6 8 10 6 ... |
| correct output |
|---|
| 25 |
| user output |
|---|
| 25 |
Test 4
Verdict: ACCEPTED
| input |
|---|
| 10 20 -1 3 86 -1 32 34 9 50 -1 -1 6 7 7 8 9 2 ... |
| correct output |
|---|
| 3 |
| user output |
|---|
| 3 |
Test 5
Verdict: ACCEPTED
| input |
|---|
| 10 20 -1 43 38 -1 7 54 26 97 76 -1 3 9 9 10 6 7 ... |
| correct output |
|---|
| 76 |
| user output |
|---|
| 76 |
Test 6
Verdict: ACCEPTED
| input |
|---|
| 100 1000 -1 425576195 274150382 1021768... |
| correct output |
|---|
| 6091126630 |
| user output |
|---|
| 6091126630 |
Test 7
Verdict: ACCEPTED
| input |
|---|
| 100 1000 -1 769953265 -1 389517741 2323... |
| correct output |
|---|
| 769953265 |
| user output |
|---|
| 769953265 |
Test 8
Verdict: ACCEPTED
| input |
|---|
| 100 1000 -1 584988267 763129662 6781413... |
| correct output |
|---|
| 1699511766 |
| user output |
|---|
| 1699511766 |
Test 9
Verdict: ACCEPTED
| input |
|---|
| 100 1000 -1 921671366 121044688 2933366... |
| correct output |
|---|
| 1805833567 |
| user output |
|---|
| 1805833567 |
Test 10
Verdict: ACCEPTED
| input |
|---|
| 100 1000 -1 763842763 612011030 4532521... |
| correct output |
|---|
| 3342235784 |
| user output |
|---|
| 3342235784 |
Test 11
Verdict: ACCEPTED
| input |
|---|
| 3 3
-1 1 -1 1 2 2 3 2 2 |
| correct output |
|---|
| 1 |
| user output |
|---|
| 1 |
Test 12
Verdict: ACCEPTED
| input |
|---|
| 3 4
-1 1 -1 1 2 1 2 2 3 ... |
| correct output |
|---|
| 1 |
| user output |
|---|
| 1 |
Test 13
Verdict: ACCEPTED
| input |
|---|
| 7 8 -1 1 1 1 1 1 -1 1 2 1 3 2 4 ... |
| correct output |
|---|
| 1 |
| user output |
|---|
| 1 |
