# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
107753 | Just_Solve_The_Problem | Travelling Merchant (APIO17_merchant) | C++11 | 131 ms | 3452 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 ll long long
using namespace std;
const int N = (int)1e2 + 7;
const ll inf = (ll)1e18;
int n, m, k;
vector<vector<int>> S, B;
ll dist[N][N], opt[N][N], dp[N][N];
bool check(int x) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (dist[i][j] == inf) {
dp[i][j] = inf;
} else {
dp[i][j] = dist[i][j] * x - opt[i][j];
}
}
}
for (int k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
}
}
}
for (int i = 0; i < n; i++) {
if (dp[i][i] <= 0) return 1;
}
return 0;
}
main() {
scanf("%d %d %d", &n, &m, &k);
S.resize(n, vector<int>(k, 0));
B.resize(n, vector<int>(k, 0));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
dist[i][j] = inf;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++) {
scanf("%d %d", &B[i][j], &S[i][j]);
}
}
for (int i = 0; i < m; i++) {
int u, v, w;
scanf("%d %d %d", &u, &v, &w);
u--;
v--;
dist[u][v] = w;
}
for (int k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int l = 0; l < k; l++) {
if (B[i][l] >= 0 && S[j][l] >= 0)
opt[i][j] = max(opt[i][j], -B[i][l] * 1LL + S[j][l]);
}
}
}
int l = 0;
int r = 1e9 + 123;
while (r - l > 1) {
int mid = (l + r) >> 1;
if (check(mid)) {
l = mid;
} else {
r = mid;
}
}
cout << l;
}
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... |