This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#line 1 "main.cpp"
/**
* @title Template
*/
#include <iostream>
#include <algorithm>
#include <utility>
#include <numeric>
#include <vector>
#include <array>
#include <queue>
#include <cassert>
#line 2 "/Users/kodamankod/Desktop/Programming/Library/other/chmin_chmax.cpp"
template <class T, class U>
constexpr bool chmin(T &lhs, const U &rhs) {
if (lhs > rhs) {
lhs = rhs;
return true;
}
return false;
}
template <class T, class U>
constexpr bool chmax(T &lhs, const U &rhs) {
if (lhs < rhs) {
lhs = rhs;
return true;
}
return false;
}
/**
* @title Chmin/Chmax
*/
#line 2 "/Users/kodamankod/Desktop/Programming/Library/other/range.cpp"
#line 4 "/Users/kodamankod/Desktop/Programming/Library/other/range.cpp"
class range {
public:
class iterator {
private:
int64_t M_position;
public:
constexpr iterator(int64_t position) noexcept: M_position(position) { }
constexpr void operator ++ () noexcept { ++M_position; }
constexpr bool operator != (iterator other) const noexcept { return M_position != other.M_position; }
constexpr int64_t operator * () const noexcept { return M_position; }
};
class reverse_iterator {
private:
int64_t M_position;
public:
constexpr reverse_iterator(int64_t position) noexcept: M_position(position) { }
constexpr void operator ++ () noexcept { --M_position; }
constexpr bool operator != (reverse_iterator other) const noexcept { return M_position != other.M_position; }
constexpr int64_t operator * () const noexcept { return M_position; }
};
private:
const iterator M_first, M_last;
public:
constexpr range(int64_t first, int64_t last) noexcept: M_first(first), M_last(std::max(first, last)) { }
constexpr iterator begin() const noexcept { return M_first; }
constexpr iterator end() const noexcept { return M_last; }
constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(*M_last - 1); }
constexpr reverse_iterator rend() const noexcept { return reverse_iterator(*M_first - 1); }
};
/**
* @title Range
*/
#line 2 "/Users/kodamankod/Desktop/Programming/Library/other/rev.cpp"
#include <type_traits>
#include <iterator>
#line 6 "/Users/kodamankod/Desktop/Programming/Library/other/rev.cpp"
template <class T>
class rev_impl {
public:
using iterator = typename std::decay<T>::type::reverse_iterator;
private:
const iterator M_begin;
const iterator M_end;
public:
constexpr rev_impl(T &&cont) noexcept: M_begin(std::rbegin(cont)), M_end(std::rend(cont)) { }
constexpr iterator begin() const noexcept { return M_begin; }
constexpr iterator end() const noexcept { return M_end; }
};
template <class T>
constexpr decltype(auto) rev(T &&cont) {
return rev_impl<T>(std::forward<T>(cont));
}
/**
* @title Reverser
*/
#line 18 "main.cpp"
using i32 = int32_t;
using i64 = int64_t;
using u32 = uint32_t;
using u64 = uint64_t;
constexpr i32 inf32 = (i32(1) << 30) - 1;
constexpr i64 inf64 = (i64(1) << 62) - 1;
template <class T>
void warshall_floyd(std::vector<std::vector<T>> &vec) {
i32 N = vec.size();
for (auto i: range(0, N)) {
for (auto j: range(0, N)) {
for (auto k: range(0, N)) {
chmin(vec[j][k], vec[j][i] + vec[i][k]);
}
}
}
}
int main() {
i32 N, M, K;
std::cin >> N >> M >> K;
std::vector<std::vector<i32>> buy(N, std::vector<i32>(K));
std::vector<std::vector<i32>> sell(N, std::vector<i32>(K));
for (auto i: range(0, N)) {
for (auto k: range(0, K)) {
std::cin >> buy[i][k] >> sell[i][k];
}
}
std::vector<std::vector<i32>> cost(N, std::vector<i32>(N, inf32));
for (auto i: range(0, M)) {
i32 u, v, c;
std::cin >> u >> v >> c;
--u; --v;
chmin(cost[u][v], c);
}
warshall_floyd(cost);
for (auto i: range(0, N)) {
cost[i][i] = inf32;
}
std::vector<std::vector<i32>> gain(N, std::vector<i32>(N));
for (auto i: range(0, N)) {
for (auto j: range(0, N)) {
if (cost[i][j] == inf32) {
continue;
}
for (auto k: range(0, K)) {
if (sell[j][k] != -1 && buy[i][k] != -1) {
chmin(gain[i][j], buy[i][k] - sell[j][k]);
}
}
}
}
i64 ok = 0, ng = (i64) inf32 * 100;
while (ng - ok > 1) {
i64 md = (ok + ng) / 2;
std::vector<std::vector<i64>> dist(N, std::vector<i64>(N, inf64));
for (auto i: range(0, N)) {
for (auto j: range(0, N)) {
if (cost[i][j] != inf32) {
dist[i][j] = md * cost[i][j] + gain[i][j];
}
}
}
warshall_floyd(dist);
bool flag = false;
for (auto i: range(0, N)) {
if (dist[i][i] <= 0) {
flag = true;
break;
}
}
(flag ? ok : ng) = md;
}
std::cout << ok << '\n';
return 0;
}
Compilation message (stderr)
main.cpp: In function 'int main()':
main.cpp:50:13: warning: unused variable 'i' [-Wunused-variable]
# | 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... |