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>
#define fi first
#define se second
#define endl "\n"
#define ii pair<int, int>
using namespace std;
const int M = 1010, N = 110;
int s[N][N], b[N][N];
int n, m, k;
int cnt[2][M][N];
long long f[2][M][N];
bool in[2][M][N];
queue<tuple<int, int, int> > q;
vector<pair<int, int> > adj[N];
bool check(int mid) {
for(int i = 1; i <= n; i++) {
memset(f, -127, sizeof(f));
memset(cnt, 0, sizeof(cnt));
f[0][0][i] = 0;
q.push({0, 0, i});
while (!q.empty()) {
auto [exit, carry, u] = q.front(); q.pop();
in[exit][carry][u] = 0;
cnt[exit][carry][u]++;
// cerr << exit << " " << carry << " " << u << " " << f[exit][carry][u] << endl;
if (cnt[exit][carry][u] >= 20) return 1;
if (carry) {
if (s[u][carry] != -1 && f[exit][0][u] < f[exit][carry][u] + s[u][carry]) {
f[exit][0][u] = f[exit][carry][u] + s[u][carry];
if (!in[exit][0][u]) q.push({exit, 0, u}), in[exit][0][u] = 1;
}
}
if (!carry) {
for(int j = 1; j <= k; j++) {
// if (exit == 1 && carry == 0 && u == 4) cerr << b[u][j] << " " << f[exit][carry][u] - b[u][j] << endl;
if (b[u][j] != -1 && f[exit][j][u] < f[exit][carry][u] - b[u][j]) {
f[exit][j][u] = f[exit][carry][u] - b[u][j];
if (!in[exit][j][u]) q.push({exit, j, u}), in[exit][j][u] = 1;
}
}
}
for(auto &[v, w] : adj[u]) {
int _e = exit | 1;
// if (_e && carry == 1 && v == 4) cerr << u << " " << f[exit][carry][u] << endl;
if (f[_e][carry][v] < f[exit][carry][u] - 1LL * w * mid) {
f[_e][carry][v] = f[exit][carry][u] - 1LL * w * mid;
if (!in[_e][carry][v]) q.push({_e, carry, v}), in[exit][carry][v] = 1;
}
}
}
if (f[1][0][i] >= 0) return 1;
}
return 0;
}
int main() {
#define task ""
cin.tie(0) -> sync_with_stdio(0);
if (fopen ("task.inp", "r")) {
freopen ("task.inp", "r", stdin);
freopen ("task.out", "w", stdout);
}
if (fopen (task".inp", "r")) {
freopen (task".inp", "r", stdin);
freopen (task".out", "w", stdout);
}
cin >> n >> m >> k;
for(int i = 1; i <= n; i++) for(int j = 1; j <= k; j++) cin >> b[i][j] >> s[i][j];
while (m--) {
int u, v, w; cin >> u >> v >> w;
adj[u].emplace_back(v, w);
}
int l = 1, r = 1e9, res = 0;
while (l <= r) {
int mid = l + r >> 1;
if (check(mid)) {
l = mid + 1;
res = mid;
}
else r = mid - 1;
}
cout << res;
}
Compilation message (stderr)
merchant.cpp: In function 'int main()':
merchant.cpp:74:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
74 | int mid = l + r >> 1;
| ~~^~~
merchant.cpp:59:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
59 | freopen ("task.inp", "r", stdin);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
merchant.cpp:60:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
60 | freopen ("task.out", "w", stdout);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
merchant.cpp:63:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
63 | freopen (task".inp", "r", stdin);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
merchant.cpp:64:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
64 | freopen (task".out", "w", stdout);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |