이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) static_cast<int>((x).size())
template<class T, size_t D>
struct vec : vector<vec<T, D - 1>> {
template<class... Args>
vec(size_t n = 0, Args... args)
: vector<vec<T, D - 1>>(n, vec<T, D - 1>(args...)) {}
};
template<class T>
struct vec<T, 1> : vector<T> {
template<class... Args>
vec(Args... args)
: vector<T>(args...) {}
};
template<class T>
inline bool Minimize(T& a, const T& b) { return a > b ? a = b, true : false; }
template<class T>
inline bool Maximize(T& a, const T& b) { return a < b ? a = b, true : false; }
inline int Next(int i, int n) { return i == n - 1 ? 0 : i + 1; }
inline int Prev(int i, int n) { return !i ? n - 1 : i - 1; }
mt19937 rng(static_cast<uint32_t>(chrono::steady_clock::now().time_since_epoch().count()));
constexpr int kSmallInf = 1e9;
constexpr int64_t kBigInf = 1e18;
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int n_vertices, n_edges, n_items; cin >> n_vertices >> n_edges >> n_items;
vec<pair<int, int>, 2> price_items_vertices(n_vertices, n_items);
for (auto& price_items : price_items_vertices) {
for (auto& price_item : price_items) {
cin >> price_item.first >> price_item.second;
}
}
vec<int, 2> max_profit_pairs(n_vertices, n_vertices);
for (int i = 0; i < n_vertices; ++i) {
for (int j = 0; j < n_vertices; ++j) {
for (int k = 0; k < n_items; ++k) {
if (~price_items_vertices[i][k].first && ~price_items_vertices[j][k].second) {
Maximize(max_profit_pairs[i][j], - price_items_vertices[i][k].first + price_items_vertices[j][k].second);
}
}
}
}
vec<int, 2> min_distance_paths(n_vertices, n_vertices, kSmallInf);
for (int u = 0; u < n_vertices; ++u) {
min_distance_paths[u][u] = 0;
}
while (n_edges--) {
int u, v, w; cin >> u >> v >> w; --u; --v;
min_distance_paths[u][v] = w;
}
for (int k = 0; k < n_vertices; ++k) {
for (int i = 0; i < n_vertices; ++i) {
for (int j = 0; j < n_vertices; ++j) {
Minimize(min_distance_paths[i][j], min_distance_paths[i][k] + min_distance_paths[k][j]);
}
}
}
auto Check = [&](int efficiency) -> bool {
vec<int64_t, 2> f(n_vertices, n_vertices, -kBigInf);
for (int i = 0; i < n_vertices; ++i) {
for (int j = 0; j < n_vertices; ++j) {
if (min_distance_paths[i][j] != kSmallInf && i != j) {
f[i][j] = static_cast<int64_t>(max_profit_pairs[i][j]) - static_cast<int64_t>(efficiency) * min_distance_paths[i][j];
}
}
}
for (int k = 0; k < n_vertices; ++k) {
for (int i = 0; i < n_vertices; ++i) {
for (int j = 0; j < n_vertices; ++j) {
Maximize(f[i][j], f[i][k] + f[k][j]);
}
}
}
for (int k = 0; k < n_vertices; ++k) {
for (int i = 0; i < n_vertices; ++i) {
for (int j = 0; j < n_vertices; ++j) {
if (Maximize(f[i][j], f[i][k] + f[k][j])) {
return true;
}
}
}
}
for (int i = 0; i < n_vertices; ++i) {
if (f[i][i] >= 0) {
return true;
}
}
return false;
};
int left_answer = 0, right_answer = static_cast<int>(1e9);
while (left_answer < right_answer) {
int middle_answer = (left_answer + right_answer + 1) >> 1;
if (Check(middle_answer)) {
left_answer = middle_answer;
} else {
right_answer = middle_answer - 1;
}
}
cout << left_answer << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |