# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
200315 | BThero | Travelling Merchant (APIO17_merchant) | C++17 | 1099 ms | 1912 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.
// Why am I so dumb? :c
// chrono::system_clock::now().time_since_epoch().count()
#include<bits/stdc++.h>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
using namespace std;
//using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int, int> pii;
//template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int MAXN = (int)1e2 + 5;
const int MAXK = (int)1e3 + 5;
const ll LINF = (ll)1e17;
int buy[MAXN][MAXK], sell[MAXN][MAXK];
ll dp[MAXN][MAXK];
int adj[MAXN][MAXN];
int n, m, k;
bool check(int x) {
for (int st = 1; st <= n; ++st) {
for (int i = 1; i <= n; ++i) {
for (int j = 0; j <= k; ++j) {
dp[i][j] = -LINF;
}
}
for (int to = 1; to <= n; ++to) {
if (adj[st][to] == -1) {
continue;
}
ll w = adj[st][to] * 1ll * x;
dp[to][0] = -w;
for (int i = 1; i <= k; ++i) {
if (buy[st][i] != -1) {
dp[to][i] = -w - buy[st][i];
}
}
}
bool changed = 1;
int iter = 0;
while (changed && iter <= n) {
changed = 0;
++iter;
if (dp[st][0] >= 0) {
return 1;
}
for (int v = 1; v <= n; ++v) {
for (int i = 1; i <= k; ++i) {
if (dp[v][i] == -LINF || sell[v][i] == -1) {
continue;
}
if (dp[v][i] + sell[v][i] > dp[v][0]) {
dp[v][0] = dp[v][i] + sell[v][i];
changed = 1;
}
}
if (dp[v][0] != -LINF) {
for (int i = 1; i <= k; ++i) {
if (buy[v][i] != -1) {
if (dp[v][0] - buy[v][i] > dp[v][i]) {
dp[v][i] = dp[v][0] - buy[v][i];
changed = 1;
}
}
}
}
for (int i = 0; i <= k; ++i) {
if (dp[v][i] == -LINF) {
continue;
}
for (int to = 1; to <= n; ++to) {
if (adj[v][to] == -1) {
continue;
}
ll w = adj[v][to] * 1ll * x;
if (dp[v][i] - w > dp[to][i]) {
dp[to][i] = dp[v][i] - w;
changed = 1;
}
}
}
}
}
}
return 0;
}
void solve() {
scanf("%d %d %d", &n, &m, &k);
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= k; ++j) {
scanf("%d %d", &buy[i][j], &sell[i][j]);
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
adj[i][j] = -1;
}
}
for (int i = 1; i <= m; ++i) {
int u, v, w;
scanf("%d %d %d", &u, &v, &w);
adj[u][v] = w;
}
int l = 1, r = (int)1e9, ans = 0;
while (l <= r) {
int mid = (l + r) >> 1;
if (check(mid)) {
l = mid + 1;
ans = mid;
}
else {
r = mid - 1;
}
}
printf("%d\n", ans);
}
int main() {
int tt = 1;
while (tt--) {
solve();
}
return 0;
}
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... |