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;
#define ll long long
#define pb push_back
#define pii pair <ll, ll>
#define ff first
#define ss second
#define sz size()
const int N = 1e2 + 5;
ll baha[N], vis[N], buy[N][N], sell[N][N], n, m, d, dis[N][N], profit[N][N];
vector <ll> nodes;
vector <pii> v[N];
ll node, val, new_jog;
ll ugrat () {
ll ret = 0;
for (int i = 1; i < (int)nodes.sz; ++i) {
for (int j = 0; j < i; ++j) {
ll x = nodes[j], y = nodes[i];
baha[i] = max (baha[i], baha[j] + profit[x][y]);
}
ret = max (ret, baha[i]);
}
for (int i = 0; i < (int)nodes.sz; ++i) {
baha[i] = 0;
}
return ret;
}
void dfs (ll x, ll wagt) {
if (vis[x]) {
if (x == node) {
nodes.pb(x);
ll jog = ugrat ();
jog -= val * wagt;
if (jog >= 0) new_jog = 1;
nodes.pop_back();
}
return;
}
vis[x] = 1;
nodes.pb (x);
for (pii i : v[x]) {
dfs (i.ff, wagt + i.ss);
}
vis[x] = 0;
nodes.pop_back();
}
bool check (ll x) {
// jog - x * time >= 0
for (ll i = 1; i <= n; ++i) {
node = i;val = x;
new_jog = 0;
dfs (node, 0);
if (new_jog) return 1;
}
return 0;
}
int main () {
// freopen ("input.txt", "r", stdin);
cin >> n >> m >> d;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= d; ++j) {
cin >> buy[i][j] >> sell[i][j];
}
}
for (int i = 1; i <= m; ++i) {
ll l, r, w;
cin >> l >> r >> w;
v[l].pb ({r, w});
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (i == j) continue;
for (int k = 1; k <= d; ++k) {
// k-th item-y i-njiden satyn alyan j-nynjyda satyan
if (sell[j][k] != -1 and buy[i][k] != -1)
profit[i][j] = max (profit[i][j], sell[j][k] - buy[i][k]);
}
}
}
ll l = 0, r = 1e13, jog = 0;
while (l <= r) {
ll md = (l + r) / 2;
if (check (md)) {
jog = md;
l = md + 1;
}
else {
r = md - 1;
}
}
cout << jog;
}
# | 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... |