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;
typedef long long ll;
typedef long double ld;
#define FOR(i, a, b) for(ll i = (ll)a; i <= (ll)b; i++)
#define DEC(i, a, b) for(ll i = (ll)a; i >= (ll)b; i--)
typedef pair<ll, ll> pi;
typedef pair<pi, ll> pii;
typedef pair<pi, pi> pipi;
#define f first
#define s second
typedef vector<ll> vi;
typedef vector<pi> vpi;
typedef vector<pii> vpii;
#define pb push_back
#define pf push_front
#define all(v) v.begin(), v.end()
#define disc(v) sort(all(v)); v.resize(unique(all(v)) - v.begin());
#define INF (ll) 1e9 + 100
#define LLINF (ll) 1e18
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define sandybridge __attribute__((optimize("Ofast"), target("arch=sandybridge")))
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //can be used by calling rng() or shuffle(A, A+n, rng)
inline ll rand(ll x, ll y) { ++y; return (rng() % (y-x)) + x; } //inclusivesss
ll n, m, K, buy[105][1005], sell[105][1005], dist[105][105], earn[105][105];
bool check(ll x) {
ll diff[105][105]; FOR(i, 1, n) FOR(j, 1, n) diff[i][j] = LLINF;
FOR(i, 1, n) FOR(j, 1, n) if (dist[i][j] != LLINF) diff[i][j] = min(diff[i][j], dist[i][j] * x - earn[i][j]);
// how much you must earn to profit - how much you can earn
FOR(k, 1, n) FOR(i, 1, n) FOR(j, 1, n) diff[i][j] = min(diff[i][j], diff[i][k] + diff[k][j]);
FOR(i, 1, n) if (diff[i][i] <= 0) return 1;
return 0;
}
int main() {
fastio; cin >> n >> m >> K;
FOR(i, 1, n) FOR(k, 1, K) {
cin >> buy[i][k] >> sell[i][k];
if (buy[i][k] == -1) buy[i][k] = LLINF;
if (sell[i][k] == -1) sell[i][k] = -LLINF;
}
FOR(i, 1, n) FOR(j, 1, n) dist[i][j] = LLINF;
FOR(i, 1, m) {
ll a, b, c; cin >> a >> b >> c;
dist[a][b] = c;
}
FOR(k, 1, n) FOR(i, 1, n) FOR(j, 1, n) dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
FOR(i, 1, n) FOR(j, 1, n) FOR(k, 1, K) earn[i][j] = max(earn[i][j], sell[j][k] - buy[i][k]);
ll lower = 0, upper = 1000000001;
while (upper - lower > 1) {
ll mid = (upper + lower)/2;
if (check(mid)) lower = mid;
else upper = mid;
}
cout << lower;
}
# | 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... |