This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
#ifndef LOCAL
#define cerr if(false) cerr
#endif
#define endl "\n"
#define all(x) (x).begin(), (x).end()
#define forn(i, n) for(ll i = 0; i < n; i ++)
#define revn(i, n) for(ll i = n - 1; i >= 0; i --)
typedef long long ll;
template<class T, template<class T2, class A=allocator<T2> > class cont> inline ostream &operator <<(ostream &out, const cont<T> &x) { for(const auto &it : x) { out << it << " ";} return out;}
template<class T, template<class T2, class A=allocator<T2> > class cont> inline istream &operator >>(istream &in, cont<T> &x) { for(auto &it : x) { in >> it;} return in;}
template<class T, class T2> inline ostream &operator <<(ostream &out, const pair<T, T2> &x) { out << x.first << " " << x.second; return out;}
template<class T, class T2> inline istream &operator >>(istream &in, pair<T, T2> &x) { in >> x.first >> x.second; return in;}
template<class T, class T2> inline bool chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline bool chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const ll mod = 1e17 + 7;
const ll MAX_N = 110, MAX_K = 1010;
ll dist[MAX_N][MAX_N];
ll n, k, m;
ll buy[MAX_N][MAX_K], sell[MAX_N][MAX_K];
ll dist2[MAX_N][MAX_N];
void prec() {
for(ll k = 0; k < n; k ++) {
for(ll i = 0; i < n; i ++) {
for(ll j = 0; j < n; j ++) {
chkmin(dist[i][j], dist[i][k] + dist[k][j]);
}
}
}
}
bool hasNegative(ll x) {
for(ll i = 0; i < n; i ++) {
for(ll j = 0; j < n; j ++) {
ll best = 0;
for(ll w = 0; w < k; w ++) {
if(buy[i][w] != -1 && sell[j][w] != -1) {
chkmax(best, sell[j][w] - buy[i][w]);
}
}
if(mod / x < dist[i][j]) {
dist2[i][j] = mod;
} else {
dist2[i][j] = x * dist[i][j] - best;
}
}
}
for(ll k = 0; k < n; k ++) {
for(ll i = 0; i < n; i ++) {
for(ll j = 0; j < n; j ++) {
chkmin(dist2[i][j], dist2[i][k] + dist2[k][j]);
}
}
}
for(ll i = 0; i < n; i ++) {
if(dist2[i][i] < 0) {
return true;
}
}
return false;
}
signed main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
cin >> n >> m >> k;
for(ll i = 0; i < n; i ++) {
for(ll j = 0; j < n; j ++) {
dist[i][j] = mod;
}
}
for(ll i = 0; i < n; i ++) {
for(ll j = 0; j < k; j ++) {
cin >> buy[i][j] >> sell[i][j];
}
}
for(ll i = 0; i < m; i ++) {
ll a, b, c;
cin >> a >> b >> c;
chkmin(dist[a - 1][b - 1], c);
}
prec();
ll l = 0, r = mod;
while(l < r - 1) {
ll mid = (l + r) / 2ll;
if(!hasNegative(mid)) {
r = mid;
} else {
l = mid;
}
}
cout << l << endl;
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... |