# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
840440 | tch1cherin | Travelling Merchant (APIO17_merchant) | C++17 | 0 ms | 0 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>
using namespace std;
const long long INF = 1ll << 60;
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int64_t N, M, K;
cin >> N >> M >> K;
vector<vector<int64_t>> B(N, vector<int64_t>(K)), S(N, vector<int64_t>(K));
for (int i = 0; i < N; i++) {
for (int j = 0; j < K; j++) {
cin >> B[i][j] >> S[i][j];
}
}
vector<vector<pair<int64_t, int64_t>>> G(N);
for (int i = 0; i < M; i++) {
int64_t u, v, t;
cin >> u >> v >> t;
u--, v--;
G[u].push_back({v, t});
}
vector dist(N, vector<long long>(N, INF));
for (int u = 0; u < N; u++) {
for (auto [v, t] : G[u]) {
dist[u][v] = t;
}
}
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]);
vector profit(N, vector<int64_t>(N));
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
for (int k = 0; k < K; k++)
if (B[i][k] != -1 && S[j][k] != -1)
profit[i][j] = max(profit[i][j], (long long)S[j][k] - B[i][k]);
int64_t l = -1, r = 1 << 30;
while (r > l + 1) {
int64_t mid = (l + r) / 2;
vector dis(N, vector<__int128_t>(N, INF));
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
dis[i][j] = min(INF, 1LL * min(2 * INF / mid, dist[i][j]) * mid - profit[i][j]);
for (int k = 0; k < N; k++)
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
bool good = false;
for (int i = 0; i < N; i++) {
if (dis[i][i] <= 0) {
good = true;
}
}
if (good) {
l = mid;
} else {
r = mid;
}
}
cout << l << "\n";
}