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;
const int Inf = 1e9;
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
int n, m, k;
cin >> n >> m >> k;
vector <vector <int>> s(n, vector <int> (k)), b(n, vector <int> (k));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < k; ++j) {
cin >> b[i][j] >> s[i][j];
}
}
vector <vector <int>> d(n, vector <int> (n, Inf));
for (int i = 0; i < m; ++i) {
int u, v, x;
cin >> u >> v >> x;
--u, --v;
d[u][v] = x;
}
for (int k = 0; k < n; ++k) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
}
}
vector <vector <int>> best(n, vector <int> (n, 0));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
for (int id = 0; id < k; ++id) {
if (s[j][id] != -1 && b[i][id] != -1) best[i][j] = max(best[i][j], s[j][id] - b[i][id]);
}
}
}
vector <vector <ll>> w(n, vector <ll> (n));
int l = 0, r = 1e9 + 7;
while (r - l > 1) {
int m = (l + r) >> 1;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
w[i][j] = best[i][j] - (ll)m * d[i][j];
}
}
for (int i = 0; i < n; ++i) assert(w[i][i] < 0);
bool fl = false;
for (int k = 0; k < n; ++k) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
w[i][j] = max(w[i][j], w[i][k] + w[k][j]);
}
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (j != i && w[i][j] + w[j][i] >= 0) fl = true;
}
}
bool fnd = false;
for (int i = 0; i < n; ++i) if (w[i][i] >= 0) fnd = true;
//if (!fl && fnd) assert(false);
if (fl) l = m;
else r = m;
}
cout << l << '\n';
}
Compilation message (stderr)
merchant.cpp: In function 'int main()':
merchant.cpp:66:10: warning: variable 'fnd' set but not used [-Wunused-but-set-variable]
bool fnd = false;
^~~
# | 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... |