This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3", "unroll-loops") // "Ofast"
#pragma GCC target("avx2", "bmi", "bmi2", "lzcnt", "popcnt")
#include <bits/stdc++.h>
#define int long long
#define vt vector
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define ff first
#define ss second
#define dbg(x) cerr << #x << " = " << x << '\n'
#define bit(x, i) ((x) >> (i) & 1)
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vvt = vt< vt<ll> >;
const int N = 1e3 + 5, mod = 1e9 + 7, B = 500;
const ll inf = 1e18 + 7, LIM = (1ll << 60);
const double eps = 1e-6;
int n;
ll b[105][N], s[105][N], p[105][N];
void floyd (vvt &d) {
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) if (d[i][k] != inf && d[k][j] != inf) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
}
}
int m, k;
vvt d(105, vt<ll>(N, inf));
bool check (ll x) {
vvt g(105, vt<ll>(N, inf));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) if (i != j && d[i][j] != inf) g[i][j] = (x * d[i][j]) - p[i][j];
}
floyd(g);
for (int i = 1; i <= n; i++) if (g[i][i] <= 0) return true;
return false;
}
void solve () {
cin >> n >> m >> k;
for (int i = 1; i <= n; i++) {
d[i][i] = 0;
for (int j = 1; j <= k; j++) cin >> b[i][j] >> s[i][j];
}
for (int i = 1; i <= m; i++) {
ll x, y, w;
cin >> x >> y >> w;
d[x][y] = w;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
for (int x = 1; x <= k; x++) {
if (s[j][x] != -1 && b[i][x] != -1) p[i][j] = max(p[i][j], (s[j][x] - b[i][x]));
}
}
}
floyd(d);
ll l = 1, r = inf, ans = 0;
while (l <= r) {
ll mid = l + r >> 1;
if (check(mid)) ans = mid, l = mid + 1;
else r = mid - 1;
}
cout << ans;
cout << '\n';
}
bool testcases = 0;
signed main() {
#ifdef ONLINE_JUDGE
freopen(".in", "r", stdin);
freopen(".out", "w", stdout);
#endif
cin.tie(0) -> sync_with_stdio(0);
int test = 1;
if (testcases) cin >> test;
for (int cs = 1; cs <= test; cs++) {
solve();
}
}
Compilation message (stderr)
merchant.cpp: In function 'void solve()':
merchant.cpp:71:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
71 | ll mid = l + r >> 1;
| ~~^~~
# | 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... |