# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
698890 | nguyentunglam | Travelling Merchant (APIO17_merchant) | C++17 | 35 ms | 1996 KiB |
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 N = 110;
int s[N][N], b[N][N];
int n, m, k;
int cnt[2][N][N];
long long f[2][N][N];
bool in[2][N][N];
queue<tuple<int, int, int> > q;
vector<pair<int, int> > adj[N];
bool check(int mid) {
for(int i = 1; i <= 1; 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]++;
if (cnt[exit][carry][u] >= 5) 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 (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 (f[_e][carry][v] < f[exit][carry][u] - w * mid) {
f[_e][carry][v] = f[exit][carry][u] - 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)
# | 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... |